add property change events
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 16 May 2008 15:11:30 +0000 (15:11 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 16 May 2008 15:11:30 +0000 (15:11 +0000)
reorder properties

git-svn-id: http://trooper/svn/projects/jSite/trunk@897 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/core/Project.java

index cc53268..e57c298 100644 (file)
 
 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 &lt;bombe@freenetproject.org&gt;
  * @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.
         * 
@@ -58,7 +131,11 @@ public class 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);
+               }
        }
 
        /**
@@ -77,26 +154,11 @@ public class Project {
         *            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);
+               }
        }
 
        /**
@@ -115,7 +177,11 @@ public class Project {
         *            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);
+               }
        }
 
        /**
@@ -134,7 +200,32 @@ public class Project {
         *            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);
        }
 
 }