1 19 20 package org.netbeans.modules.apisupport.project.ui; 21 22 import java.awt.Image ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 import javax.swing.Action ; 28 import org.netbeans.api.project.ProjectUtils; 29 import org.netbeans.spi.java.project.support.ui.PackageView; 30 import org.netbeans.api.project.ProjectInformation; 31 import org.netbeans.api.project.SourceGroup; 32 import org.netbeans.api.project.Sources; 33 import org.netbeans.modules.apisupport.project.NbModuleProject; 34 import org.netbeans.spi.project.ui.LogicalViewProvider; 35 import org.netbeans.spi.project.ui.support.DefaultProjectOperations; 36 import org.netbeans.spi.project.ui.support.NodeFactorySupport; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.loaders.DataObject; 40 import org.openide.loaders.DataObjectNotFoundException; 41 import org.openide.nodes.Node; 42 import org.openide.util.NbBundle; 43 import org.openide.util.lookup.Lookups; 44 45 49 public final class ModuleLogicalView implements LogicalViewProvider { 50 51 52 private final NbModuleProject project; 53 54 public ModuleLogicalView(NbModuleProject project) { 55 this.project = project; 56 } 57 58 public Node createLogicalView() { 59 return new RootNode(project); 60 } 61 62 63 public Node findPath(Node root, Object target) { 64 if (root.getLookup().lookup(NbModuleProject.class) != project) { 65 return null; 67 } 68 69 Node[] rootChildren = root.getChildren().getNodes(true); 70 DataObject file; 71 72 if (target instanceof FileObject) { 73 try { 74 file = DataObject.find((FileObject) target); 75 } catch (DataObjectNotFoundException e) { 76 throw new AssertionError (e); 77 } 78 } else if (target instanceof DataObject) { 79 file = (DataObject) target; 80 } else { 81 return null; 83 } 84 85 for (int i = 0; i < rootChildren.length; i++) { 86 Node found = PackageView.findPath(rootChildren[i], target); 87 if (found != null) { 89 return found; 90 } 91 if (rootChildren[i].getName().equals(ImportantFilesNodeFactory.IMPORTANT_FILES_NAME)) { 93 Node[] ifChildren = rootChildren[i].getChildren().getNodes(true); 94 for (int j = 0; j < ifChildren.length; j++) { 95 if (ifChildren[j].getCookie(DataObject.class) == file) { 96 return ifChildren[j]; 97 } 98 } 99 } 100 } 101 102 return null; 103 } 104 105 private static final class RootNode extends AnnotatedNode { 106 107 private final NbModuleProject project; 108 109 public RootNode(NbModuleProject project) { 110 111 super(NodeFactorySupport.createCompositeChildren(project, "Projects/org-netbeans-modules-apisupport-project/Nodes"), 113 Lookups.fixed(new Object [] {project})); 114 this.project = project; 115 setForceAnnotation(true); 116 setIconBaseWithExtension(NbModuleProject.NB_PROJECT_ICON_PATH); 117 ProjectInformation pi = ProjectUtils.getInformation(project); 118 setDisplayName(pi.getDisplayName()); 119 setShortDescription(NbBundle.getMessage(ModuleLogicalView.class, "HINT_project_root_node", FileUtil.getFileDisplayName(project.getProjectDirectory()))); 120 setFiles(getProjectFiles()); 121 pi.addPropertyChangeListener(new PropertyChangeListener () { 122 public void propertyChange(PropertyChangeEvent evt) { 123 if (evt.getPropertyName() == ProjectInformation.PROP_DISPLAY_NAME) { 124 RootNode.this.setDisplayName((String ) evt.getNewValue()); 125 } else if (evt.getPropertyName() == ProjectInformation.PROP_NAME) { 126 RootNode.this.setName((String ) evt.getNewValue()); 127 } 128 } 129 }); 130 } 131 132 private Set getProjectFiles() { 133 Set roots = new HashSet (); 134 Sources sources = ProjectUtils.getSources(project); 135 136 SourceGroup[] groups = sources.getSourceGroups(Sources.TYPE_GENERIC); 138 for (int i = 0; i<groups.length; i++) { 139 SourceGroup group = groups[i]; 140 FileObject fo = group.getRootFolder(); 141 if (fo != null) { 142 FileObject [] files = fo.getChildren(); 143 for (int j = 0; j < files.length; j++) { 144 FileObject file = files[j]; 145 if (group.contains(file)) { 146 roots.add(file); 147 } 148 } 149 } 150 } 151 return roots; 152 } 153 154 public Action [] getActions(boolean ignore) { 155 return ModuleActions.getProjectActions(project); 156 } 157 158 public Image getIcon(int type) { 159 return annotateIcon(super.getIcon(type), type); 160 } 161 162 public Image getOpenedIcon(int type) { 163 return getIcon(type); } 165 166 public boolean canRename() { 167 return true; 168 } 169 170 public String getName() { 171 return ProjectUtils.getInformation(project).getDisplayName(); 172 } 173 174 public void setName(String name) { 175 DefaultProjectOperations.performDefaultRenameOperation(project, name); 176 } 177 178 } 179 } 180 | Popular Tags |