001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.j2seproject.ui;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.io.File;
047: import java.net.URI;
048: import java.net.URL;
049: import java.util.ArrayList;
050: import java.util.List;
051: import javax.swing.Action;
052: import javax.swing.SwingUtilities;
053: import javax.swing.event.ChangeListener;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.modules.java.api.common.SourceRoots;
056: import org.netbeans.modules.java.api.common.ant.UpdateHelper;
057: import org.netbeans.modules.java.j2seproject.J2SEProject;
058: import org.netbeans.modules.java.j2seproject.ui.customizer.CustomizerLibraries;
059: import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
060: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
061: import org.netbeans.spi.project.support.ant.ReferenceHelper;
062: import org.netbeans.spi.project.ui.support.NodeFactory;
063: import org.netbeans.spi.project.ui.support.NodeList;
064: import org.openide.nodes.Node;
065: import org.openide.util.ChangeSupport;
066: import org.openide.util.NbBundle;
067:
068: /**
069: *
070: * @author mkleint
071: */
072: public final class LibrariesNodeFactory implements NodeFactory {
073:
074: /** Creates a new instance of LibrariesNodeFactory */
075: public LibrariesNodeFactory() {
076: }
077:
078: public NodeList createNodes(Project p) {
079: J2SEProject project = (J2SEProject) p.getLookup().lookup(
080: J2SEProject.class);
081: assert project != null;
082: return new LibrariesNodeList(project);
083: }
084:
085: private static class LibrariesNodeList implements NodeList<String>,
086: PropertyChangeListener {
087: private static final String LIBRARIES = "Libs"; //NOI18N
088: private static final String TEST_LIBRARIES = "TestLibs"; //NOI18N
089:
090: private SourceRoots testSources;
091: private J2SEProject project;
092: private final ChangeSupport changeSupport = new ChangeSupport(
093: this );
094:
095: private PropertyEvaluator evaluator;
096: private UpdateHelper helper;
097: private ReferenceHelper resolver;
098:
099: LibrariesNodeList(J2SEProject proj) {
100: project = proj;
101: testSources = project.getTestSourceRoots();
102: J2SELogicalViewProvider logView = (J2SELogicalViewProvider) project
103: .getLookup().lookup(J2SELogicalViewProvider.class);
104: assert logView != null;
105: evaluator = logView.getEvaluator();
106: helper = logView.getUpdateHelper();
107: resolver = logView.getRefHelper();
108: }
109:
110: public List<String> keys() {
111: List<String> result = new ArrayList<String>();
112: result.add(LIBRARIES);
113: URL[] testRoots = testSources.getRootURLs();
114: boolean addTestSources = false;
115: for (int i = 0; i < testRoots.length; i++) {
116: File f = new File(URI.create(testRoots[i]
117: .toExternalForm()));
118: if (f.exists()) {
119: addTestSources = true;
120: break;
121: }
122: }
123: if (addTestSources) {
124: result.add(TEST_LIBRARIES);
125: }
126: return result;
127: }
128:
129: public void addChangeListener(ChangeListener l) {
130: changeSupport.addChangeListener(l);
131: }
132:
133: public void removeChangeListener(ChangeListener l) {
134: changeSupport.removeChangeListener(l);
135: }
136:
137: public Node node(String key) {
138: if (key == LIBRARIES) {
139: //Libraries Node
140: return new LibrariesNode(
141: NbBundle.getMessage(
142: J2SELogicalViewProvider.class,
143: "CTL_LibrariesNode"),
144: project,
145: evaluator,
146: helper,
147: resolver,
148: J2SEProjectProperties.RUN_CLASSPATH,
149: new String[] { J2SEProjectProperties.BUILD_CLASSES_DIR },
150: "platform.active", // NOI18N
151: new Action[] {
152: LibrariesNode.createAddProjectAction(
153: project, true),
154: LibrariesNode.createAddLibraryAction(
155: project, true),
156: LibrariesNode.createAddFolderAction(
157: project, true),
158: null,
159: new SourceNodeFactory.PreselectPropertiesAction(
160: project, "Libraries",
161: CustomizerLibraries.COMPILE), // NOI18N
162: });
163: } else if (key == TEST_LIBRARIES) {
164: return new LibrariesNode(
165: NbBundle.getMessage(
166: J2SELogicalViewProvider.class,
167: "CTL_TestLibrariesNode"),
168: project,
169: evaluator,
170: helper,
171: resolver,
172: J2SEProjectProperties.RUN_TEST_CLASSPATH,
173: new String[] {
174: J2SEProjectProperties.BUILD_TEST_CLASSES_DIR,
175: J2SEProjectProperties.JAVAC_CLASSPATH,
176: J2SEProjectProperties.BUILD_CLASSES_DIR, },
177: null,
178: new Action[] {
179: LibrariesNode.createAddProjectAction(
180: project, false),
181: LibrariesNode.createAddLibraryAction(
182: project, false),
183: LibrariesNode.createAddFolderAction(
184: project, false),
185: null,
186: new SourceNodeFactory.PreselectPropertiesAction(
187: project,
188: "Libraries",
189: CustomizerLibraries.COMPILE_TESTS), // NOI18N
190: });
191: }
192: assert false : "No node for key: " + key;
193: return null;
194:
195: }
196:
197: public void addNotify() {
198: testSources.addPropertyChangeListener(this );
199: }
200:
201: public void removeNotify() {
202: testSources.removePropertyChangeListener(this );
203: }
204:
205: public void propertyChange(PropertyChangeEvent evt) {
206: // The caller holds ProjectManager.mutex() read lock
207: SwingUtilities.invokeLater(new Runnable() {
208: public void run() {
209: changeSupport.fireChange();
210: }
211: });
212: }
213:
214: }
215:
216: }
|