KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > WebSources


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;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.io.File JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30
31 import org.openide.filesystems.FileUtil;
32 import org.openide.util.Mutex;
33
34 import org.netbeans.api.project.Sources;
35 import org.netbeans.api.project.SourceGroup;
36 import org.netbeans.api.project.ProjectManager;
37 import org.netbeans.api.project.FileOwnerQuery;
38 import org.netbeans.api.java.project.JavaProjectConstants;
39 import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
40 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
41 import org.netbeans.spi.project.support.ant.SourcesHelper;
42 import org.netbeans.spi.project.support.ant.AntProjectHelper;
43 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
44
45 /**
46  * Implementation of {@link Sources} interface for WebProject.
47  */

48 public class WebSources implements Sources, PropertyChangeListener JavaDoc, ChangeListener JavaDoc {
49
50     private static final String JavaDoc BUILD_DIR_PROP = "${" + WebProjectProperties.BUILD_DIR + "}"; //NOI18N
51
private static final String JavaDoc DIST_DIR_PROP = "${" + WebProjectProperties.DIST_DIR + "}"; //NOI18N
52

53     private final AntProjectHelper helper;
54     private final PropertyEvaluator evaluator;
55     private final SourceRoots sourceRoots;
56     private final SourceRoots testRoots;
57     private Sources delegate;
58     /**
59      * Flag to forbid multiple invocation of {@link SourcesHelper#registerExternalRoots}
60      **/

61     private boolean externalRootsRegistered;
62     private final List JavaDoc/*<ChangeListener>*/ listeners = new ArrayList JavaDoc();
63     private SourcesHelper sourcesHelper;
64
65     WebSources(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots, SourceRoots testRoots) {
66         this.helper = helper;
67         this.evaluator = evaluator;
68         this.sourceRoots = sourceRoots;
69         this.testRoots = testRoots;
70         this.sourceRoots.addPropertyChangeListener(this);
71         this.testRoots.addPropertyChangeListener(this);
72         this.evaluator.addPropertyChangeListener(this);
73         initSources(); // have to register external build roots eagerly
74
}
75
76     /**
77      * Returns an array of SourceGroup of given type. It delegates to {@link SourcesHelper}.
78      * This method firstly acquire the {@link ProjectManager#mutex} in read mode then it enters
79      * into the synchronized block to ensure that just one instance of the {@link SourcesHelper}
80      * is created. These instance is cleared also in the synchronized block by the
81      * {@link WebSources#fireChange} method.
82      */

83     public SourceGroup[] getSourceGroups(final String JavaDoc type) {
84         return (SourceGroup[]) ProjectManager.mutex().readAccess(new Mutex.Action() {
85             public Object JavaDoc run() {
86                 Sources _delegate;
87                 synchronized (WebSources.this) {
88                     if (delegate == null) {
89                         delegate = initSources();
90                         delegate.addChangeListener(WebSources.this);
91                     }
92                     _delegate = delegate;
93                 }
94                 return _delegate.getSourceGroups(type);
95             }
96         });
97     }
98
99     private Sources initSources() {
100         sourcesHelper = new SourcesHelper(helper, evaluator);
101         File JavaDoc projectDir = FileUtil.toFile(this.helper.getProjectDirectory());
102         String JavaDoc[] propNames = sourceRoots.getRootProperties();
103         String JavaDoc[] rootNames = sourceRoots.getRootNames();
104         for (int i = 0; i < propNames.length; i++) {
105             String JavaDoc displayName = rootNames[i];
106             String JavaDoc prop = "${" + propNames[i] + "}";
107             if (displayName.length() ==0) {
108                 //If the prop is src.dir use the default name
109
if ("src.dir".equals(propNames[i])) { //NOI18N
110
displayName = SourceRoots.DEFAULT_SOURCE_LABEL;
111                 }
112                 else {
113                     //If the name is not given, it should be either a relative path in the project dir
114
//or absolute path when the root is not under the project dir
115
File JavaDoc sourceRoot = helper.resolveFile(evaluator.evaluate(prop));
116                     if (sourceRoot != null) {
117                         String JavaDoc srPath = sourceRoot.getAbsolutePath();
118                         String JavaDoc pdPath = projectDir.getAbsolutePath() + File.separatorChar;
119                         if (srPath.startsWith(pdPath)) {
120                             displayName = srPath.substring(pdPath.length());
121                         }
122                         else {
123                             displayName = sourceRoot.getAbsolutePath();
124                         }
125                     }
126                     else {
127                         displayName = SourceRoots.DEFAULT_SOURCE_LABEL;
128                     }
129                 }
130             }
131             sourcesHelper.addPrincipalSourceRoot(prop, displayName, /*XXX*/null, null);
132             sourcesHelper.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, /*XXX*/null, null);
133         }
134         propNames = testRoots.getRootProperties();
135         rootNames = testRoots.getRootNames();
136         for (int i = 0; i < propNames.length; i++) {
137             String JavaDoc displayName = rootNames[i];
138             String JavaDoc prop = "${" + propNames[i] + "}";
139             if (displayName.length() ==0) {
140                 //If the prop is test.src.dir use the default name
141
if ("test.src.dir".equals(propNames[i])) { //NOI18N
142
displayName = SourceRoots.DEFAULT_TEST_LABEL;
143                 }
144                 else {
145                     //If the name is not given, it should be either a relative path in the project dir
146
//or absolute path when the root is not under the project dir
147
File JavaDoc sourceRoot = helper.resolveFile(evaluator.evaluate(prop));
148                     if (sourceRoot != null) {
149                         String JavaDoc srPath = sourceRoot.getAbsolutePath();
150                         String JavaDoc pdPath = projectDir.getAbsolutePath() + File.separatorChar;
151                         if (srPath.startsWith(pdPath)) {
152                             displayName = srPath.substring(pdPath.length());
153                         }
154                         else {
155                             displayName = sourceRoot.getAbsolutePath();
156                         }
157                     }
158                     else {
159                         displayName = SourceRoots.DEFAULT_TEST_LABEL;
160                     }
161                 }
162             }
163             sourcesHelper.addPrincipalSourceRoot(prop, displayName, /*XXX*/null, null);
164             sourcesHelper.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, /*XXX*/null, null);
165         }
166         
167         //Web Pages
168
String JavaDoc webModuleLabel = org.openide.util.NbBundle.getMessage(WebSources.class, "LBL_Node_WebModule"); //NOI18N
169
String JavaDoc webPagesLabel = org.openide.util.NbBundle.getMessage(WebSources.class, "LBL_Node_DocBase"); //NOI18N
170
sourcesHelper.addPrincipalSourceRoot("${"+ WebProjectProperties.SOURCE_ROOT+"}", webModuleLabel, /*XXX*/null, null); //NOI18N
171
sourcesHelper.addPrincipalSourceRoot("${"+ WebProjectProperties.WEB_DOCBASE_DIR+"}", webPagesLabel, /*XXX*/null, null); //NOI18N
172
sourcesHelper.addTypedSourceRoot("${"+ WebProjectProperties.WEB_DOCBASE_DIR+"}", WebProjectConstants.TYPE_DOC_ROOT, webPagesLabel, /*XXX*/null, null); //NOI18N
173
sourcesHelper.addTypedSourceRoot("${"+ WebProjectProperties.WEB_DOCBASE_DIR+"}/WEB-INF", WebProjectConstants.TYPE_WEB_INF, /*XXX I18N*/ "WEB-INF", /*XXX*/null, null); //NOI18N
174

