KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > ui > LibrariesNodeFactory


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.ui;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.modules.java.j2seproject.J2SEProject;
36 import org.netbeans.modules.java.j2seproject.SourceRoots;
37 import org.netbeans.modules.java.j2seproject.UpdateHelper;
38 import org.netbeans.modules.java.j2seproject.ui.customizer.CustomizerLibraries;
39 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
40 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
41 import org.netbeans.spi.project.support.ant.ReferenceHelper;
42 import org.netbeans.spi.project.ui.support.NodeFactory;
43 import org.netbeans.spi.project.ui.support.NodeList;
44 import org.openide.nodes.Node;
45 import org.openide.util.NbBundle;
46
47 /**
48  *
49  * @author mkleint
50  */

51 public final class LibrariesNodeFactory implements NodeFactory {
52     
53     /** Creates a new instance of LibrariesNodeFactory */
54     public LibrariesNodeFactory() {
55     }
56
57     public NodeList createNodes(Project p) {
58         J2SEProject project = (J2SEProject)p.getLookup().lookup(J2SEProject.class);
59         assert project != null;
60         return new LibrariesNodeList(project);
61     }
62
63     private static class LibrariesNodeList implements NodeList<String JavaDoc>, PropertyChangeListener JavaDoc {
64         private static final String JavaDoc LIBRARIES = "Libs"; //NOI18N
65
private static final String JavaDoc TEST_LIBRARIES = "TestLibs"; //NOI18N
66

67         private SourceRoots testSources;
68         private J2SEProject project;
69         private ArrayList JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
70
71         private PropertyEvaluator evaluator;
72         private UpdateHelper helper;
73         private ReferenceHelper resolver;
74         
75         LibrariesNodeList(J2SEProject proj) {
76             project = proj;
77             testSources = project.getTestSourceRoots();
78             J2SELogicalViewProvider logView = (J2SELogicalViewProvider)project.getLookup().lookup(J2SELogicalViewProvider.class);
79             assert logView != null;
80             evaluator = logView.getEvaluator();
81             helper = logView.getUpdateHelper();
82             resolver = logView.getRefHelper();
83         }
84         
85         public List JavaDoc<String JavaDoc> keys() {
86             List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>();
87             result.add(LIBRARIES);
88             URL JavaDoc[] testRoots = testSources.getRootURLs();
89             boolean addTestSources = false;
90             for (int i = 0; i < testRoots.length; i++) {
91                 File JavaDoc f = new File JavaDoc(URI.create(testRoots[i].toExternalForm()));
92                 if (f.exists()) {
93                     addTestSources = true;
94                     break;
95                 }
96             }
97             if (addTestSources) {
98                 result.add(TEST_LIBRARIES);
99             }
100             return result;
101         }
102
103         public synchronized void addChangeListener(ChangeListener JavaDoc l) {
104             listeners.add(l);
105         }
106
107         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
108             listeners.remove(l);
109         }
110         
111         private void fireChange() {
112             ArrayList JavaDoc<ChangeListener JavaDoc> list = new ArrayList JavaDoc<ChangeListener JavaDoc>();
113             synchronized (this) {
114                 list.addAll(listeners);
115             }
116             Iterator JavaDoc<ChangeListener JavaDoc> it = list.iterator();
117             while (it.hasNext()) {
118                 ChangeListener JavaDoc elem = it.next();
119                 elem.stateChanged(new ChangeEvent JavaDoc( this ));
120             }
121         }
122
123         public Node node(String JavaDoc key) {
124             if (key == LIBRARIES) {
125                 //Libraries Node
126
return
127                     new LibrariesNode(NbBundle.getMessage(J2SELogicalViewProvider.class,"CTL_LibrariesNode"),
128                         project, evaluator, helper, resolver, J2SEProjectProperties.RUN_CLASSPATH,
129                         new String JavaDoc[] {J2SEProjectProperties.BUILD_CLASSES_DIR},
130                         "platform.active", // NOI18N
131
new Action JavaDoc[] {
132                             LibrariesNode.createAddProjectAction(project, J2SEProjectProperties.JAVAC_CLASSPATH),
133                             LibrariesNode.createAddLibraryAction(project, J2SEProjectProperties.JAVAC_CLASSPATH),
134                             LibrariesNode.createAddFolderAction(project, J2SEProjectProperties.JAVAC_CLASSPATH),
135                             null,
136                             new SourceNodeFactory.PreselectPropertiesAction(project, "Libraries", CustomizerLibraries.COMPILE), // NOI18N
137
}
138                     );
139             } else if (key == TEST_LIBRARIES) {
140                 return
141                     new LibrariesNode(NbBundle.getMessage(J2SELogicalViewProvider.class,"CTL_TestLibrariesNode"),
142                         project, evaluator, helper, resolver, J2SEProjectProperties.RUN_TEST_CLASSPATH,
143                         new String JavaDoc[] {
144                             J2SEProjectProperties.BUILD_TEST_CLASSES_DIR,
145                             J2SEProjectProperties.JAVAC_CLASSPATH,
146                             J2SEProjectProperties.BUILD_CLASSES_DIR,
147                         },
148                         null,
149                         new Action JavaDoc[] {
150                             LibrariesNode.createAddProjectAction(project, J2SEProjectProperties.JAVAC_TEST_CLASSPATH),
151                             LibrariesNode.createAddLibraryAction(project, J2SEProjectProperties.JAVAC_TEST_CLASSPATH),
152                             LibrariesNode.createAddFolderAction(project, J2SEProjectProperties.JAVAC_TEST_CLASSPATH),
153                             null,
154                             new SourceNodeFactory.PreselectPropertiesAction(project, "Libraries", CustomizerLibraries.COMPILE_TESTS), // NOI18N
155
}
156                     );
157             }
158             assert false: "No node for key: " + key;
159             return null;
160             
161         }
162
163         public void addNotify() {
164             testSources.addPropertyChangeListener(this);
165         }
166
167         public void removeNotify() {
168             testSources.removePropertyChangeListener(this);
169         }
170
171         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
172             // The caller holds ProjectManager.mutex() read lock
173
SwingUtilities.invokeLater(new Runnable JavaDoc() {
174                 public void run() {
175                     fireChange();
176                 }
177             });
178         }
179         
180     }
181     
182 }
183
Popular Tags