2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.java.classpath;
43
import org.netbeans.spi.java.classpath.ClassPathImplementation;
44
import org.netbeans.spi.java.classpath.PathResourceImplementation;
46
import java.util.List;
47
import java.util.ArrayList;
48
import java.util.Iterator;
49
import java.util.Collections;
50
import java.beans.PropertyChangeListener;
51
import java.beans.PropertyChangeEvent;
52
import org.openide.util.WeakListeners;
54
/** ProxyClassPathImplementation provides read only proxy for ClassPathImplementations.
55
* The order of the resources is given by the order of its delegates.
56
* The proxy is designed to be used as a union of class paths.
57
* E.g. to be able to easily iterate or listen on all design resources = sources + compile resources
59
public class ProxyClassPathImplementation implements ClassPathImplementation {
61
private ClassPathImplementation[] classPaths;
62
private List<PathResourceImplementation> resourcesCache;
63
private ArrayList<PropertyChangeListener> listeners;
64
private PropertyChangeListener classPathsListener;
66
public ProxyClassPathImplementation (ClassPathImplementation[] classPaths) {
67
if (classPaths == null)
68
throw new IllegalArgumentException ();
69
List<ClassPathImplementation> impls = new ArrayList<ClassPathImplementation> ();
70
classPathsListener = new DelegatesListener ();
71
for (ClassPathImplementation cpImpl : classPaths) {
74
cpImpl.addPropertyChangeListener (WeakListeners.propertyChange(classPathsListener,cpImpl));
77
this.classPaths = impls.toArray(new ClassPathImplementation[impls.size()]);
82
public List <? extends PathResourceImplementation> getResources() {
84
if (this.resourcesCache != null) {
85
return this.resourcesCache;
89
ArrayList<PathResourceImplementation> result = new ArrayList<PathResourceImplementation> (classPaths.length*10);
90
for (ClassPathImplementation cpImpl : classPaths) {
91
List<? extends PathResourceImplementation> subPath = cpImpl.getResources();
92
assert subPath != null : "ClassPathImplementation.getResources() returned null. ClassPathImplementation.class: "
93
+ cpImpl.getClass().toString() + " ClassPathImplementation: " + cpImpl.toString();
94
result.addAll (subPath);
98
if (this.resourcesCache == null) {
99
resourcesCache = Collections.unmodifiableList (result);
101
return this.resourcesCache;
105
public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
106
if (this.listeners == null)
107
this.listeners = new ArrayList<PropertyChangeListener> ();
108
this.listeners.add (listener);
111
public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
112
if (this.listeners == null)
114
this.listeners.remove (listener);
117
public String toString () {
118
StringBuilder builder = new StringBuilder("["); //NOI18N
119
for (ClassPathImplementation cpImpl : this.classPaths) {
120
builder.append (cpImpl.toString());
121
builder.append(", "); //NOI18N
123
builder.append ("]"); //NOI18N
124
return builder.toString ();
128
private class DelegatesListener implements PropertyChangeListener {
130
public void propertyChange(PropertyChangeEvent evt) {
131
PropertyChangeListener[] _listeners;
132
synchronized (ProxyClassPathImplementation.this) {
133
ProxyClassPathImplementation.this.resourcesCache = null; //Clean the cache
134
if (ProxyClassPathImplementation.this.listeners == null)
136
_listeners = ProxyClassPathImplementation.this.listeners.toArray(new PropertyChangeListener[ProxyClassPathImplementation.this.listeners.size()]);
138
PropertyChangeEvent event = new PropertyChangeEvent (ProxyClassPathImplementation.this, evt.getPropertyName(),null,null);
139
for (PropertyChangeListener l : _listeners) {
140
l.propertyChange (event);