KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > 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.ruby.rubyproject.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.URL JavaDoc;
30 import org.netbeans.modules.ruby.rubyproject.SourceRoots;
31 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyEvaluator;
32 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
33 import org.netbeans.spi.java.classpath.ClassPathImplementation;
34 import org.netbeans.spi.java.classpath.PathResourceImplementation;
35 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
36 import org.openide.ErrorManager;
37
38 /**
39  * Implementation of a single classpath that is derived from one Ant property.
40  */

41 final class SourcePathImplementation implements ClassPathImplementation, PropertyChangeListener JavaDoc {
42
43     private static final String JavaDoc PROP_BUILD_DIR = "build.dir"; //NOI18N
44

45     private final PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
46     private List JavaDoc<PathResourceImplementation> resources;
47     private final SourceRoots sourceRoots;
48     private final RakeProjectHelper projectHelper;
49     private final PropertyEvaluator evaluator;
50     
51     /**
52      * Construct the implementation.
53      * @param sourceRoots used to get the roots information and events
54      */

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

68     public SourcePathImplementation(SourceRoots sourceRoots, RakeProjectHelper projectHelper, PropertyEvaluator evaluator) {
69         assert sourceRoots != null && projectHelper != null && evaluator != null;
70         this.sourceRoots = sourceRoots;
71         sourceRoots.addPropertyChangeListener (this);
72         this.projectHelper = projectHelper;
73         this.evaluator = evaluator;
74         evaluator.addPropertyChangeListener (this);
75     }
76
77     public List JavaDoc<PathResourceImplementation> getResources() {
78         synchronized (this) {
79             if (this.resources != null) {
80                 return this.resources;
81             }
82         }
83         URL JavaDoc[] roots = sourceRoots.getRootURLs();
84         synchronized (this) {
85             if (this.resources == null) {
86                 List JavaDoc<PathResourceImplementation> result = new ArrayList JavaDoc<PathResourceImplementation>(roots.length);
87                 for (URL JavaDoc root : roots) {
88                     result.add(ClassPathSupport.createResource(root));
89                 }
90                 // adds java artifacts generated by wscompile and wsimport to resources to be available for code completion
91
if (projectHelper!=null) {
92                     try {
93                         String JavaDoc buildDir = this.evaluator.getProperty(PROP_BUILD_DIR);
94                         if (buildDir != null) {
95                             // generated/wsclient
96
File JavaDoc f = new File JavaDoc (this.projectHelper.resolveFile (buildDir),"generated/wsclient"); //NOI18N
97
URL JavaDoc url = f.toURI().toURL();
98                             if (!f.exists()) { //NOI18N
99
assert !url.toExternalForm().endsWith("/"); //NOI18N
100
url = new URL JavaDoc (url.toExternalForm()+'/'); //NOI18N
101
}
102                             result.add(ClassPathSupport.createResource(url));
103                             
104                             // generated/wsimport/client
105
f = new File JavaDoc (projectHelper.resolveFile(buildDir),"generated/wsimport/client"); //NOI18N
106
url = f.toURI().toURL();
107                             if (!f.exists()) { //NOI18N
108
assert !url.toExternalForm().endsWith("/"); //NOI18N
109
url = new URL JavaDoc (url.toExternalForm()+'/'); //NOI18N
110
}
111                             result.add(ClassPathSupport.createResource(url));
112                         }
113                     } catch (MalformedURLException JavaDoc ex) {
114                         ErrorManager.getDefault ().notify (ex);
115                     }
116                 }
117                 this.resources = Collections.unmodifiableList(result);
118             }
119             return this.resources;
120         }
121     }
122
123     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
124         support.addPropertyChangeListener (listener);
125     }
126
127     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
128         support.removePropertyChangeListener (listener);
129     }
130
131
132     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
133         if (SourceRoots.PROP_ROOTS.equals (evt.getPropertyName())) {
134             synchronized (this) {
135                 this.resources = null;
136             }
137             this.support.firePropertyChange (PROP_RESOURCES,null,null);
138         }
139         else if (this.evaluator != null && evt.getSource() == this.evaluator &&
140             (evt.getPropertyName() == null || PROP_BUILD_DIR.equals(evt.getPropertyName()))) {
141             synchronized (this) {
142                 this.resources = null;
143             }
144             this.support.firePropertyChange (PROP_RESOURCES,null,null);
145         }
146     }
147
148 }
149
Popular Tags