1 19 20 package org.netbeans.modules.tasklist.docscan; 21 22 import java.awt.Image ; 23 import java.util.Arrays ; 24 import java.util.Comparator ; 25 import java.util.Collections ; 26 import javax.swing.Icon ; 27 import org.netbeans.api.project.ui.OpenProjects; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.api.project.ProjectUtils; 30 import org.netbeans.api.project.SourceGroup; 31 import org.netbeans.api.project.Sources; 32 import org.openide.nodes.AbstractNode; 33 import org.openide.nodes.Children; 34 import org.openide.nodes.Node; 35 import org.openide.filesystems.FileObject; 36 import org.openide.util.Utilities; 37 import org.openide.util.lookup.Lookups; 38 import org.openide.loaders.DataObject; 39 import org.openide.loaders.DataObjectNotFoundException; 40 41 46 class Choosers { 47 public static Node icons = null; 49 50 53 public static Node projectView() { 54 55 Children.SortedArray kids = new Children.SortedArray(); 56 kids.setComparator(new Comparator () { 57 public int compare(Object o1, Object o2) { 58 return ((Node) o1).getDisplayName().compareToIgnoreCase(((Node) o2).getDisplayName()); 59 } 60 }); 61 62 Project[] projects = OpenProjects.getDefault().getOpenProjects(); 63 for (int pi = 0; pi < projects.length; pi++) { 64 Project project = projects[pi]; 65 Sources sources = ProjectUtils.getSources(project); 66 SourceGroup[] group = sources.getSourceGroups(Sources.TYPE_GENERIC); 67 68 if (group.length == 0) continue; 69 70 74 if (group.length > 1) { 75 kids.add(new Node[] {new ProjectNode(project)}); 76 } else { 77 FileObject folder = group[0].getRootFolder(); 78 if (folder.isFolder() == false) continue; 79 kids.add(new Node[]{new FolderNode(folder, group[0])}); 80 prepareFolderIcons(folder); 81 } 82 } 83 84 final Node content = new AbstractNode(kids) { 85 public void setName(String name) { 86 super.setName(name); 87 super.setIconBase("org/netbeans/modules/tasklist/docscan/repository"); } 89 }; 90 91 content.setName(Util.getString("projects")); 92 return content; 93 } 94 95 96 private static void prepareFolderIcons(FileObject fo) { 97 if (icons == null) { 98 try { 99 DataObject dobj = DataObject.find(fo); 100 icons = dobj.getNodeDelegate(); 101 } catch (DataObjectNotFoundException e) { 102 } 104 } 105 } 106 107 110 public static class ProjectNode extends AbstractNode { 111 112 private final Project project; 113 114 public ProjectNode(Project project) { 115 this(new Children.SortedArray(), project); 116 } 117 118 private ProjectNode(Children.SortedArray children, Project project) { 119 super(children); 120 this.project = project; 121 122 children.setComparator(new Comparator () { 123 public int compare(Object o1, Object o2) { 124 return ((Node) o1).getDisplayName().compareToIgnoreCase(((Node) o2).getDisplayName()); 125 } 126 }); 127 128 Sources sources = ProjectUtils.getSources(project); 129 SourceGroup[] group = sources.getSourceGroups(Sources.TYPE_GENERIC); 130 Arrays.sort(group, new Comparator () { 131 public int compare(Object o1, Object o2) { 132 return ((SourceGroup) o1).getDisplayName().compareToIgnoreCase(((SourceGroup) o2).getDisplayName()); 133 } 134 }); 135 136 for (int i = 0; i < group.length; i++) { 137 FileObject folder = group[i].getRootFolder(); 138 if (folder.isFolder() == false) continue; 139 children.add(new Node[]{new FolderNode(folder, group[i])}); 140 prepareFolderIcons(folder); 141 } 142 } 143 144 public String getDisplayName() { 145 return ProjectUtils.getInformation(project).getDisplayName(); 146 } 147 148 public Image getIcon(int type) { 149 return Utilities.icon2Image(ProjectUtils.getInformation(project).getIcon()); 150 } 151 152 public Image getOpenedIcon(int type) { 153 return getIcon(type); 154 } 155 156 } 157 158 161 public static class FolderNode extends AbstractNode { 162 163 private final FileObject fileObject; 164 private SourceGroup group; 165 166 public FolderNode(FileObject fileObject, SourceGroup root) { 167 super(new FolderContent(fileObject, root), Lookups.singleton(fileObject)); 168 this.fileObject = fileObject; 169 group = root; 170 } 171 172 public FolderNode(FileObject fileObject) { 173 super(new FolderContent(fileObject), Lookups.singleton(fileObject)); 174 this.fileObject = fileObject; 175 } 176 177 public String getDisplayName() { 178 if (group != null) { 179 return group.getDisplayName(); 180 } else { 181 return fileObject.getName(); 182 } 183 } 184 185 public Image getIcon(int type) { 186 187 if (group != null) { 188 Icon icon = group.getIcon(false); 189 if (icon != null) { 190 return Utilities.icon2Image(icon); 191 } 192 } 193 194 if (icons != null) { 196 return icons.getIcon(type); 197 } else { 198 return super.getIcon(type); 199 } 200 } 201 202 public Image getOpenedIcon(int type) { 203 204 if (group != null) { 205 Icon icon = group.getIcon(true); 206 if (icon != null) { 207 return Utilities.icon2Image(icon); 208 } 209 } 210 211 if (icons != null) { 213 return icons.getOpenedIcon(type); 214 } else { 215 return super.getOpenedIcon(type); 216 } 217 } 218 219 private static class FolderContent extends Children.Keys { 220 221 private final FileObject fileObject; 222 private final SourceGroup group; 223 224 public FolderContent(FileObject fileObject) { 225 this(fileObject, null); 226 } 227 228 public FolderContent(FileObject fileObject, SourceGroup group) { 229 this.fileObject = fileObject; 230 this.group = group; 231 } 232 233 protected void addNotify() { 234 FileObject[] fo = fileObject.getChildren(); 235 Arrays.sort(fo, new Comparator () { 236 public int compare(Object o1, Object o2) { 237 return ((FileObject) o1).getNameExt().compareToIgnoreCase(((FileObject) o2).getNameExt()); 238 } 239 }); 240 setKeys(Arrays.asList(fo)); 241 } 242 243 protected void removeNotify() { 244 setKeys(Collections.EMPTY_SET); 245 } 246 247 protected Node[] createNodes(Object key) { 248 FileObject fo = (FileObject) key; 249 if (fo.isFolder() && (group == null || group.contains(fo))) { 250 return new Node[]{new FolderNode(fo)}; 251 } else { 252 return new Node[0]; 253 } 254 } 255 } 256 } 257 } 258 | Popular Tags |