KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > spi > project > support > rake > FilterPropertyProvider


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.ruby.spi.project.support.rake;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27 import org.openide.util.WeakListeners;
28
29 /**
30  * Property provider that delegates to another source.
31  * Useful, for example, when conditionally loading from one or another properties file.
32  * @since org.netbeans.modules.ruby.modules.project.rake/1 1.14
33  */

34 public abstract class FilterPropertyProvider implements PropertyProvider {
35
36     private PropertyProvider delegate;
37     private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
38     private final ChangeListener JavaDoc strongListener = new ChangeListener JavaDoc() {
39         public void stateChanged(ChangeEvent JavaDoc e) {
40             //System.err.println("DPP: change from current provider " + delegate);
41
fireChange();
42         }
43     };
44     private ChangeListener JavaDoc weakListener = null; // #50572: must be weak
45

46     /**
47      * Initialize the proxy.
48      * @param delegate the initial delegate to use
49      */

50     protected FilterPropertyProvider(PropertyProvider delegate) {
51         assert delegate != null;
52         setDelegate(delegate);
53     }
54
55     /**
56      * Change the current delegate (firing changes as well).
57      * @param delegate the initial delegate to use
58      */

59     protected final void setDelegate(PropertyProvider delegate) {
60         if (delegate == this.delegate) {
61             return;
62         }
63         if (this.delegate != null) {
64             assert weakListener != null;
65             this.delegate.removeChangeListener(weakListener);
66         }
67         this.delegate = delegate;
68         weakListener = WeakListeners.change(strongListener, delegate);
69         delegate.addChangeListener(weakListener);
70         fireChange();
71     }
72
73     public final Map JavaDoc<String JavaDoc, String JavaDoc> getProperties() {
74         return delegate.getProperties();
75     }
76
77     public final synchronized void addChangeListener(ChangeListener JavaDoc listener) {
78         // XXX could listen to delegate only when this has listeners
79
listeners.add(listener);
80     }
81
82     public final synchronized void removeChangeListener(ChangeListener JavaDoc listener) {
83         listeners.add(listener);
84     }
85
86     private void fireChange() {
87         ChangeListener JavaDoc[] ls;
88         synchronized (this) {
89             if (listeners.isEmpty()) {
90                 return;
91             }
92             ls = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
93         }
94         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
95         for(ChangeListener JavaDoc l : ls) {
96             l.stateChanged(ev);
97         }
98     }
99
100 }
101
Popular Tags