KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > classpath > ProxyClassPathImplementation


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 package org.netbeans.modules.java.classpath;
20
21 import org.netbeans.spi.java.classpath.ClassPathImplementation;
22 import org.netbeans.spi.java.classpath.PathResourceImplementation;
23
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import org.openide.util.WeakListeners;
31
32 /** ProxyClassPathImplementation provides read only proxy for ClassPathImplementations.
33  * The order of the resources is given by the order of its delegates.
34  * The proxy is designed to be used as a union of class paths.
35  * E.g. to be able to easily iterate or listen on all design resources = sources + compile resources
36  */

37 public class ProxyClassPathImplementation implements ClassPathImplementation {
38
39     private ClassPathImplementation[] classPaths;
40     private List JavaDoc<PathResourceImplementation> resourcesCache;
41     private ArrayList JavaDoc<PropertyChangeListener JavaDoc> listeners;
42     private PropertyChangeListener JavaDoc classPathsListener;
43
44     public ProxyClassPathImplementation (ClassPathImplementation[] classPaths) {
45         if (classPaths == null)
46             throw new IllegalArgumentException JavaDoc ();
47         List JavaDoc<ClassPathImplementation> impls = new ArrayList JavaDoc<ClassPathImplementation> ();
48         classPathsListener = new DelegatesListener ();
49         for (ClassPathImplementation cpImpl : classPaths) {
50             if (cpImpl == null)
51                 continue;
52             cpImpl.addPropertyChangeListener (WeakListeners.propertyChange(classPathsListener,cpImpl));
53             impls.add (cpImpl);
54         }
55         this.classPaths = impls.toArray(new ClassPathImplementation[impls.size()]);
56     }
57
58
59
60     public List JavaDoc <? extends PathResourceImplementation> getResources() {
61         synchronized (this) {
62             if (this.resourcesCache != null) {
63                 return this.resourcesCache;
64             }
65         }
66         
67         ArrayList JavaDoc<PathResourceImplementation> result = new ArrayList JavaDoc<PathResourceImplementation> (classPaths.length*10);
68         for (ClassPathImplementation cpImpl : classPaths) {
69             List JavaDoc<? extends PathResourceImplementation> subPath = cpImpl.getResources();
70             assert subPath != null : "ClassPathImplementation.getResources() returned null. ClassPathImplementation.class: "
71                 + cpImpl.getClass().toString() + " ClassPathImplementation: " + cpImpl.toString();
72             result.addAll (subPath);
73         }
74         
75         synchronized (this) {
76             if (this.resourcesCache == null) {
77                 resourcesCache = Collections.unmodifiableList (result);
78             }
79             return this.resourcesCache;
80         }
81     }
82
83     public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
84         if (this.listeners == null)
85             this.listeners = new ArrayList JavaDoc<PropertyChangeListener JavaDoc> ();
86         this.listeners.add (listener);
87     }
88
89     public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
90         if (this.listeners == null)
91             return;
92         this.listeners.remove (listener);
93     }
94     
95     public String JavaDoc toString () {
96         StringBuilder JavaDoc builder = new StringBuilder JavaDoc("["); //NOI18N
97
for (ClassPathImplementation cpImpl : this.classPaths) {
98             builder.append (cpImpl.toString());
99             builder.append(", "); //NOI18N
100
}
101         builder.append ("]"); //NOI18N
102
return builder.toString ();
103     }
104
105
106     private class DelegatesListener implements PropertyChangeListener JavaDoc {
107
108         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
109             PropertyChangeListener JavaDoc[] _listeners;
110             synchronized (ProxyClassPathImplementation.this) {
111                 ProxyClassPathImplementation.this.resourcesCache = null; //Clean the cache
112
if (ProxyClassPathImplementation.this.listeners == null)
113                     return;
114                 _listeners = ProxyClassPathImplementation.this.listeners.toArray(new PropertyChangeListener JavaDoc[ProxyClassPathImplementation.this.listeners.size()]);
115             }
116             PropertyChangeEvent JavaDoc event = new PropertyChangeEvent JavaDoc (ProxyClassPathImplementation.this, evt.getPropertyName(),null,null);
117             for (PropertyChangeListener JavaDoc l : _listeners) {
118                 l.propertyChange (event);
119             }
120         }
121     }
122
123 }
124
Popular Tags