KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > FreeformSources


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.ant.freeform;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.netbeans.api.project.FileOwnerQuery;
27 import org.netbeans.api.project.ProjectManager;
28 import org.netbeans.api.project.SourceGroup;
29 import org.netbeans.api.project.Sources;
30 import org.netbeans.modules.ant.freeform.spi.support.Util;
31 import org.netbeans.spi.project.support.ant.AntProjectEvent;
32 import org.netbeans.spi.project.support.ant.AntProjectListener;
33 import org.netbeans.spi.project.support.ant.SourcesHelper;
34 import org.openide.util.Mutex;
35 import org.w3c.dom.Element JavaDoc;
36
37 /**
38  * Handles source dir list for a freeform project.
39  * XXX will not correctly unregister released external source roots
40  * @author Jesse Glick
41  */

42 final class FreeformSources implements Sources, AntProjectListener {
43     
44     private final FreeformProject project;
45     
46     public FreeformSources(FreeformProject project) {
47         this.project = project;
48         project.helper().addAntProjectListener(this);
49         initSources(); // have to register external build roots eagerly
50
}
51     
52     private Sources delegate;
53     private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
54     
55     public SourceGroup[] getSourceGroups(final String JavaDoc type) {
56         return ProjectManager.mutex().readAccess(new Mutex.Action<SourceGroup[]>() {
57             public SourceGroup[] run() {
58                 if (delegate == null) {
59                     delegate = initSources();
60                 }
61                 return delegate.getSourceGroups(type);
62             }
63         });
64     }
65     
66     private Sources initSources() {
67         final SourcesHelper h = new SourcesHelper(project.helper(), project.evaluator());
68         Element JavaDoc genldata = project.getPrimaryConfigurationData();
69         Element JavaDoc foldersE = Util.findElement(genldata, "folders", FreeformProjectType.NS_GENERAL); // NOI18N
70
if (foldersE != null) {
71             for (Element JavaDoc folderE : Util.findSubElements(foldersE)) {
72                 Element JavaDoc locationE = Util.findElement(folderE, "location", FreeformProjectType.NS_GENERAL); // NOI18N
73
String JavaDoc location = Util.findText(locationE);
74                 if (folderE.getLocalName().equals("build-folder")) { // NOI18N
75
h.addNonSourceRoot(location);
76                 } else {
77                     assert folderE.getLocalName().equals("source-folder") : folderE;
78                     Element JavaDoc nameE = Util.findElement(folderE, "label", FreeformProjectType.NS_GENERAL); // NOI18N
79
String JavaDoc name = Util.findText(nameE);
80                     Element JavaDoc typeE = Util.findElement(folderE, "type", FreeformProjectType.NS_GENERAL); // NOI18N
81
String JavaDoc includes = null;
82                     Element JavaDoc includesE = Util.findElement(folderE, "includes", FreeformProjectType.NS_GENERAL); // NOI18N
83
if (includesE != null) {
84                         includes = Util.findText(includesE);
85                     }
86                     String JavaDoc excludes = null;
87                     Element JavaDoc excludesE = Util.findElement(folderE, "excludes", FreeformProjectType.NS_GENERAL); // NOI18N
88
if (excludesE != null) {
89                         excludes = Util.findText(excludesE);
90                     }
91                     if (typeE != null) {
92                         String JavaDoc type = Util.findText(typeE);
93                         h.addTypedSourceRoot(location, includes, excludes, type, name, null, null);
94                     } else {
95                         h.addPrincipalSourceRoot(location, includes, excludes, name, null, null);
96                     }
97                 }
98             }
99         }
100         ProjectManager.mutex().postWriteRequest(new Runnable JavaDoc() {
101             public void run() {
102                 h.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
103             }
104         });
105         return h.createSources();
106     }
107     
108     public void addChangeListener(ChangeListener JavaDoc changeListener) {
109         synchronized (listeners) {
110             listeners.add(changeListener);
111         }
112     }
113     
114     public void removeChangeListener(ChangeListener JavaDoc changeListener) {
115         synchronized (listeners) {
116             listeners.remove(changeListener);
117         }
118     }
119     
120     private void fireChange() {
121         ChangeListener JavaDoc[] _listeners;
122         synchronized (listeners) {
123             delegate = null;
124             if (listeners.isEmpty()) {
125                 return;
126             }
127             _listeners = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
128         }
129         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
130         for (ChangeListener JavaDoc l : _listeners) {
131             l.stateChanged(ev);
132         }
133     }
134     
135     public void configurationXmlChanged(AntProjectEvent ev) {
136         fireChange();
137     }
138     
139     public void propertiesChanged(AntProjectEvent ev) {
140         // ignore
141
}
142     
143 }
144
Popular Tags