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;
+
+import net.pterodactylus.util.beans.Comparer;
+
/**
- * Container for project information.
+ * Container for project information. A Project 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 Project {
+ /** Property change listeners. */
+ private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
+
+ /** Name of the “name” property. */
+ public static final String PROPERTY_NAME = "name";
+
+ /** Name of the “description” property. */
+ public static final String PROPERTY_DESCRIPTION = "description";
+
+ /** Name of the “public key” property. */
+ public static final String PROPERTY_PUBLIC_KEY = "publicKey";
+
+ /** Name of the “private key” property. */
+ public static final String PROPERTY_PRIVATE_KEY = "privateKey";
+
+ /** Name of the “local path” property. */
+ public static final String PROPERTY_LOCAL_PATH = "localPath";
+
/** The name of the project. */
private String name;
/** The description of the project. */
private String description;
- /** The local path of the project. */
- private String localPath;
-
/** The public key. */
private String publicKey;
/** The private key. */
private String privateKey;
+ /** The local path of the project. */
+ private String localPath;
+
+ //
+ // 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 name of the project.
*
* The name of the project
*/
public void setName(String name) {
+ String oldName = this.name;
this.name = name;
+ if (!Comparer.equal(oldName, name)) {
+ firePropertyChange(PROPERTY_NAME, oldName, name);
+ }
}
/**
* The description of the project
*/
public void setDescription(String description) {
+ String oldDescription = this.description;
this.description = description;
- }
-
- /**
- * Returns the local path of the project.
- *
- * @return The local path of the project
- */
- public String getLocalPath() {
- return localPath;
- }
-
- /**
- * Sets the local path of the project.
- *
- * @param localPath
- * The local path of the project
- */
- public void setLocalPath(String localPath) {
- this.localPath = localPath;
+ if (!Comparer.equal(oldDescription, description)) {
+ firePropertyChange(PROPERTY_DESCRIPTION, oldDescription, description);
+ }
}
/**
* The public key of the project
*/
public void setPublicKey(String publicKey) {
+ String oldPublicKey = this.publicKey;
this.publicKey = publicKey;
+ if (!Comparer.equal(oldPublicKey, publicKey)) {
+ firePropertyChange(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
+ }
}
/**
* The private key of the project
*/
public void setPrivateKey(String privateKey) {
+ String oldPrivateKey = this.privateKey;
this.privateKey = privateKey;
+ if (!Comparer.equal(oldPrivateKey, privateKey)) {
+ firePropertyChange(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
+ }
+ }
+
+ /**
+ * Returns the local path of the project.
+ *
+ * @return The local path of the project
+ */
+ public String getLocalPath() {
+ return localPath;
+ }
+
+ /**
+ * Sets the local path of the project.
+ *
+ * @param localPath
+ * The local path of the project
+ */
+ public void setLocalPath(String localPath) {
+ String oldLocalPath = this.localPath;
+ this.localPath = localPath;
+ firePropertyChange(PROPERTY_LOCAL_PATH, oldLocalPath, localPath);
}
}