X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNode.java;h=afcce6a8a410833e7a1271dc41db5355a618fb30;hb=52f29c032eb637965d5342d3bd45843774d926b5;hp=e0d61f7a9adedf273fc384e6dd82eb9e1fd62519;hpb=3189829661d3411ba3f79b1199ee5fab5c75aae5;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/Node.java b/src/net/pterodactylus/jsite/core/Node.java index e0d61f7..afcce6a 100644 --- a/src/net/pterodactylus/jsite/core/Node.java +++ b/src/net/pterodactylus/jsite/core/Node.java @@ -19,6 +19,12 @@ 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. * @@ -27,6 +33,9 @@ package net.pterodactylus.jsite.core; */ public class Node { + /** Property change listeners. */ + private final List propertyChangeListeners = Collections.synchronizedList(new ArrayList()); + /** The name of the node. */ private String name; @@ -36,8 +45,51 @@ public class Node { /** The port number of the node. */ private int port; - /** Whether the node is running on the same machine as jSite. */ - private boolean sameMachine; + // + // 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. @@ -55,7 +107,11 @@ public class Node { * 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("name", oldName, name); + } } /** @@ -74,7 +130,11 @@ public class Node { * 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("hostname", oldHostname, hostname); + } } /** @@ -93,28 +153,40 @@ public class Node { * The port number of the node */ public void setPort(int port) { + int oldPort = this.port; this.port = port; + if (oldPort != port) { + firePropertyChange("port", oldPort, port); + } } - /** - * Returns whether this node is running on the same machine as jSite. - * - * @return the sameMachine true if, and only if, the node is - * running on the same machine as jSite - */ - boolean isSameMachine() { - return sameMachine; - } +// /** +// * {@inheritDoc} Two Node objects are considered equal if their hostnames +// * and their port numbers are equal. +// */ +// @Override +// public boolean equals(Object object) { +// if ((object == null) || !(object instanceof Node)) { +// return false; +// } +// Node node = (Node) object; +// return hostname.equals(node.hostname) && port == node.port; +// } +// /** +// * {@inheritDoc} +// */ +// @Override +// public int hashCode() { +// return hostname.hashCode() ^ (-1 - port); +// } + /** - * Sets whether this node is running on the same machine as jSite. - * - * @param sameMachine - * true if the node is running on the same machine - * as jSite, false otherwise + * {@inheritDoc} */ - void setSameMachine(boolean sameMachine) { - this.sameMachine = sameMachine; + @Override + public String toString() { + return name + " (" + hostname + ((port == 9481) ? ("") : (":" + port)) + ")"; } }