KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > 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.j2ee.clientproject.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.spi.java.classpath.ClassPathImplementation;
31 import org.netbeans.spi.java.classpath.PathResourceImplementation;
32 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
33 import org.netbeans.modules.j2ee.clientproject.SourceRoots;
34 import org.netbeans.spi.project.support.ant.AntProjectHelper;
35 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
36 import org.openide.ErrorManager;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileStateInvalidException;
39 import org.openide.util.Utilities;
40
41
42 /**
43  * Implementation of a single classpath that is derived from one Ant property.
44  */

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

49     private final PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
50     private List JavaDoc resources;
51     private final SourceRoots sourceRoots;
52     private final AntProjectHelper projectHelper;
53     private final PropertyEvaluator evaluator;
54     
55     /**
56      * Construct the implementation.
57      * @param sourceRoots used to get the roots information and events
58      */

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

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