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-2007 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.ant.freeform.ui;
043:
044: import java.awt.Image;
045: import java.util.Collections;
046: import java.util.List;
047: import java.util.StringTokenizer;
048: import javax.swing.Action;
049: import org.netbeans.api.project.ProjectUtils;
050: import org.netbeans.modules.ant.freeform.Actions;
051: import org.netbeans.modules.ant.freeform.FreeformProject;
052: import org.netbeans.modules.ant.freeform.spi.ProjectNature;
053: import org.netbeans.spi.project.ui.LogicalViewProvider;
054: import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
055: import org.netbeans.spi.project.ui.support.NodeFactorySupport;
056: import org.openide.filesystems.FileObject;
057: import org.openide.filesystems.FileUtil;
058: import org.openide.loaders.DataObject;
059: import org.openide.nodes.AbstractNode;
060: import org.openide.nodes.Node;
061: import org.openide.nodes.NodeNotFoundException;
062: import org.openide.nodes.NodeOp;
063: import org.openide.util.HelpCtx;
064: import org.openide.util.Lookup;
065: import org.openide.util.NbCollections;
066: import org.openide.util.Utilities;
067: import org.openide.util.lookup.Lookups;
068:
069: /**
070: * Logical view of a freeform project.
071: * @author Jesse Glick
072: */
073: public final class View implements LogicalViewProvider {
074:
075: private final FreeformProject project;
076:
077: public View(FreeformProject project) {
078: this .project = project;
079: }
080:
081: public Node createLogicalView() {
082: return new ProjectNodeWrapper(new RootNode(project));
083: }
084:
085: public Node findPath(Node root, Object target) {
086: // Check each child node in turn.
087: Node[] kids = root.getChildren().getNodes(true);
088: for (Node kid : kids) {
089: // First ask natures.
090: for (ProjectNature nature : Lookup.getDefault().lookupAll(
091: ProjectNature.class)) {
092: Node n = nature.findSourceFolderViewPath(project, kid,
093: target);
094: if (n != null) {
095: return n;
096: }
097: }
098: // Otherwise, check children and look for <source-folder>/<source-file> matches.
099: if (target instanceof DataObject
100: || target instanceof FileObject) {
101: DataObject d = kid.getLookup().lookup(DataObject.class);
102: if (d == null) {
103: continue;
104: }
105: // Copied from org.netbeans.spi.java.project.support.ui.TreeRootNode.PathFinder.findPath:
106: FileObject kidFO = d.getPrimaryFile();
107: FileObject targetFO = target instanceof DataObject ? ((DataObject) target)
108: .getPrimaryFile()
109: : (FileObject) target;
110: if (kidFO == targetFO) {
111: return kid;
112: } else if (FileUtil.isParentOf(kidFO, targetFO)) {
113: String relPath = FileUtil.getRelativePath(kidFO,
114: targetFO);
115: List<String> path = Collections.list(NbCollections
116: .checkedEnumerationByFilter(
117: new StringTokenizer(relPath, "/"),
118: String.class, true)); // NOI18N
119: // XXX see original code for justification
120: path.set(path.size() - 1, targetFO.getName());
121: try {
122: return NodeOp.findPath(kid, Collections
123: .enumeration(path));
124: } catch (NodeNotFoundException e) {
125: return null;
126: }
127: }
128: }
129: }
130: return null;
131: }
132:
133: private static final class RootNode extends AbstractNode {
134:
135: private final FreeformProject p;
136:
137: public RootNode(FreeformProject p) {
138: super (
139: NodeFactorySupport
140: .createCompositeChildren(p,
141: "Projects/org-netbeans-modules-ant-freeform/Nodes"),
142: Lookups.singleton(p));
143: this .p = p;
144: }
145:
146: public String getName() {
147: return ProjectUtils.getInformation(p).getName();
148: }
149:
150: public String getDisplayName() {
151: return ProjectUtils.getInformation(p).getDisplayName();
152: }
153:
154: public Image getIcon(int type) {
155: return Utilities.icon2Image(ProjectUtils.getInformation(p)
156: .getIcon());
157: }
158:
159: public Image getOpenedIcon(int type) {
160: return getIcon(type);
161: }
162:
163: public Action[] getActions(boolean context) {
164: return Actions.createContextMenu(p);
165: }
166:
167: public boolean canRename() {
168: return true;
169: }
170:
171: public boolean canDestroy() {
172: return false;
173: }
174:
175: public boolean canCut() {
176: return false;
177: }
178:
179: public void setName(String name) {
180: DefaultProjectOperations.performDefaultRenameOperation(p,
181: name);
182: }
183:
184: public HelpCtx getHelpCtx() {
185: return new HelpCtx("freeform.node."
186: + org.netbeans.modules.ant.freeform.Util
187: .getMergedHelpIDFragments(p)); // NOI18N
188: }
189:
190: }
191:
192: }
|