KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > 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
20 package org.netbeans.modules.web.project.classpath;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.beans.PropertyChangeSupport JavaDoc;
30 import java.net.URL JavaDoc;
31 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
32 import org.netbeans.spi.java.classpath.ClassPathImplementation;
33 import org.netbeans.spi.java.classpath.PathResourceImplementation;
34 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
35 import org.netbeans.modules.web.project.SourceRoots;
36 import org.netbeans.spi.project.support.ant.AntProjectHelper;
37 import org.openide.ErrorManager;
38
39 /**
40  * Implementation of a single classpath that is derived from one Ant property.
41  */

42 final class SourcePathImplementation implements ClassPathImplementation, PropertyChangeListener JavaDoc {
43
44     private PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
45     private List JavaDoc resources;
46     private SourceRoots sourceRoots;
47     private AntProjectHelper projectHelper;
48
49     /**
50      * Construct the implementation.
51      * @param sourceRoots used to get the roots information and events
52      */

53     public SourcePathImplementation(SourceRoots sourceRoots) {
54         assert sourceRoots != null;
55         this.sourceRoots = sourceRoots;
56         this.sourceRoots.addPropertyChangeListener (this);
57     }
58     
59     /**
60      * Construct the implementation.
61      * @param sourceRoots used to get the roots information and events
62      * @param projectHelper used to obtain the project root
63      */

64     public SourcePathImplementation(SourceRoots sourceRoots, AntProjectHelper projectHelper) {
65         assert sourceRoots != null;
66         this.sourceRoots = sourceRoots;
67         this.sourceRoots.addPropertyChangeListener (this);
68         this.projectHelper=projectHelper;
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 = this.sourceRoots.getRootURLs();
78         String JavaDoc buildDir = projectHelper.getStandardPropertyEvaluator().getProperty(WebProjectProperties.BUILD_DIR);
79         synchronized (this) {
80             if (this.resources == null) {
81                 List JavaDoc result = new ArrayList JavaDoc (roots.length);
82                 for (int i = 0; i < roots.length; i++) {
83                     PathResourceImplementation res = ClassPathSupport.createResource(roots[i]);
84                     result.add (res);
85                 }
86                 // adds build/generated/wsclient and build/generated/wsimport to resources to be available for code completion
87
if (projectHelper!=null) {
88                     try {
89                         if (buildDir!=null) {
90                             // generated/wsclient
91
File JavaDoc f = new File JavaDoc (projectHelper.resolveFile(buildDir),"generated/wsclient"); //NOI18N
92
URL JavaDoc url = f.toURI().toURL();
93                             if (!f.exists()) { //NOI18N
94
assert !url.toExternalForm().endsWith("/"); //NOI18N
95
url = new URL JavaDoc (url.toExternalForm()+'/'); //NOI18N
96
}
97                             result.add(ClassPathSupport.createResource(url));
98                             // generated/wsimport/client
99
f = new File JavaDoc (projectHelper.resolveFile(buildDir),"generated/wsimport/client"); //NOI18N
100
url = f.toURI().toURL();
101                             if (!f.exists()) { //NOI18N
102
assert !url.toExternalForm().endsWith("/"); //NOI18N
103
url = new URL JavaDoc (url.toExternalForm()+'/'); //NOI18N
104
}
105                             result.add(ClassPathSupport.createResource(url));
106                             // generated/wsimport/service
107
f = new File JavaDoc (projectHelper.resolveFile(buildDir),"generated/wsimport/service"); //NOI18N
108
url = f.toURI().toURL();
109                             if (!f.exists()) { //NOI18N
110
assert !url.toExternalForm().endsWith("/"); //NOI18N
111
url = new URL JavaDoc (url.toExternalForm()+'/'); //NOI18N
112
}
113                             result.add(ClassPathSupport.createResource(url));
114                         }
115                     } catch (MalformedURLException JavaDoc ex) {
116                         ErrorManager.getDefault ().notify (ex);
117                     }
118                 }
119                 this.resources = Collections.unmodifiableList(result);
120             }
121         }
122         return this.resources;
123     }
124
125     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
126         support.addPropertyChangeListener (listener);
127     }
128
129     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
130         support.removePropertyChangeListener (listener);
131     }
132
133
134     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
135         if (SourceRoots.PROP_ROOTS.equals (evt.getPropertyName())) {
136             synchronized (this) {
137                 this.resources = null;
138             }
139             this.support.firePropertyChange (PROP_RESOURCES,null,null);
140         }
141     }
142
143 }
144
Popular Tags