1 19 20 package org.netbeans.modules.ant.freeform.ui; 21 22 import java.awt.Image ; 23 import java.util.Collections ; 24 import java.util.List ; 25 import java.util.StringTokenizer ; 26 import javax.swing.Action ; 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 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 target) { 64 Node[] kids = root.getChildren().getNodes(true); 66 for (Node kid : kids) { 67 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 if (target instanceof DataObject || target instanceof FileObject) { 76 DataObject d = kid.getLookup().lookup(DataObject.class); 77 if (d == null) { 78 continue; 79 } 80 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 relPath = FileUtil.getRelativePath(kidFO, targetFO); 87 List <String > path = Collections.list(NbCollections.checkedEnumerationByFilter(new StringTokenizer (relPath, "/"), String .class, true)); 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 getName() { 111 return ProjectUtils.getInformation(p).getName(); 112 } 113 114 public String getDisplayName() { 115 return ProjectUtils.getInformation(p).getDisplayName(); 116 } 117 118 public Image getIcon(int type) { 119 return Utilities.icon2Image(ProjectUtils.getInformation(p).getIcon()); 120 } 121 122 public Image getOpenedIcon(int type) { 123 return getIcon(type); 124 } 125 126 public Action [] 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 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)); } 149 150 } 151 152 153 } 154 | Popular Tags |