KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > java > classpath > support > CompositePathResourceBase


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.spi.java.classpath.support;
20
21 import org.netbeans.spi.java.classpath.PathResourceImplementation;
22 import org.netbeans.spi.java.classpath.ClassPathImplementation;
23
24 import java.net.URL JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Arrays JavaDoc;
31
32 /**
33  * This class provides a base class for PathResource implementations
34  * @since org.netbeans.api.java/1 1.4
35  */

36 public abstract class CompositePathResourceBase implements PathResourceImplementation {
37
38     private URL JavaDoc[] roots;
39     private ClassPathImplementation model;
40     private ArrayList JavaDoc<PropertyChangeListener JavaDoc> pListeners;
41
42     /**
43      * Returns the roots of the PathResource
44      * @return URL[]
45      */

46     public final URL JavaDoc[] getRoots() {
47         if (this.roots == null) {
48             synchronized (this) {
49                 if (this.roots == null) {
50                     initContent ();
51                     List JavaDoc<URL JavaDoc> result = new ArrayList JavaDoc<URL JavaDoc> ();
52                     for (PathResourceImplementation pri : this.model.getResources()) {
53                         result.addAll (Arrays.asList(pri.getRoots()));
54                     }
55                     this.roots = result.toArray (new URL JavaDoc [result.size()]);
56                 }
57             }
58         }
59         return this.roots;
60     }
61
62
63     /**
64      * Returns the ClassPathImplementation representing the content of this PathResourceImplementation
65      * @return ClassPathImplementation
66      */

67     public final ClassPathImplementation getContent() {
68         initContent ();
69         return this.model;
70     }
71
72     /**
73      * Adds property change listener.
74      * The listener is notified when the roots of the PathResource are changed.
75      * @param listener
76      */

77     public synchronized final void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
78         if (this.pListeners == null)
79             this.pListeners = new ArrayList JavaDoc<PropertyChangeListener JavaDoc> ();
80         this.pListeners.add (listener);
81     }
82
83     /**
84      * Removes PropertyChangeListener
85      * @param listener
86      */

87     public synchronized final void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
88         if (this.pListeners == null)
89             return;
90         this.pListeners.remove (listener);
91     }
92
93     /**
94      * Fires PropertyChangeEvent
95      * @param propName name of property
96      * @param oldValue old property value or null
97      * @param newValue new property value or null
98      */

99     protected final void firePropertyChange (String JavaDoc propName, Object JavaDoc oldValue, Object JavaDoc newValue) {
100         PropertyChangeListener JavaDoc[] _listeners;
101         synchronized (this) {
102             if (this.pListeners == null)
103                 return;
104             _listeners = this.pListeners.toArray(new PropertyChangeListener JavaDoc[this.pListeners.size()]);
105         }
106         PropertyChangeEvent JavaDoc event = new PropertyChangeEvent JavaDoc (this, propName, oldValue, newValue);
107         for (PropertyChangeListener JavaDoc l : _listeners) {
108             l.propertyChange (event);
109         }
110     }
111
112     /** Creates the array of the roots of PathResource.
113      * Most PathResource (directory, jar) have single root,
114      * but the PathResource can have more than one root to
115      * represent more complex resources like libraries.
116      * The returned value is cached.
117      * @return ClassPathImplementation
118      */

119     protected abstract ClassPathImplementation createContent ();
120
121
122     private synchronized void initContent () {
123         if (this.model == null) {
124             ClassPathImplementation cp = createContent ();
125             assert cp != null;
126             cp.addPropertyChangeListener (new PropertyChangeListener JavaDoc () {
127                 public void propertyChange (PropertyChangeEvent JavaDoc event) {
128                     roots = null;
129                     firePropertyChange (PROP_ROOTS, null,null);
130                 }
131             });
132             this.model = cp;
133         }
134     }
135 }
136
Popular Tags