KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > classpath > SourcePathImplementation


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.j2seproject.classpath;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.beans.PropertyChangeSupport JavaDoc;
29 import java.net.URI JavaDoc;
30 import java.net.URL JavaDoc;
31 import org.netbeans.spi.java.classpath.ClassPathImplementation;
32 import org.netbeans.spi.java.classpath.PathResourceImplementation;
33 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
34 import org.netbeans.modules.java.j2seproject.SourceRoots;
35 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
36 import org.netbeans.spi.java.classpath.FilteringPathResourceImplementation;
37 import org.netbeans.spi.java.classpath.support.PathResourceBase;
38 import org.netbeans.spi.project.support.ant.AntProjectHelper;
39 import org.netbeans.spi.project.support.ant.PathMatcher;
40 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
41 import org.openide.ErrorManager;
42 import org.openide.util.WeakListeners;
43
44 /**
45  * Implementation of a single classpath that is derived from one Ant property.
46  */

47 final class SourcePathImplementation implements ClassPathImplementation, PropertyChangeListener JavaDoc {
48
49     private static final String JavaDoc PROP_BUILD_DIR = "build.dir"; //NOI18N
50

51     private final PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
52     private List JavaDoc<PathResourceImplementation> resources;
53     private final SourceRoots sourceRoots;
54     private final AntProjectHelper projectHelper;
55     private final PropertyEvaluator evaluator;
56     
57     /**
58      * Construct the implementation.
59      * @param sourceRoots used to get the roots information and events
60      * @param projectHelper used to obtain the project root
61      */

62     public SourcePathImplementation(SourceRoots sourceRoots, AntProjectHelper projectHelper, PropertyEvaluator evaluator) {
63         assert sourceRoots != null && projectHelper != null && evaluator != null;
64         this.sourceRoots = sourceRoots;
65         sourceRoots.addPropertyChangeListener (this);
66         this.projectHelper = projectHelper;
67         this.evaluator = evaluator;
68         evaluator.addPropertyChangeListener (this);
69     }
70
71     public List JavaDoc<PathResourceImplementation> getResources() {
72         synchronized (this) {
73             if (this.resources != null) {
74                 return this.resources;
75             }
76         }
77         URL JavaDoc[] roots = sourceRoots.getRootURLs();
78         synchronized (this) {
79             if (this.resources == null) {
80                 List JavaDoc<PathResourceImplementation> result = new ArrayList JavaDoc<PathResourceImplementation>(roots.length);
81                 for (final URL JavaDoc root : roots) {
82                     class PRI implements FilteringPathResourceImplementation, PropertyChangeListener JavaDoc {
83                         PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
84                         PathMatcher matcher;
85                         PRI() {
86                             evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
87                         }
88                         public URL JavaDoc[] getRoots() {
89                             return new URL JavaDoc[] {root};
90                         }
91                         public boolean includes(URL JavaDoc root, String JavaDoc resource) {
92                             if (matcher == null) {
93                                 matcher = new PathMatcher(
94                                         evaluator.getProperty(J2SEProjectProperties.INCLUDES),
95                                         evaluator.getProperty(J2SEProjectProperties.EXCLUDES),
96                                         new File JavaDoc(URI.create(root.toExternalForm())));
97                             }
98                             return matcher.matches(resource, true);
99                         }
100                         public ClassPathImplementation getContent() {
101                             return null;
102                         }
103                         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
104                             pcs.addPropertyChangeListener(listener);
105                         }
106                         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
107                             pcs.removePropertyChangeListener(listener);
108                         }
109                         public void propertyChange(PropertyChangeEvent JavaDoc ev) {
110                             String JavaDoc prop = ev.getPropertyName();
111                             if (prop == null || prop.equals(J2SEProjectProperties.INCLUDES) || prop.equals(J2SEProjectProperties.EXCLUDES)) {
112                                 matcher = null;
113                                 PropertyChangeEvent JavaDoc ev2 = new PropertyChangeEvent JavaDoc(this, FilteringPathResourceImplementation.PROP_INCLUDES, null, null);
114                                 ev2.setPropagationId(ev);
115                                 pcs.firePropertyChange(ev2);
116                             }
117                         }
118                     }
119                     result.add(new PRI());
120                 }
121                 // adds java artifacts generated by wscompile and wsimport to resources to be available for code completion
122
if (!sourceRoots.isTest()) {
123                     try {
124                         String JavaDoc buildDir = this.evaluator.getProperty(PROP_BUILD_DIR);
125                         if (buildDir != null) {
126                             // generated/wsclient
127
File JavaDoc f = new File JavaDoc (this.projectHelper.resolveFile (buildDir),"generated/wsclient"); //NOI18N
128
URL JavaDoc url = f.toURI().toURL();
129                             if (!f.exists()) { //NOI18N
130
assert !url.toExternalForm().endsWith("/"); //NOI18N
131
url = new URL JavaDoc (url.toExternalForm()+'/'); //NOI18N
132
}
133                             result.add(ClassPathSupport.createResource(url));
134                             
135                             // generated/wsimport/client
136
f = new File JavaDoc (projectHelper.resolveFile(buildDir),"generated/wsimport/client"); //NOI18N
137
url = f.toURI().toURL();
138                             if (!f.exists()) { //NOI18N
139
assert !url.toExternalForm().endsWith("/"); //NOI18N
140
url = new URL JavaDoc (url.toExternalForm()+'/'); //NOI18N
141
}
142                             result.add(ClassPathSupport.createResource(url));
143                         }
144                     } catch (MalformedURLException JavaDoc ex) {
145                         ErrorManager.getDefault ().notify (ex);
146                     }
147                 }
148                 this.resources = Collections.unmodifiableList(result);
149             }
150             return this.resources;
151         }
152     }
153
154     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
155         support.addPropertyChangeListener (listener);
156     }
157
158     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
159         support.removePropertyChangeListener (listener);
160     }
161
162
163     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
164         if (SourceRoots.PROP_ROOTS.equals (evt.getPropertyName())) {
165             synchronized (this) {
166                 this.resources = null;
167             }
168             this.support.firePropertyChange (PROP_RESOURCES,null,null);
169         }
170         else if (this.evaluator != null && evt.getSource() == this.evaluator &&
171             (evt.getPropertyName() == null || PROP_BUILD_DIR.equals(evt.getPropertyName()))) {
172             synchronized (this) {
173                 this.resources = null;
174             }
175             this.support.firePropertyChange (PROP_RESOURCES,null,null);
176         }
177     }
178
179 }
180
Popular Tags