~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/api/src/org/netbeans/modules/java/classpath/ProxyClassPathImplementation.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.netbeans.modules.java.classpath;
 
42
 
 
43
import org.netbeans.spi.java.classpath.ClassPathImplementation;
 
44
import org.netbeans.spi.java.classpath.PathResourceImplementation;
 
45
 
 
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;
 
53
 
 
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
 
58
 */
 
59
public class ProxyClassPathImplementation implements ClassPathImplementation {
 
60
 
 
61
    private ClassPathImplementation[] classPaths;
 
62
    private List<PathResourceImplementation> resourcesCache;
 
63
    private ArrayList<PropertyChangeListener> listeners;
 
64
    private PropertyChangeListener classPathsListener;
 
65
 
 
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) {
 
72
            if (cpImpl == null)
 
73
                continue;
 
74
            cpImpl.addPropertyChangeListener (WeakListeners.propertyChange(classPathsListener,cpImpl));
 
75
            impls.add (cpImpl);
 
76
        }
 
77
        this.classPaths = impls.toArray(new ClassPathImplementation[impls.size()]);
 
78
    }
 
79
 
 
80
 
 
81
 
 
82
    public List <? extends PathResourceImplementation> getResources() {
 
83
        synchronized (this) {
 
84
            if (this.resourcesCache != null) {
 
85
                return this.resourcesCache;
 
86
            }
 
87
        }
 
88
        
 
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);
 
95
        }
 
96
        
 
97
        synchronized (this) {
 
98
            if (this.resourcesCache == null) {
 
99
                resourcesCache = Collections.unmodifiableList (result);
 
100
            }
 
101
            return this.resourcesCache;
 
102
        }
 
103
    }
 
104
 
 
105
    public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
 
106
        if (this.listeners == null)
 
107
            this.listeners = new ArrayList<PropertyChangeListener> ();
 
108
        this.listeners.add (listener);
 
109
    }
 
110
 
 
111
    public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
 
112
        if (this.listeners == null)
 
113
            return;
 
114
        this.listeners.remove (listener);
 
115
    }
 
116
    
 
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
 
122
        }
 
123
        builder.append ("]");   //NOI18N
 
124
        return builder.toString ();
 
125
    }
 
126
 
 
127
 
 
128
    private class DelegatesListener implements PropertyChangeListener {
 
129
 
 
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)
 
135
                    return;
 
136
                _listeners = ProxyClassPathImplementation.this.listeners.toArray(new PropertyChangeListener[ProxyClassPathImplementation.this.listeners.size()]);
 
137
            }
 
138
            PropertyChangeEvent event = new PropertyChangeEvent (ProxyClassPathImplementation.this, evt.getPropertyName(),null,null);
 
139
            for (PropertyChangeListener l : _listeners) {
 
140
                l.propertyChange (event);
 
141
            }
 
142
        }
 
143
    }
 
144
 
 
145
}