2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.ant.freeform.ui;
44
import java.awt.Image;
45
import java.util.Collections;
46
import java.util.List;
47
import java.util.StringTokenizer;
48
import javax.swing.Action;
49
import org.netbeans.api.project.ProjectUtils;
50
import org.netbeans.modules.ant.freeform.Actions;
51
import org.netbeans.modules.ant.freeform.FreeformProject;
52
import org.netbeans.modules.ant.freeform.spi.ProjectNature;
53
import org.netbeans.spi.project.ui.LogicalViewProvider;
54
import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
55
import org.netbeans.spi.project.ui.support.NodeFactorySupport;
56
import org.openide.filesystems.FileObject;
57
import org.openide.filesystems.FileUtil;
58
import org.openide.loaders.DataObject;
59
import org.openide.nodes.AbstractNode;
60
import org.openide.nodes.Node;
61
import org.openide.nodes.NodeNotFoundException;
62
import org.openide.nodes.NodeOp;
63
import org.openide.util.HelpCtx;
64
import org.openide.util.Lookup;
65
import org.openide.util.NbCollections;
66
import org.openide.util.Utilities;
67
import org.openide.util.lookup.Lookups;
70
* Logical view of a freeform project.
73
public final class View implements LogicalViewProvider {
75
private final FreeformProject project;
77
public View(FreeformProject project) {
78
this.project = project;
81
public Node createLogicalView() {
82
return new ProjectNodeWrapper(new RootNode(project));
85
public Node findPath(Node root, Object target) {
86
// Check each child node in turn.
87
Node[] kids = root.getChildren().getNodes(true);
88
for (Node kid : kids) {
90
for (ProjectNature nature : Lookup.getDefault().lookupAll(ProjectNature.class)) {
91
Node n = nature.findSourceFolderViewPath(project, kid, target);
96
// Otherwise, check children and look for <source-folder>/<source-file> matches.
97
if (target instanceof DataObject || target instanceof FileObject) {
98
DataObject d = kid.getLookup().lookup(DataObject.class);
102
// Copied from org.netbeans.spi.java.project.support.ui.TreeRootNode.PathFinder.findPath:
103
FileObject kidFO = d.getPrimaryFile();
104
FileObject targetFO = target instanceof DataObject ? ((DataObject) target).getPrimaryFile() : (FileObject) target;
105
if (kidFO == targetFO) {
107
} else if (FileUtil.isParentOf(kidFO, targetFO)) {
108
String relPath = FileUtil.getRelativePath(kidFO, targetFO);
109
List<String> path = Collections.list(NbCollections.checkedEnumerationByFilter(new StringTokenizer(relPath, "/"), String.class, true)); // NOI18N
110
// XXX see original code for justification
111
path.set(path.size() - 1, targetFO.getName());
113
return NodeOp.findPath(kid, Collections.enumeration(path));
114
} catch (NodeNotFoundException e) {
123
private static final class RootNode extends AbstractNode {
125
private final FreeformProject p;
127
public RootNode(FreeformProject p) {
128
super(NodeFactorySupport.createCompositeChildren(p, "Projects/org-netbeans-modules-ant-freeform/Nodes"), Lookups.singleton(p));
132
public String getName() {
133
return ProjectUtils.getInformation(p).getName();
136
public String getDisplayName() {
137
return ProjectUtils.getInformation(p).getDisplayName();
140
public Image getIcon(int type) {
141
return Utilities.icon2Image(ProjectUtils.getInformation(p).getIcon());
144
public Image getOpenedIcon(int type) {
145
return getIcon(type);
148
public Action[] getActions(boolean context) {
149
return Actions.createContextMenu(p);
152
public boolean canRename() {
156
public boolean canDestroy() {
160
public boolean canCut() {
164
public void setName(String name) {
165
DefaultProjectOperations.performDefaultRenameOperation(p, name);
168
public HelpCtx getHelpCtx() {
169
return new HelpCtx("freeform.node." + org.netbeans.modules.ant.freeform.Util.getMergedHelpIDFragments(p)); // NOI18N