KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SESources


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

42 public class J2SESources implements Sources, PropertyChangeListener JavaDoc, ChangeListener JavaDoc {
43     
44     private static final String JavaDoc BUILD_DIR_PROP = "${" + J2SEProjectProperties.BUILD_DIR + "}"; //NOI18N
45
private static final String JavaDoc DIST_DIR_PROP = "${" + J2SEProjectProperties.DIST_DIR + "}"; //NOI18N
46

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

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

78     public SourceGroup[] getSourceGroups(final String JavaDoc type) {
79         return ProjectManager.mutex().readAccess(new Mutex.Action<SourceGroup[]>() {
80             public SourceGroup[] run() {
81                 Sources _delegate;
82                 synchronized (J2SESources.this) {
83                     if (delegate == null) {
84                         delegate = initSources();
85                         delegate.addChangeListener(J2SESources.this);
86                     }
87                     _delegate = delegate;
88                 }
89                 return _delegate.getSourceGroups(type);
90             }
91         });
92     }
93
94     private Sources initSources() {
95         this.sourcesHelper = new SourcesHelper(helper, evaluator); //Safe to pass APH
96
register(sourceRoots);
97         register(testRoots);
98         this.sourcesHelper.addNonSourceRoot (BUILD_DIR_PROP);
99         this.sourcesHelper.addNonSourceRoot(DIST_DIR_PROP);
100         externalRootsRegistered = false;
101         ProjectManager.mutex().postWriteRequest(new Runnable JavaDoc() {
102             public void run() {
103                 if (!externalRootsRegistered) {
104                     sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
105                     externalRootsRegistered = true;
106                 }
107             }
108         });
109         return this.sourcesHelper.createSources();
110     }
111
112     private void register(SourceRoots roots) {
113         String JavaDoc[] propNames = roots.getRootProperties();
114         String JavaDoc[] rootNames = roots.getRootNames();
115         for (int i = 0; i < propNames.length; i++) {
116             String JavaDoc prop = propNames[i];
117             String JavaDoc displayName = roots.getRootDisplayName(rootNames[i], prop);
118             String JavaDoc loc = "${" + prop + "}"; // NOI18N
119
String JavaDoc includes = "${" + J2SEProjectProperties.INCLUDES + "}"; // NOI18N
120
String JavaDoc excludes = "${" + J2SEProjectProperties.EXCLUDES + "}"; // NOI18N
121
sourcesHelper.addPrincipalSourceRoot(loc, includes, excludes, displayName, null, null); // NOI18N
122
sourcesHelper.addTypedSourceRoot(loc, includes, excludes, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); // NOI18N
123
}
124     }
125
126     public void addChangeListener(ChangeListener JavaDoc changeListener) {
127         synchronized (listeners) {
128             listeners.add(changeListener);
129         }
130     }
131
132     public void removeChangeListener(ChangeListener JavaDoc changeListener) {
133         synchronized (listeners) {
134             listeners.remove(changeListener);
135         }
136     }
137
138     private void fireChange() {
139         ChangeListener JavaDoc[] _listeners;
140         synchronized (this) {
141             if (delegate != null) {
142                 delegate.removeChangeListener(this);
143                 delegate = null;
144             }
145         }
146         synchronized (listeners) {
147             if (listeners.isEmpty()) {
148                 return;
149             }
150             _listeners = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
151         }
152         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
153         for (ChangeListener JavaDoc l : _listeners) {
154             l.stateChanged(ev);
155         }
156     }
157
158     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
159         String JavaDoc propName = evt.getPropertyName();
160         if (SourceRoots.PROP_ROOT_PROPERTIES.equals(propName) ||
161             J2SEProjectProperties.BUILD_DIR.equals(propName) ||
162             J2SEProjectProperties.DIST_DIR.equals(propName)) {
163             this.fireChange();
164         }
165     }
166     
167     public void stateChanged (ChangeEvent JavaDoc event) {
168         this.fireChange();
169     }
170
171 }
172
Popular Tags