extract abstract bean superclass
[jSite2.git] / src / net / pterodactylus / util / beans / AbstractBean.java
1 /*
2  * jSite2 - AbstractBean.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.util.beans;
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 /**
29  * Abstract bean super class that contains property change listener management.
30  * 
31  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32  * @version $Id$
33  */
34 public class AbstractBean {
35
36         /** Property change listeners. */
37         private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
38
39         /**
40          * Adds a property change listener.
41          * 
42          * @param propertyChangeListener
43          *            The property change listener to add
44          */
45         public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
46                 propertyChangeListeners.add(propertyChangeListener);
47         }
48
49         /**
50          * Removes a property change listener.
51          * 
52          * @param propertyChangeListener
53          *            The property change listener to remove
54          */
55         public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
56                 propertyChangeListeners.remove(propertyChangeListener);
57         }
58
59         /**
60          * Notifies all listeners that a property has changed.
61          * 
62          * @param property
63          *            The name of the property
64          * @param oldValue
65          *            The old value of the property
66          * @param newValue
67          *            The new value of the property
68          */
69         protected void firePropertyChange(String property, Object oldValue, Object newValue) {
70                 PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue);
71                 for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) {
72                         propertyChangeListener.propertyChange(propertyChangeEvent);
73                 }
74
75         }
76
77 }