KickJava   Java API By Example, From Geeks To Geeks.

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


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.spi.project.support.ant.SourcesHelper;
40 import org.netbeans.spi.project.support.ant.AntProjectHelper;
41 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
42
43 public class JavaSources implements Sources, PropertyChangeListener JavaDoc {
44
45     private final AntProjectHelper helper;
46     private final PropertyEvaluator evaluator;
47     private final SourceRoots sourceRoots;
48     private final SourceRoots testRoots;
49     private Sources delegate;
50     private final List JavaDoc/*<ChangeListener>*/ listeners = new ArrayList JavaDoc();
51
52     JavaSources(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots, SourceRoots testRoots) {
53         this.helper = helper;
54         this.evaluator = evaluator;
55         this.sourceRoots = sourceRoots;
56         this.testRoots = testRoots;
57         this.sourceRoots.addPropertyChangeListener(this);
58         this.testRoots.addPropertyChangeListener(this);
59         initSources(); // have to register external build roots eagerly
60
}
61
62     public SourceGroup[] getSourceGroups(final String JavaDoc type) {
63         return (SourceGroup[]) ProjectManager.mutex().readAccess(new Mutex.Action() {
64             public Object JavaDoc run() {
65                 if (delegate == null) {
66                     delegate = initSources();
67                 }
68                 return delegate.getSourceGroups(type);
69             }
70         });
71     }
72
73     private Sources initSources() {
74         final SourcesHelper h = new SourcesHelper(helper, evaluator);
75         File JavaDoc projectDir = FileUtil.toFile(this.helper.getProjectDirectory());
76         String JavaDoc[] propNames = sourceRoots.getRootProperties();
77         String JavaDoc[] rootNames = sourceRoots.getRootNames();
78         for (int i = 0; i < propNames.length; i++) {
79             String JavaDoc displayName = rootNames[i];
80             String JavaDoc prop = "${" + propNames[i] + "}";
81             if (displayName.length() ==0) {
82                 //If the prop is src.dir use the default name
83
if ("src.dir".equals(propNames[i])) { //NOI18N
84
displayName = SourceRoots.DEFAULT_SOURCE_LABEL;
85                 }
86                 else {
87                     //If the name is not given, it should be either a relative path in the project dir
88
//or absolute path when the root is not under the project dir
89
File JavaDoc sourceRoot = helper.resolveFile(evaluator.evaluate(prop));
90                     if (sourceRoot != null) {
91                         String JavaDoc srPath = sourceRoot.getAbsolutePath();
92                         String JavaDoc pdPath = projectDir.getAbsolutePath() + File.separatorChar;
93                         if (srPath.startsWith(pdPath)) {
94                             displayName = srPath.substring(pdPath.length());
95                         }
96                         else {
97                             displayName = sourceRoot.getAbsolutePath();
98                         }
99                     }
100                     else {
101                         displayName = SourceRoots.DEFAULT_SOURCE_LABEL;
102                     }
103                 }
104             }
105             h.addPrincipalSourceRoot(prop, displayName, /*XXX*/null, null);
106             h.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, /*XXX*/null, null);
107         }
108         propNames = testRoots.getRootProperties();
109         rootNames = testRoots.getRootNames();
110         for (int i = 0; i < propNames.length; i++) {
111             String JavaDoc displayName = rootNames[i];
112             String JavaDoc prop = "${" + propNames[i] + "}";
113             if (displayName.length() ==0) {
114                 //If the prop is test.src.dir use the default name
115
if ("test.src.dir".equals(propNames[i])) { //NOI18N
116
displayName = SourceRoots.DEFAULT_TEST_LABEL;
117                 }
118                 else {
119                     //If the name is not given, it should be either a relative path in the project dir
120
//or absolute path when the root is not under the project dir
121
File JavaDoc sourceRoot = helper.resolveFile(evaluator.evaluate(prop));
122                     if (sourceRoot != null) {
123                         String JavaDoc srPath = sourceRoot.getAbsolutePath();
124                         String JavaDoc pdPath = projectDir.getAbsolutePath() + File.separatorChar;
125                         if (srPath.startsWith(pdPath)) {
126                             displayName = srPath.substring(pdPath.length());
127                         }
128                         else {
129                             displayName = sourceRoot.getAbsolutePath();
130                         }
131                     }
132                     else {
133                         displayName = SourceRoots.DEFAULT_TEST_LABEL;
134                     }
135                 }
136             }
137             h.addPrincipalSourceRoot(prop, displayName, /*XXX*/null, null);
138             h.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, /*XXX*/null, null);
139         }
140         // XXX add build dir too?
141
ProjectManager.mutex().postWriteRequest(new Runnable JavaDoc() {
142             public void run() {
143                 h.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
144             }
145         });
146         return h.createSources();
147     }
148
149     public void addChangeListener(ChangeListener JavaDoc changeListener) {
150         synchronized (listeners) {
151             listeners.add(changeListener);
152         }
153     }
154
155     public void removeChangeListener(ChangeListener JavaDoc changeListener) {
156         synchronized (listeners) {
157             listeners.remove(changeListener);
158         }
159     }
160
161     private void fireChange() {
162         ChangeListener JavaDoc[] _listeners;
163         synchronized (listeners) {
164             delegate = null;
165             if (listeners.isEmpty()) {
166                 return;
167             }
168             _listeners = (ChangeListener JavaDoc[])listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
169         }
170         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
171         for (int i = 0; i < _listeners.length; i++) {
172             _listeners[i].stateChanged(ev);
173         }
174     }
175
176     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
177         if (SourceRoots.PROP_ROOT_PROPERTIES.equals(evt.getPropertyName())) {
178             this.fireChange();
179         }
180     }
181
182 }
183
Popular Tags