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.apisupport.project.ui;
043:
044: import java.awt.Image;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.util.Enumeration;
048: import java.util.HashSet;
049: import java.util.Set;
050: import javax.swing.Action;
051: import org.netbeans.api.project.FileOwnerQuery;
052: import org.netbeans.api.project.ProjectInformation;
053: import org.netbeans.api.project.ProjectUtils;
054: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
055: import org.netbeans.spi.project.ui.LogicalViewProvider;
056: import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
057: import org.netbeans.spi.project.ui.support.NodeFactorySupport;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileUtil;
060: import org.openide.loaders.DataObject;
061: import org.openide.loaders.DataObjectNotFoundException;
062: import org.openide.nodes.Node;
063: import org.openide.util.NbBundle;
064: import org.openide.util.Utilities;
065: import org.openide.util.WeakListeners;
066: import org.openide.util.lookup.Lookups;
067:
068: /**
069: * Provides a logical view of a NetBeans suite project.
070: *
071: * @author Jesse Glick, Martin Krauskopf
072: */
073: public final class SuiteLogicalView implements LogicalViewProvider {
074:
075: private final SuiteProject suite;
076:
077: public SuiteLogicalView(final SuiteProject suite) {
078: this .suite = suite;
079: }
080:
081: public Node createLogicalView() {
082: return new SuiteRootNode(suite);
083: }
084:
085: public Node findPath(Node root, Object target) {
086: if (root.getLookup().lookup(SuiteProject.class) != suite) {
087: // Not intended for this project. Should not normally happen anyway.
088: return null;
089: }
090:
091: DataObject file;
092: if (target instanceof FileObject) {
093: try {
094: file = DataObject.find((FileObject) target);
095: } catch (DataObjectNotFoundException e) {
096: return null; // OK
097: }
098: } else if (target instanceof DataObject) {
099: file = (DataObject) target;
100: } else {
101: // What is it?
102: return null;
103: }
104:
105: Node impFilesNode = root.getChildren().findChild(
106: "important.files"); // NOI18N
107: if (impFilesNode != null) {
108: Node[] impFiles = impFilesNode.getChildren().getNodes(true);
109: for (int i = 0; i < impFiles.length; i++) {
110: if (impFiles[i].getCookie(DataObject.class) == file) {
111: return impFiles[i];
112: }
113: }
114: }
115:
116: return null;
117: }
118:
119: /** Package private for unit test only. */
120: static final class SuiteRootNode extends AnnotatedNode implements
121: PropertyChangeListener {
122:
123: private static final Image ICON = Utilities.loadImage(
124: SuiteProject.SUITE_ICON_PATH, true);
125:
126: private final SuiteProject suite;
127: private final ProjectInformation info;
128:
129: SuiteRootNode(final SuiteProject suite) {
130: super (
131: NodeFactorySupport
132: .createCompositeChildren(suite,
133: "Projects/org-netbeans-modules-apisupport-project-suite/Nodes"),
134: Lookups.fixed(new Object[] { suite }));
135: this .suite = suite;
136: info = ProjectUtils.getInformation(suite);
137: info.addPropertyChangeListener(WeakListeners
138: .propertyChange(this , info));
139: setFiles(getProjectFiles());
140: }
141:
142: /** Package private for unit test only. */
143: Set<FileObject> getProjectFiles() {
144: Set<FileObject> files = new HashSet<FileObject>();
145: Enumeration en = suite.getProjectDirectory().getChildren(
146: false);
147: while (en.hasMoreElements()) {
148: FileObject child = (FileObject) en.nextElement();
149: if (FileOwnerQuery.getOwner(child) == suite) {
150: files.add(child);
151: }
152: }
153: return files;
154: }
155:
156: public String getName() {
157: return info.getDisplayName();
158: }
159:
160: public String getDisplayName() {
161: return info.getDisplayName();
162: }
163:
164: public String getShortDescription() {
165: return NbBundle.getMessage(SuiteLogicalView.class,
166: "HINT_suite_project_root_node", FileUtil
167: .getFileDisplayName(suite
168: .getProjectDirectory()));
169: }
170:
171: public Action[] getActions(boolean context) {
172: return SuiteActions.getProjectActions(suite);
173: }
174:
175: public Image getIcon(int type) {
176: return annotateIcon(ICON, type);
177: }
178:
179: public Image getOpenedIcon(int type) {
180: return getIcon(type); // the same in the meantime
181: }
182:
183: public void propertyChange(PropertyChangeEvent evt) {
184: if (evt.getPropertyName().equals(
185: ProjectInformation.PROP_NAME)) {
186: fireNameChange(null, getName());
187: } else if (evt.getPropertyName().equals(
188: ProjectInformation.PROP_DISPLAY_NAME)) {
189: fireDisplayNameChange(null, getDisplayName());
190: }
191: }
192:
193: public boolean canRename() {
194: return true;
195: }
196:
197: public void setName(String name) {
198: DefaultProjectOperations.performDefaultRenameOperation(
199: suite, name);
200: }
201:
202: }
203: }
|