1 19 package org.netbeans.modules.xsl.ui; 20 21 import org.netbeans.modules.xml.core.lib.AbstractUtil; 22 import org.netbeans.api.project.Project; 23 import org.netbeans.api.project.Sources; 24 import org.netbeans.api.project.ProjectUtils; 25 import org.netbeans.api.project.SourceGroup; 26 import org.netbeans.api.project.ui.OpenProjects; 27 import org.openide.nodes.Node; 28 import org.openide.nodes.Children; 29 import org.openide.nodes.AbstractNode; 30 import org.openide.nodes.FilterNode; 31 import org.openide.filesystems.FileObject; 32 import org.openide.loaders.DataObject; 33 import org.openide.loaders.DataObjectNotFoundException; 34 import org.openide.util.lookup.Lookups; 35 import org.openide.ErrorManager; 36 37 import java.util.Comparator ; 38 import java.util.Arrays ; 39 import java.util.Collections ; 40 import java.awt.*; 41 42 47 class Util extends AbstractUtil { 48 49 50 public static final Util THIS = new Util(); 51 52 53 private Util () { 54 } 55 56 58 59 static Node projectView() { 60 61 Children.SortedArray kids = new Children.SortedArray(); 62 kids.setComparator(new Comparator () { 63 public int compare(Object o1, Object o2) { 64 return ((Node) o1).getDisplayName().compareToIgnoreCase(((Node) o2).getDisplayName()); 65 } 66 }); 67 68 Project[] projects = OpenProjects.getDefault().getOpenProjects(); 69 for (int pi = 0; pi<projects.length; pi++) { 70 Project project = projects[pi]; 71 Sources sources = ProjectUtils.getSources(project); 72 SourceGroup[] group =sources.getSourceGroups(Sources.TYPE_GENERIC); 73 Arrays.sort(group, new Comparator () { 74 public int compare(Object o1, Object o2) { 75 return ((SourceGroup) o1).getDisplayName().compareToIgnoreCase(((SourceGroup) o2).getDisplayName()); 76 } 77 }); 78 79 for (int i=0; i<group.length; i++) { 80 FileObject folder = group[i].getRootFolder(); 81 if (folder.isFolder()) { 82 kids.add(new Node[] {new FolderNode(folder, group[i])}); 83 if (icons == null) { 84 try { 85 DataObject dobj = DataObject.find(folder); 86 icons = dobj.getNodeDelegate(); 87 } catch (DataObjectNotFoundException e) { 88 ErrorManager err = ErrorManager.getDefault(); 89 err.notify(e); 90 } 91 } 92 } else { 93 try { 94 kids.add(new Node[] {new FilterNode(DataObject.find(folder).getNodeDelegate())}); 95 } catch (DataObjectNotFoundException e) { 96 ErrorManager err = ErrorManager.getDefault(); 97 err.notify(e); 98 } 99 } 100 } 101 } 102 final Node content = new AbstractNode(kids) { 103 public void setName(String name) { 104 super.setName(name); 105 super.setIconBase("org/netbeans/modules/xsl/resources/repository"); } 107 }; 108 109 content.setName(THIS.getString("projects")); 110 return content; 111 } 112 113 static Node icons = null; 115 116 117 private static class FolderNode extends AbstractNode { 118 119 private final FileObject fileObject; 120 private SourceGroup group; 121 122 public FolderNode(FileObject fileObject, SourceGroup root) { 123 super(new FolderContent(fileObject, root), Lookups.singleton(fileObject)); 124 this.fileObject = fileObject; 125 group = root; 126 } 127 128 public FolderNode(FileObject fileObject) { 129 super(new FolderContent(fileObject), Lookups.singleton(fileObject)); 130 this.fileObject = fileObject; 131 } 132 133 public String getDisplayName() { 134 if (group != null) { 135 return group.getDisplayName(); 136 } else { 137 return fileObject.getName(); 138 } 139 } 140 141 public Image getIcon(int type) { 142 143 151 if (icons != null) { 153 return icons.getIcon(type); 154 } else { 155 return super.getIcon(type); 156 } 157 } 158 159 public Image getOpenedIcon(int type) { 160 if (icons != null) { 162 return icons.getOpenedIcon(type); 163 } else { 164 return super.getOpenedIcon(type); 165 } 166 } 167 168 private static class FolderContent extends Children.Keys { 169 170 private final FileObject fileObject; 171 private final SourceGroup group; 172 173 public FolderContent(FileObject fileObject) { 174 this(fileObject, null); 175 } 176 177 public FolderContent(FileObject fileObject, SourceGroup group) { 178 this.fileObject = fileObject; 179 this.group = group; 180 } 181 182 protected void addNotify() { 183 FileObject[] fo = fileObject.getChildren(); 184 Arrays.sort(fo, new Comparator () { 185 public int compare(Object o1, Object o2) { 186 return ((FileObject) o1).getNameExt().compareToIgnoreCase(((FileObject) o2).getNameExt()); 187 } 188 }); 189 setKeys(Arrays.asList(fo)); 190 } 191 192 protected void removeNotify() { 193 setKeys(Collections.EMPTY_SET); 194 } 195 196 protected Node[] createNodes(Object key) { 197 FileObject fo = (FileObject) key; 198 if (group == null || group.contains(fo)) { 199 if (fo.isFolder()) { 200 return new Node[] {new FolderNode(fo)}; 201 } else { 202 try { 203 return new Node[] {new FilterNode(DataObject.find(fo).getNodeDelegate())}; 204 } catch (DataObjectNotFoundException e) { 205 ErrorManager err = ErrorManager.getDefault(); 206 err.notify(e); 207 } 208 } 209 } 210 return new Node[0]; 211 } 212 } 213 } 214 215 217 } 218 | Popular Tags |