175         sourcesHelper.addNonSourceRoot(BUILD_DIR_PROP);
176         sourcesHelper.addNonSourceRoot(DIST_DIR_PROP);
177         
178         externalRootsRegistered = false;
179         ProjectManager.mutex().postWriteRequest(new Runnable JavaDoc() {
180             public void run() {
181                 if (!externalRootsRegistered) {
182                     sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
183                     externalRootsRegistered = true;
184                 }
185             }
186         });
187         return sourcesHelper.createSources();
188     }
189
190     public void addChangeListener(ChangeListener JavaDoc changeListener) {
191         synchronized (listeners) {
192             listeners.add(changeListener);
193         }
194     }
195
196     public void removeChangeListener(ChangeListener JavaDoc changeListener) {
197         synchronized (listeners) {
198             listeners.remove(changeListener);
199         }
200     }
201
202     private void fireChange() {
203         ChangeListener JavaDoc[] _listeners;
204         synchronized (listeners) {
205             delegate = null;
206             if (listeners.isEmpty()) {
207                 return;
208             }
209             _listeners = (ChangeListener JavaDoc[])listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
210         }
211         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
212         for (int i = 0; i < _listeners.length; i++) {
213             _listeners[i].stateChanged(ev);
214         }
215     }
216
217     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
218         String JavaDoc propName = evt.getPropertyName();
219         if (SourceRoots.PROP_ROOT_PROPERTIES.equals(propName) || WebProjectProperties.BUILD_DIR.equals(propName) || WebProjectProperties.DIST_DIR.equals(propName))
220             this.fireChange();
221     }
222
223     public void stateChanged (ChangeEvent JavaDoc event) {
224         this.fireChange();
225     }
226
227 }
228
Popular Tags