KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > ui > View


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.ant.freeform.ui;
21
22 import java.awt.Image JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import org.netbeans.api.project.ProjectUtils;
28 import org.netbeans.modules.ant.freeform.Actions;
29 import org.netbeans.modules.ant.freeform.FreeformProject;
30 import org.netbeans.modules.ant.freeform.spi.ProjectNature;
31 import org.netbeans.spi.project.ui.LogicalViewProvider;
32 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
33 import org.netbeans.spi.project.ui.support.NodeFactorySupport;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.loaders.DataObject;
37 import org.openide.nodes.AbstractNode;
38 import org.openide.nodes.Node;
39 import org.openide.nodes.NodeNotFoundException;
40 import org.openide.nodes.NodeOp;
41 import org.openide.util.HelpCtx;
42 import org.openide.util.Lookup;
43 import org.openide.util.NbCollections;
44 import org.openide.util.Utilities;
45 import org.openide.util.lookup.Lookups;
46
47 /**
48  * Logical view of a freeform project.
49  * @author Jesse Glick
50  */

51 public final class View implements LogicalViewProvider {
52     
53     private final FreeformProject project;
54     
55     public View(FreeformProject project) {
56         this.project = project;
57     }
58     
59     public Node createLogicalView() {
60         return new ProjectNodeWrapper(new RootNode(project));
61     }
62     
63     public Node findPath(Node root, Object JavaDoc target) {
64         // Check each child node in turn.
65
Node[] kids = root.getChildren().getNodes(true);
66         for (Node kid : kids) {
67             // First ask natures.
68
for (ProjectNature nature : Lookup.getDefault().lookupAll(ProjectNature.class)) {
69                 Node n = nature.findSourceFolderViewPath(project, kid, target);
70                 if (n != null) {
71                     return n;
72                 }
73             }
74             // Otherwise, check children and look for <source-folder>/<source-file> matches.
75
if (target instanceof DataObject || target instanceof FileObject) {
76                 DataObject d = kid.getLookup().lookup(DataObject.class);
77                 if (d == null) {
78                     continue;
79                 }
80                 // Copied from org.netbeans.spi.java.project.support.ui.TreeRootNode.PathFinder.findPath:
81
FileObject kidFO = d.getPrimaryFile();
82                 FileObject targetFO = target instanceof DataObject ? ((DataObject) target).getPrimaryFile() : (FileObject) target;
83                 if (kidFO == targetFO) {
84                     return kid;
85                 } else if (FileUtil.isParentOf(kidFO, targetFO)) {
86                     String JavaDoc relPath = FileUtil.getRelativePath(kidFO, targetFO);
87                     List JavaDoc<String JavaDoc> path = Collections.list(NbCollections.checkedEnumerationByFilter(new StringTokenizer JavaDoc(relPath, "/"), String JavaDoc.class, true)); // NOI18N
88
// XXX see original code for justification
89
path.set(path.size() - 1, targetFO.getName());
90                     try {
91                         return NodeOp.findPath(kid, Collections.enumeration(path));
92                     } catch (NodeNotFoundException e) {
93                         return null;
94                     }
95                 }
96             }
97         }
98         return null;
99     }
100     
101     private static final class RootNode extends AbstractNode {
102         
103         private final FreeformProject p;
104         
105         public RootNode(FreeformProject p) {
106             super(NodeFactorySupport.createCompositeChildren(p, "Projects/org-netbeans-modules-ant-freeform/Nodes"), Lookups.singleton(p));
107             this.p = p;
108         }
109         
110         public String JavaDoc getName() {
111             return ProjectUtils.getInformation(p).getName();
112         }
113         
114         public String JavaDoc getDisplayName() {
115             return ProjectUtils.getInformation(p).getDisplayName();
116         }
117         
118         public Image JavaDoc getIcon(int type) {
119             return Utilities.icon2Image(ProjectUtils.getInformation(p).getIcon());
120         }
121         
122         public Image JavaDoc getOpenedIcon(int type) {
123             return getIcon(type);
124         }
125         
126         public Action JavaDoc[] getActions(boolean context) {
127             return Actions.createContextMenu(p);
128         }
129         
130         public boolean canRename() {
131             return true;
132         }
133         
134         public boolean canDestroy() {
135             return false;
136         }
137         
138         public boolean canCut() {
139             return false;
140         }
141         
142         public void setName(String JavaDoc name) {
143             DefaultProjectOperations.performDefaultRenameOperation(p, name);
144         }
145         
146         public HelpCtx getHelpCtx() {
147             return new HelpCtx("freeform.node." + org.netbeans.modules.ant.freeform.Util.getMergedHelpIDFragments(p)); // NOI18N
148
}
149         
150     }
151
152     
153 }
154
Popular Tags