add property change events
[jSite2.git] / src / net / pterodactylus / jsite / core / Project.java
1 /*
2  * jSite2 - Project.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.core;
21
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27
28 import net.pterodactylus.util.beans.Comparer;
29
30 /**
31  * Container for project information. A Project is capable of notifying
32  * {@link PropertyChangeListener}s if any of the contained properties change.
33  * 
34  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
35  * @version $Id$
36  */
37 public class Project {
38
39         /** Property change listeners. */
40         private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
41
42         /** Name of the “name” property. */
43         public static final String PROPERTY_NAME = "name";
44
45         /** Name of the “description” property. */
46         public static final String PROPERTY_DESCRIPTION = "description";
47
48         /** Name of the “public key” property. */
49         public static final String PROPERTY_PUBLIC_KEY = "publicKey";
50
51         /** Name of the “private key” property. */
52         public static final String PROPERTY_PRIVATE_KEY = "privateKey";
53
54         /** Name of the “local path” property. */
55         public static final String PROPERTY_LOCAL_PATH = "localPath";
56
57         /** The name of the project. */
58         private String name;
59
60         /** The description of the project. */
61         private String description;
62
63         /** The public key. */
64         private String publicKey;
65
66         /** The private key. */
67         private String privateKey;
68
69         /** The local path of the project. */
70         private String localPath;
71
72         //
73         // EVENT MANAGEMENT
74         //
75
76         /**
77          * Adds a property change listener.
78          * 
79          * @param propertyChangeListener
80          *            The property change listener to add
81          */
82         public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
83                 propertyChangeListeners.add(propertyChangeListener);
84         }
85
86         /**
87          * Removes a property change listener.
88          * 
89          * @param propertyChangeListener
90          *            The property change listener to remove
91          */
92         public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
93                 propertyChangeListeners.remove(propertyChangeListener);
94         }
95
96         /**
97          * Notifies all listeners that a property has changed.
98          * 
99          * @param property
100          *            The name of the property
101          * @param oldValue
102          *            The old value of the property
103          * @param newValue
104          *            The new value of the property
105          */
106         private void firePropertyChange(String property, Object oldValue, Object newValue) {
107                 PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue);
108                 for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) {
109                         propertyChangeListener.propertyChange(propertyChangeEvent);
110                 }
111
112         }
113
114         //
115         // ACCESSORS
116         //
117
118         /**
119          * Returns the name of the project.
120          * 
121          * @return The name of the project
122          */
123         public String getName() {
124                 return name;
125         }
126
127         /**
128          * Sets the name of the project.
129          * 
130          * @param name
131          *            The name of the project
132          */
133         public void setName(String name) {
134                 String oldName = this.name;
135                 this.name = name;
136                 if (!Comparer.equal(oldName, name)) {
137                         firePropertyChange(PROPERTY_NAME, oldName, name);
138                 }
139         }
140
141         /**
142          * Returns the description of the project.
143          * 
144          * @return The description of the project
145          */
146         public String getDescription() {
147                 return description;
148         }
149
150         /**
151          * Sets the description of the project
152          * 
153          * @param description
154          *            The description of the project
155          */
156         public void setDescription(String description) {
157                 String oldDescription = this.description;
158                 this.description = description;
159                 if (!Comparer.equal(oldDescription, description)) {
160                         firePropertyChange(PROPERTY_DESCRIPTION, oldDescription, description);
161                 }
162         }
163
164         /**
165          * Returns the public key of the project.
166          * 
167          * @return The public key of the project
168          */
169         public String getPublicKey() {
170                 return publicKey;
171         }
172
173         /**
174          * Sets the public key of the project.
175          * 
176          * @param publicKey
177          *            The public key of the project
178          */
179         public void setPublicKey(String publicKey) {
180                 String oldPublicKey = this.publicKey;
181                 this.publicKey = publicKey;
182                 if (!Comparer.equal(oldPublicKey, publicKey)) {
183                         firePropertyChange(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
184                 }
185         }
186
187         /**
188          * Returns the private key of the project.
189          * 
190          * @return The private key of the project
191          */
192         public String getPrivateKey() {
193                 return privateKey;
194         }
195
196         /**
197          * Sets the private key of the project.
198          * 
199          * @param privateKey
200          *            The private key of the project
201          */
202         public void setPrivateKey(String privateKey) {
203                 String oldPrivateKey = this.privateKey;
204                 this.privateKey = privateKey;
205                 if (!Comparer.equal(oldPrivateKey, privateKey)) {
206                         firePropertyChange(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
207                 }
208         }
209
210         /**
211          * Returns the local path of the project.
212          * 
213          * @return The local path of the project
214          */
215         public String getLocalPath() {
216                 return localPath;
217         }
218
219         /**
220          * Sets the local path of the project.
221          * 
222          * @param localPath
223          *            The local path of the project
224          */
225         public void setLocalPath(String localPath) {
226                 String oldLocalPath = this.localPath;
227                 this.localPath = localPath;
228                 firePropertyChange(PROPERTY_LOCAL_PATH, oldLocalPath, localPath);
229         }
230
231 }