KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > 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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.project.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.web.project.SourceRoots;
36 import org.netbeans.modules.web.project.UpdateHelper;
37 import org.netbeans.modules.web.project.WebProject;
38 import org.netbeans.modules.web.project.ui.customizer.CustomizerLibraries;
39 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
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         WebProject project = (WebProject)p.getLookup().lookup(WebProject.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 final SourceRoots testSources;
68         private final WebProject project;
69         private final ArrayList JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
70
71         private final PropertyEvaluator evaluator;
72         private final UpdateHelper helper;
73         private final ReferenceHelper resolver;
74         
75         LibrariesNodeList(WebProject proj) {
76             project = proj;
77             testSources = project.getTestSourceRoots();
78             WebLogicalViewProvider logView = (WebLogicalViewProvider)project.getLookup().lookup(WebLogicalViewProvider.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(
128                         NbBundle.getMessage(LibrariesNodeFactory.class,"CTL_LibrariesNode"),
129                         project,
130                         evaluator,
131                         helper,
132                         resolver,
133                         WebProjectProperties.JAVAC_CLASSPATH,
134                         new String JavaDoc[] {WebProjectProperties.BUILD_CLASSES_DIR},
135                         "platform.active", // NOI18N
136
WebProjectProperties.J2EE_SERVER_INSTANCE,
137                         new Action JavaDoc[] {
138                             LibrariesNode.createAddProjectAction(project, WebProjectProperties.JAVAC_CLASSPATH, WebProjectProperties.TAG_WEB_MODULE_LIBRARIES),
139                             LibrariesNode.createAddLibraryAction(project, WebProjectProperties.JAVAC_CLASSPATH, WebProjectProperties.TAG_WEB_MODULE_LIBRARIES),
140                             LibrariesNode.createAddFolderAction(project, WebProjectProperties.JAVAC_CLASSPATH, WebProjectProperties.TAG_WEB_MODULE_LIBRARIES),
141                             null,
142                             new SourceNodeFactory.PreselectPropertiesAction(project, "Libraries", CustomizerLibraries.COMPILE), // NOI18N
143
},
144                         WebProjectProperties.TAG_WEB_MODULE_LIBRARIES
145                     );
146             } else if (key == TEST_LIBRARIES) {
147                 return
148                     new LibrariesNode(
149                         NbBundle.getMessage(LibrariesNodeFactory.class,"CTL_TestLibrariesNode"),
150                         project,
151                         evaluator,
152                         helper,
153                         resolver,
154                         WebProjectProperties.JAVAC_TEST_CLASSPATH,
155                         new String JavaDoc[] {
156                             WebProjectProperties.BUILD_TEST_CLASSES_DIR,
157                             WebProjectProperties.JAVAC_CLASSPATH,
158                             WebProjectProperties.BUILD_CLASSES_DIR,
159                         },
160                         null,
161                         null,
162                         new Action JavaDoc[] {
163                             LibrariesNode.createAddProjectAction(project, WebProjectProperties.JAVAC_TEST_CLASSPATH, null),
164                             LibrariesNode.createAddLibraryAction(project, WebProjectProperties.JAVAC_TEST_CLASSPATH, null),
165                             LibrariesNode.createAddFolderAction(project, WebProjectProperties.JAVAC_TEST_CLASSPATH, null),
166                             null,
167                             new SourceNodeFactory.PreselectPropertiesAction(project, "Libraries", CustomizerLibraries.COMPILE_TESTS), // NOI18N
168
},
169                         null
170                     );
171             }
172             assert false: "No node for key: " + key;
173             return null;
174             
175         }
176
177         public void addNotify() {
178             testSources.addPropertyChangeListener(this);
179         }
180
181         public void removeNotify() {
182             testSources.removePropertyChangeListener(this);
183         }
184
185         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
186             // The caller holds ProjectManager.mutex() read lock
187
SwingUtilities.invokeLater(new Runnable JavaDoc() {
188                 public void run() {
189                     fireChange();
190                 }
191             });
192         }
193         
194     }
195     
196 }
197
Popular Tags