001: // JdbcPropertyChangeSupport.java
002: // $Id: JdbcPropertyChangeSupport.java,v 1.1 2000/09/06 11:57:43 bmahe Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.jdbc;
007:
008: import java.beans.PropertyChangeEvent;
009: import java.beans.PropertyChangeListener;
010: import java.beans.PropertyChangeSupport;
011: import java.util.Hashtable;
012: import java.util.Vector;
013:
014: /**
015: * This subclass of java.beans.PropertyChangeSupport is identical
016: * in functionality except that it fire a PropertyChangeEvent even if the
017: * old value and the new value are identicals.
018: * @version $Revision: 1.1 $
019: * @author Benoît Mahé (bmahe@w3.org)
020: */
021: public class JdbcPropertyChangeSupport extends PropertyChangeSupport {
022:
023: /**
024: * Our listeners
025: */
026: private Vector listeners = null;
027:
028: /**
029: * Our listeners for specific properties
030: */
031: private Hashtable children = null;
032:
033: /**
034: * Our Bean
035: */
036: private Object source = null;
037:
038: /**
039: * Add a PropertyChangeListener to the listener list.
040: * The listener is registered for all properties.
041: * @param listener The PropertyChangeListener to be added
042: */
043: public synchronized void addPropertyChangeListener(
044: PropertyChangeListener listener) {
045: if (listeners == null) {
046: listeners = new Vector();
047: }
048: listeners.addElement(listener);
049: }
050:
051: /**
052: * Remove a PropertyChangeListener from the listener list.
053: * This removes a PropertyChangeListener that was registered
054: * for all properties.
055: * @param listener The PropertyChangeListener to be removed
056: */
057: public synchronized void removePropertyChangeListener(
058: PropertyChangeListener listener) {
059: if (listeners == null) {
060: return;
061: }
062: listeners.removeElement(listener);
063: }
064:
065: /**
066: * Add a PropertyChangeListener for a specific property. The listener
067: * will be invoked only when a call on firePropertyChange names that
068: * specific property.
069: * @param propertyName The name of the property to listen on.
070: * @param listener The PropertyChangeListener to be added
071: */
072: public synchronized void addPropertyChangeListener(
073: String propertyName, PropertyChangeListener listener) {
074: if (children == null) {
075: children = new Hashtable();
076: }
077: JdbcPropertyChangeSupport child = (JdbcPropertyChangeSupport) children
078: .get(propertyName);
079: if (child == null) {
080: child = new JdbcPropertyChangeSupport(source);
081: children.put(propertyName, child);
082: }
083: child.addPropertyChangeListener(listener);
084: }
085:
086: /**
087: * Remove a PropertyChangeListener for a specific property.
088: * @param propertyName The name of the property that was listened on.
089: * @param listener The PropertyChangeListener to be removed
090: */
091: public synchronized void removePropertyChangeListener(
092: String propertyName, PropertyChangeListener listener) {
093: if (children == null) {
094: return;
095: }
096: JdbcPropertyChangeSupport child = (JdbcPropertyChangeSupport) children
097: .get(propertyName);
098: if (child == null) {
099: return;
100: }
101: child.removePropertyChangeListener(listener);
102: }
103:
104: /**
105: * Report a bound property update to any registered listeners.
106: * No event is fired if old and new are equal and non-null.
107: * @param propertyName The programmatic name of the property
108: * that was changed.
109: * @param oldValue The old value of the property.
110: * @param newValue The new value of the property.
111: */
112: public void firePropertyChange(String propertyName,
113: Object oldValue, Object newValue) {
114: Vector targets = null;
115: JdbcPropertyChangeSupport child = null;
116: synchronized (this ) {
117: if (listeners != null) {
118: targets = (Vector) listeners.clone();
119: }
120: if (children != null && propertyName != null) {
121: child = (JdbcPropertyChangeSupport) children
122: .get(propertyName);
123: }
124: }
125:
126: PropertyChangeEvent evt = new PropertyChangeEvent(source,
127: propertyName, oldValue, newValue);
128:
129: if (targets != null) {
130: for (int i = 0; i < targets.size(); i++) {
131: PropertyChangeListener target = (PropertyChangeListener) targets
132: .elementAt(i);
133: target.propertyChange(evt);
134: }
135: }
136:
137: if (child != null) {
138: child.firePropertyChange(evt);
139: }
140: }
141:
142: /**
143: * Report an int bound property update to any registered listeners.
144: * No event is fired if old and new are equal and non-null.
145: * <p>
146: * This is merely a convenience wrapper around the more general
147: * firePropertyChange method that takes Object values.
148: * @param propertyName The programmatic name of the property
149: * that was changed.
150: * @param oldValue The old value of the property.
151: * @param newValue The new value of the property.
152: */
153: public void firePropertyChange(String propertyName, int oldValue,
154: int newValue) {
155: firePropertyChange(propertyName, new Integer(oldValue),
156: new Integer(newValue));
157: }
158:
159: /**
160: * Report a boolean bound property update to any registered listeners.
161: * No event is fired if old and new are equal and non-null.
162: * <p>
163: * This is merely a convenience wrapper around the more general
164: * firePropertyChange method that takes Object values.
165: * @param propertyName The programmatic name of the property
166: * that was changed.
167: * @param oldValue The old value of the property.
168: * @param newValue The new value of the property.
169: */
170: public void firePropertyChange(String propertyName,
171: boolean oldValue, boolean newValue) {
172: firePropertyChange(propertyName, new Boolean(oldValue),
173: new Boolean(newValue));
174: }
175:
176: /**
177: * Fire an existing PropertyChangeEvent to any registered listeners.
178: * No event is fired if the given event's old and new values are
179: * equal and non-null.
180: * @param evt The PropertyChangeEvent object.
181: */
182: public void firePropertyChange(PropertyChangeEvent evt) {
183: String propertyName = evt.getPropertyName();
184: Vector targets = null;
185: JdbcPropertyChangeSupport child = null;
186: synchronized (this ) {
187: if (listeners != null) {
188: targets = (Vector) listeners.clone();
189: }
190: if (children != null && propertyName != null) {
191: child = (JdbcPropertyChangeSupport) children
192: .get(propertyName);
193: }
194: }
195:
196: if (targets != null) {
197: for (int i = 0; i < targets.size(); i++) {
198: PropertyChangeListener target = (PropertyChangeListener) targets
199: .elementAt(i);
200: target.propertyChange(evt);
201: }
202: }
203: if (child != null) {
204: child.firePropertyChange(evt);
205: }
206: }
207:
208: /**
209: * Check if there are any listeners for a specific property.
210: * @param propertyName the property name.
211: * @return true if there are ore or more listeners for the given property
212: */
213: public synchronized boolean hasListeners(String propertyName) {
214: if (listeners != null && !listeners.isEmpty()) {
215: return true;
216: }
217: if (children != null) {
218: JdbcPropertyChangeSupport child = (JdbcPropertyChangeSupport) children
219: .get(propertyName);
220: if (child != null && child.listeners != null) {
221: return !child.listeners.isEmpty();
222: }
223: }
224: return false;
225: }
226:
227: /**
228: * Constructor
229: * @param sourceBean our bean
230: */
231: public JdbcPropertyChangeSupport(Object sourceBean) {
232: super(sourceBean);
233: this.source = sourceBean;
234: }
235:
236: }
|