X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNode.java;h=7751225fd167b75a48da6cdfaa8311ca8931c6e1;hb=46468a98b62f04438ff5e456b199c77e00da4c44;hp=468a256d0b0c08808dd56497a121046115fbe434;hpb=47d63e2ca99951d9a4bcd5cf54017b2a06a9137c;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/Node.java b/src/net/pterodactylus/jsite/core/Node.java index 468a256..7751225 100644 --- a/src/net/pterodactylus/jsite/core/Node.java +++ b/src/net/pterodactylus/jsite/core/Node.java @@ -19,14 +19,33 @@ package net.pterodactylus.jsite.core; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + /** - * Container for a Freenet node. - * + * Container for a Freenet node. A Node is capable of notifying + * {@link PropertyChangeListener}s if any of the contained properties change. + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> * @version $Id$ */ public class Node { + /** Property change listeners. */ + private final List propertyChangeListeners = Collections.synchronizedList(new ArrayList()); + + /** Name of the “name” property. */ + public static final String PROPERTY_NAME = "name"; + + /** Name of the “hostname” property. */ + public static final String PROPERTY_HOSTNAME = "hostname"; + + /** Name of the “port” property. */ + public static final String PROPERTY_PORT = "port"; + /** The name of the node. */ private String name; @@ -36,9 +55,55 @@ public class Node { /** The port number of the node. */ private int port; + // + // EVENT MANAGEMENT + // + + /** + * Adds a property change listener. + * + * @param propertyChangeListener + * The property change listener to add + */ + public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) { + propertyChangeListeners.add(propertyChangeListener); + } + + /** + * Removes a property change listener. + * + * @param propertyChangeListener + * The property change listener to remove + */ + public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) { + propertyChangeListeners.remove(propertyChangeListener); + } + + /** + * Notifies all listeners that a property has changed. + * + * @param property + * The name of the property + * @param oldValue + * The old value of the property + * @param newValue + * The new value of the property + */ + private void firePropertyChange(String property, Object oldValue, Object newValue) { + PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue); + for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) { + propertyChangeListener.propertyChange(propertyChangeEvent); + } + + } + + // + // ACCESSORS + // + /** * Returns the user-given name of the node. - * + * * @return The name of the node */ public String getName() { @@ -47,17 +112,21 @@ public class Node { /** * Sets the user-given name of the node. - * + * * @param name * The name of the node */ public void setName(String name) { + String oldName = this.name; this.name = name; + if (((oldName != null) && (name == null)) || ((oldName == null) && (name != null)) || ((name != null) && !name.equals(oldName))) { + firePropertyChange(PROPERTY_NAME, oldName, name); + } } /** * Returns the hostname of the node. - * + * * @return The hostname of the node */ public String getHostname() { @@ -66,17 +135,21 @@ public class Node { /** * Sets the hostname of the node. - * + * * @param hostname * The hostname of the node */ public void setHostname(String hostname) { + String oldHostname = this.hostname; this.hostname = hostname; + if (((oldHostname != null) && (hostname == null)) || ((oldHostname == null) && (hostname != null)) || ((hostname != null) && !hostname.equals(oldHostname))) { + firePropertyChange(PROPERTY_HOSTNAME, oldHostname, hostname); + } } /** * Returns the port number of the node. - * + * * @return The port number of the node */ public int getPort() { @@ -85,12 +158,16 @@ public class Node { /** * Sets the port number of the node. - * + * * @param port * The port number of the node */ public void setPort(int port) { + int oldPort = this.port; this.port = port; + if (oldPort != port) { + firePropertyChange(PROPERTY_PORT, oldPort, port); + } } /**