KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > fromdb > ChangeSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb;
21
22 import java.util.Collections JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27
28 /**
29  * A simple equivalent of {@link java.beans.PropertyChangeSupport} for
30  * {@link ChangeListener}'s.
31  *
32  * @author Andrei Badea
33  */

34 public class ChangeSupport {
35
36     // XXX consider moving to j2ee/utilities
37

38     private final Object JavaDoc source;
39     private List JavaDoc<ChangeListener JavaDoc> listeners = Collections.emptyList();
40
41     public ChangeSupport(Object JavaDoc source) {
42         this.source = source;
43     }
44
45     public synchronized void addChangeListener(ChangeListener JavaDoc listener) {
46         List JavaDoc<ChangeListener JavaDoc> newListeners = new LinkedList JavaDoc<ChangeListener JavaDoc>(listeners);
47         newListeners.add(listener);
48         listeners = newListeners;
49     }
50
51     public synchronized void removeChangeListener(ChangeListener JavaDoc listener) {
52         List JavaDoc<ChangeListener JavaDoc> newListeners = new LinkedList JavaDoc<ChangeListener JavaDoc>(listeners);
53         newListeners.remove(listener);
54         listeners = newListeners;
55     }
56
57     public void fireChange() {
58         fireChange(new ChangeEvent JavaDoc(source));
59     }
60
61     public void fireChange(ChangeEvent JavaDoc event) {
62         List JavaDoc<ChangeListener JavaDoc> listenersCopy;
63         synchronized (this) {
64             listenersCopy = listeners;
65         }
66         for (ChangeListener JavaDoc listener : listenersCopy) {
67             listener.stateChanged(event);
68         }
69     }
70
71     public synchronized int getListenerCount() {
72         return listeners.size();
73     }
74 }
75
Popular Tags