KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > AppClientSources


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.j2ee.clientproject;
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 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.util.Mutex;
32 import org.netbeans.api.project.Sources;
33 import org.netbeans.api.project.SourceGroup;
34 import org.netbeans.api.project.ProjectManager;
35 import org.netbeans.api.project.FileOwnerQuery;
36 import org.netbeans.api.java.project.JavaProjectConstants;
37 import org.netbeans.modules.j2ee.clientproject.ui.AppClientLogicalViewProvider;
38 import org.netbeans.spi.project.support.ant.SourcesHelper;
39 import org.netbeans.spi.project.support.ant.AntProjectHelper;
40 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
41
42
43 /**
44  * Implementation of {@link Sources} interface for J2SEProject.
45  */

46 public class AppClientSources implements Sources, PropertyChangeListener JavaDoc, ChangeListener JavaDoc {
47     
48     private static final String JavaDoc BUILD_DIR_PROP = "${" + AppClientProjectProperties.BUILD_DIR + "}"; //NOI18N
49
private static final String JavaDoc DIST_DIR_PROP = "${" + AppClientProjectProperties.DIST_DIR + "}"; //NOI18N
50

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

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

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

170         sourcesHelper.addNonSourceRoot(BUILD_DIR_PROP);
171         sourcesHelper.addNonSourceRoot(DIST_DIR_PROP);
172         
173         externalRootsRegistered = false;
174         ProjectManager.mutex().postWriteRequest(new Runnable JavaDoc() {
175             public void run() {
176                 if (!externalRootsRegistered) {
177                     sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
178                     externalRootsRegistered = true;
179                 }
180             }
181         });
182         return sourcesHelper.createSources();
183     }
184
185     public void addChangeListener(ChangeListener JavaDoc changeListener) {
186         synchronized (listeners) {
187             listeners.add(changeListener);
188         }
189     }
190
191     public void removeChangeListener(ChangeListener JavaDoc changeListener) {
192         synchronized (listeners) {
193             listeners.remove(changeListener);
194         }
195     }
196
197     private void fireChange() {
198         ChangeListener JavaDoc[] _listeners;
199         synchronized (this) {
200             if (delegate != null) {
201                 delegate.removeChangeListener(this);
202                 delegate = null;
203             }
204         }
205         synchronized (listeners) {
206             if (listeners.isEmpty()) {
207                 return;
208             }
209             _listeners = 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) ||
220             AppClientProjectProperties.BUILD_DIR.equals(propName) ||
221             AppClientProjectProperties.DIST_DIR.equals(propName)) {
222             this.fireChange();
223         }
224     }
225     
226     public void stateChanged (ChangeEvent JavaDoc event) {
227         this.fireChange();
228     }
229
230 }
231
Popular Tags