001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.java.classpath;
042:
043: import org.netbeans.spi.java.classpath.ClassPathImplementation;
044: import org.netbeans.spi.java.classpath.PathResourceImplementation;
045:
046: import java.util.List;
047: import java.util.ArrayList;
048: import java.util.Iterator;
049: import java.util.Collections;
050: import java.beans.PropertyChangeListener;
051: import java.beans.PropertyChangeEvent;
052: import org.openide.util.WeakListeners;
053:
054: /** ProxyClassPathImplementation provides read only proxy for ClassPathImplementations.
055: * The order of the resources is given by the order of its delegates.
056: * The proxy is designed to be used as a union of class paths.
057: * E.g. to be able to easily iterate or listen on all design resources = sources + compile resources
058: */
059: public class ProxyClassPathImplementation implements
060: ClassPathImplementation {
061:
062: private ClassPathImplementation[] classPaths;
063: private List<PathResourceImplementation> resourcesCache;
064: private ArrayList<PropertyChangeListener> listeners;
065: private PropertyChangeListener classPathsListener;
066:
067: public ProxyClassPathImplementation(
068: ClassPathImplementation[] classPaths) {
069: if (classPaths == null)
070: throw new IllegalArgumentException();
071: List<ClassPathImplementation> impls = new ArrayList<ClassPathImplementation>();
072: classPathsListener = new DelegatesListener();
073: for (ClassPathImplementation cpImpl : classPaths) {
074: if (cpImpl == null)
075: continue;
076: cpImpl.addPropertyChangeListener(WeakListeners
077: .propertyChange(classPathsListener, cpImpl));
078: impls.add(cpImpl);
079: }
080: this .classPaths = impls
081: .toArray(new ClassPathImplementation[impls.size()]);
082: }
083:
084: public List<? extends PathResourceImplementation> getResources() {
085: synchronized (this ) {
086: if (this .resourcesCache != null) {
087: return this .resourcesCache;
088: }
089: }
090:
091: ArrayList<PathResourceImplementation> result = new ArrayList<PathResourceImplementation>(
092: classPaths.length * 10);
093: for (ClassPathImplementation cpImpl : classPaths) {
094: List<? extends PathResourceImplementation> subPath = cpImpl
095: .getResources();
096: assert subPath != null : "ClassPathImplementation.getResources() returned null. ClassPathImplementation.class: "
097: + cpImpl.getClass().toString()
098: + " ClassPathImplementation: " + cpImpl.toString();
099: result.addAll(subPath);
100: }
101:
102: synchronized (this ) {
103: if (this .resourcesCache == null) {
104: resourcesCache = Collections.unmodifiableList(result);
105: }
106: return this .resourcesCache;
107: }
108: }
109:
110: public synchronized void addPropertyChangeListener(
111: PropertyChangeListener listener) {
112: if (this .listeners == null)
113: this .listeners = new ArrayList<PropertyChangeListener>();
114: this .listeners.add(listener);
115: }
116:
117: public synchronized void removePropertyChangeListener(
118: PropertyChangeListener listener) {
119: if (this .listeners == null)
120: return;
121: this .listeners.remove(listener);
122: }
123:
124: public String toString() {
125: StringBuilder builder = new StringBuilder("["); //NOI18N
126: for (ClassPathImplementation cpImpl : this .classPaths) {
127: builder.append(cpImpl.toString());
128: builder.append(", "); //NOI18N
129: }
130: builder.append("]"); //NOI18N
131: return builder.toString();
132: }
133:
134: private class DelegatesListener implements PropertyChangeListener {
135:
136: public void propertyChange(PropertyChangeEvent evt) {
137: PropertyChangeListener[] _listeners;
138: synchronized (ProxyClassPathImplementation.this ) {
139: ProxyClassPathImplementation.this .resourcesCache = null; //Clean the cache
140: if (ProxyClassPathImplementation.this .listeners == null)
141: return;
142: _listeners = ProxyClassPathImplementation.this .listeners
143: .toArray(new PropertyChangeListener[ProxyClassPathImplementation.this .listeners
144: .size()]);
145: }
146: PropertyChangeEvent event = new PropertyChangeEvent(
147: ProxyClassPathImplementation.this , evt
148: .getPropertyName(), null, null);
149: for (PropertyChangeListener l : _listeners) {
150: l.propertyChange(event);
151: }
152: }
153: }
154:
155: }
|