From f42a2065246b7cef6d307478e146cb59d563504c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 16 May 2008 15:11:30 +0000 Subject: [PATCH] add property change events reorder properties git-svn-id: http://trooper/svn/projects/jSite/trunk@897 c3eda9e8-030b-0410-8277-bc7414b0a119 --- src/net/pterodactylus/jsite/core/Project.java | 137 +++++++++++++++++++++----- 1 file changed, 114 insertions(+), 23 deletions(-) diff --git a/src/net/pterodactylus/jsite/core/Project.java b/src/net/pterodactylus/jsite/core/Project.java index cc53268..e57c298 100644 --- a/src/net/pterodactylus/jsite/core/Project.java +++ b/src/net/pterodactylus/jsite/core/Project.java @@ -19,29 +19,102 @@ 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 propertyChangeListeners = Collections.synchronizedList(new ArrayList()); + + /** 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); } } -- 2.7.4