#48509 folder chooser better handles projects with multiple generic source groups.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tasklist.docscan/src/org/netbeans/modules/tasklist/docscan/Choosers.java Tue Sep 14 15:33:34 2004 +0000
1.3 @@ -0,0 +1,253 @@
1.4 +/*
1.5 + * Sun Public License Notice
1.6 + *
1.7 + * The contents of this file are subject to the Sun Public License
1.8 + * Version 1.0 (the "License"). You may not use this file except in
1.9 + * compliance with the License. A copy of the License is available at
1.10 + * http://www.sun.com/
1.11 + *
1.12 + * The Original Code is NetBeans. The Initial Developer of the Original
1.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
1.14 + * Microsystems, Inc. All Rights Reserved.
1.15 + */
1.16 +
1.17 +
1.18 +package org.netbeans.modules.tasklist.docscan;
1.19 +
1.20 +import org.openide.nodes.AbstractNode;
1.21 +import org.openide.nodes.Children;
1.22 +import org.openide.nodes.Node;
1.23 +import org.openide.filesystems.FileObject;
1.24 +import org.openide.filesystems.FileUtil;
1.25 +import org.openide.util.lookup.Lookups;
1.26 +import org.openide.loaders.DataObject;
1.27 +import org.openide.loaders.DataObjectNotFoundException;
1.28 +import org.netbeans.api.project.*;
1.29 +import org.netbeans.api.project.ui.OpenProjects;
1.30 +
1.31 +import java.awt.*;
1.32 +import java.util.Arrays;
1.33 +import java.util.Comparator;
1.34 +import java.util.Collections;
1.35 +
1.36 +/**
1.37 + * Utility class defining project chooser.
1.38 + *
1.39 + * @author Petr Kuzel
1.40 + */
1.41 +class Choosers {
1.42 + // handle select folder life-time
1.43 + public static Node icons = null;
1.44 +
1.45 + /**
1.46 + * Logical view over opened projects
1.47 + */
1.48 + public static Node projectView() {
1.49 +
1.50 + Children.SortedArray kids = new Children.SortedArray();
1.51 + kids.setComparator(new Comparator() {
1.52 + public int compare(Object o1, Object o2) {
1.53 + return ((Node) o1).getDisplayName().compareToIgnoreCase(((Node) o2).getDisplayName());
1.54 + }
1.55 + });
1.56 +
1.57 + Project[] projects = OpenProjects.getDefault().getOpenProjects();
1.58 + for (int pi = 0; pi < projects.length; pi++) {
1.59 + Project project = projects[pi];
1.60 + Sources sources = ProjectUtils.getSources(project);
1.61 + SourceGroup[] group = sources.getSourceGroups(Sources.TYPE_GENERIC);
1.62 +
1.63 + if (group.length == 0) continue;
1.64 +
1.65 + // here we work with assumption that if project has only one
1.66 + // source group it is project folder itself and that the project
1.67 + // folder source group is named by the project
1.68 +
1.69 + if (group.length > 1) {
1.70 + kids.add(new Node[] {new ProjectNode(project)});
1.71 + } else {
1.72 + FileObject folder = group[0].getRootFolder();
1.73 + if (folder.isFolder() == false) continue;
1.74 + kids.add(new Node[]{new FolderNode(folder, group[0])});
1.75 + prepareFolderIcons(folder);
1.76 + }
1.77 + }
1.78 +
1.79 + final Node content = new AbstractNode(kids) {
1.80 + public void setName(String name) {
1.81 + super.setName(name);
1.82 + super.setIconBase("org/netbeans/modules/tasklist/docscan/repository"); // NOI18N
1.83 + }
1.84 + };
1.85 +
1.86 + content.setName(Util.getString("projects"));
1.87 + return content;
1.88 + }
1.89 +
1.90 + /** Hack no way how to get L&F specifics folder icons. Get it from random folder node. */
1.91 + private static void prepareFolderIcons(FileObject fo) {
1.92 + if (icons == null) {
1.93 + try {
1.94 + DataObject dobj = DataObject.find(fo);
1.95 + icons = dobj.getNodeDelegate();
1.96 + } catch (DataObjectNotFoundException e) {
1.97 + // ignore
1.98 + }
1.99 + }
1.100 + }
1.101 +
1.102 + /**
1.103 + * Used for project with multiple generic source groups
1.104 + */
1.105 + public static class ProjectNode extends AbstractNode {
1.106 +
1.107 + private final Project project;
1.108 +
1.109 + public ProjectNode(Project project) {
1.110 + this(new Children.SortedArray(), project);
1.111 + }
1.112 +
1.113 + private ProjectNode(Children.SortedArray children, Project project) {
1.114 + super(children);
1.115 + this.project = project;
1.116 +
1.117 + children.setComparator(new Comparator() {
1.118 + public int compare(Object o1, Object o2) {
1.119 + return ((Node) o1).getDisplayName().compareToIgnoreCase(((Node) o2).getDisplayName());
1.120 + }
1.121 + });
1.122 +
1.123 + Sources sources = ProjectUtils.getSources(project);
1.124 + SourceGroup[] group = sources.getSourceGroups(Sources.TYPE_GENERIC);
1.125 + Arrays.sort(group, new Comparator() {
1.126 + public int compare(Object o1, Object o2) {
1.127 + return ((SourceGroup) o1).getDisplayName().compareToIgnoreCase(((SourceGroup) o2).getDisplayName());
1.128 + }
1.129 + });
1.130 +
1.131 + for (int i = 0; i < group.length; i++) {
1.132 + FileObject folder = group[i].getRootFolder();
1.133 + if (folder.isFolder() == false) continue;
1.134 + children.add(new Node[]{new FolderNode(folder, group[i])});
1.135 + prepareFolderIcons(folder);
1.136 + }
1.137 + }
1.138 +
1.139 + public String getDisplayName() {
1.140 + ProjectInformation pi = getProjectInformation();
1.141 + return pi != null ? pi.getDisplayName() : FileUtil.getFileDisplayName(project.getProjectDirectory());
1.142 + }
1.143 +
1.144 + public Image getIcon(int type) {
1.145 + ProjectInformation pi = getProjectInformation();
1.146 + //TODO return pi != null ? pi.getIcon().
1.147 + if (icons != null) {
1.148 + return icons.getIcon(type);
1.149 + } else {
1.150 + return super.getIcon(type);
1.151 + }
1.152 + }
1.153 +
1.154 + public Image getOpenedIcon(int type) {
1.155 + return getIcon(type);
1.156 + }
1.157 +
1.158 + private ProjectInformation getProjectInformation() {
1.159 + return (ProjectInformation) project.getLookup().lookup(ProjectInformation.class);
1.160 + }
1.161 +
1.162 + }
1.163 +
1.164 + /**
1.165 + * Visualizes folder structure.
1.166 + */
1.167 + public static class FolderNode extends AbstractNode {
1.168 +
1.169 + private final FileObject fileObject;
1.170 + private SourceGroup group;
1.171 +
1.172 + public FolderNode(FileObject fileObject, SourceGroup root) {
1.173 + super(new FolderContent(fileObject, root), Lookups.singleton(fileObject));
1.174 + this.fileObject = fileObject;
1.175 + group = root;
1.176 + }
1.177 +
1.178 + public FolderNode(FileObject fileObject) {
1.179 + super(new FolderContent(fileObject), Lookups.singleton(fileObject));
1.180 + this.fileObject = fileObject;
1.181 + }
1.182 +
1.183 + public String getDisplayName() {
1.184 + if (group != null) {
1.185 + return group.getDisplayName();
1.186 + } else {
1.187 + return fileObject.getName();
1.188 + }
1.189 + }
1.190 +
1.191 + public Image getIcon(int type) {
1.192 +
1.193 + // XXX how to convert icon to image?
1.194 +// if (group != null) {
1.195 +// Icon icon = group.getIcon(false);
1.196 +// if (icon != null) {
1.197 +// return icon.
1.198 +// }
1.199 +// }
1.200 +
1.201 + // XXX how to dynamically get icon (that is subject to L&F)
1.202 + if (icons != null) {
1.203 + return icons.getIcon(type);
1.204 + } else {
1.205 + return super.getIcon(type);
1.206 + }
1.207 + }
1.208 +
1.209 + public Image getOpenedIcon(int type) {
1.210 + // XXX how to dynamically get icon (that is subject to L&F)
1.211 + if (icons != null) {
1.212 + return icons.getOpenedIcon(type);
1.213 + } else {
1.214 + return super.getOpenedIcon(type);
1.215 + }
1.216 + }
1.217 +
1.218 + private static class FolderContent extends Children.Keys {
1.219 +
1.220 + private final FileObject fileObject;
1.221 + private final SourceGroup group;
1.222 +
1.223 + public FolderContent(FileObject fileObject) {
1.224 + this(fileObject, null);
1.225 + }
1.226 +
1.227 + public FolderContent(FileObject fileObject, SourceGroup group) {
1.228 + this.fileObject = fileObject;
1.229 + this.group = group;
1.230 + }
1.231 +
1.232 + protected void addNotify() {
1.233 + FileObject[] fo = fileObject.getChildren();
1.234 + Arrays.sort(fo, new Comparator() {
1.235 + public int compare(Object o1, Object o2) {
1.236 + return ((FileObject) o1).getNameExt().compareToIgnoreCase(((FileObject) o2).getNameExt());
1.237 + }
1.238 + });
1.239 + setKeys(Arrays.asList(fo));
1.240 + }
1.241 +
1.242 + protected void removeNotify() {
1.243 + setKeys(Collections.EMPTY_SET);
1.244 + }
1.245 +
1.246 + protected Node[] createNodes(Object key) {
1.247 + FileObject fo = (FileObject) key;
1.248 + if (fo.isFolder() && (group == null || group.contains(fo))) {
1.249 + return new Node[]{new FolderNode(fo)};
1.250 + } else {
1.251 + return new Node[0];
1.252 + }
1.253 + }
1.254 + }
1.255 + }
1.256 +}
2.1 --- a/tasklist.docscan/src/org/netbeans/modules/tasklist/docscan/SourceTasksView.java Mon Sep 13 20:49:57 2004 +0000
2.2 +++ b/tasklist.docscan/src/org/netbeans/modules/tasklist/docscan/SourceTasksView.java Tue Sep 14 15:33:34 2004 +0000
2.3 @@ -1407,7 +1407,7 @@
2.4 background = null;
2.5
2.6 // prepare content for selector
2.7 - final Node content = projectView();
2.8 + final Node content = Choosers.projectView();
2.9 NodeOperation op = NodeOperation.getDefault();
2.10
2.11 try {
2.12 @@ -1425,147 +1425,7 @@
2.13 } catch (UserCancelException e) {
2.14 // no folders selected keep previous one
2.15 } finally {
2.16 - icons = null;
2.17 - }
2.18 - }
2.19 -
2.20 - // handle select folder life-time
2.21 - static Node icons = null;
2.22 -
2.23 - /** Logical view over opened projects */
2.24 - private Node projectView() {
2.25 -
2.26 - Children.SortedArray kids = new Children.SortedArray();
2.27 - kids.setComparator(new Comparator() {
2.28 - public int compare(Object o1, Object o2) {
2.29 - return ((FolderNode) o1).getDisplayName().compareToIgnoreCase(((FolderNode) o2).getDisplayName());
2.30 - }
2.31 - });
2.32 -
2.33 - Project[] projects = OpenProjects.getDefault().getOpenProjects();
2.34 - for (int pi = 0; pi<projects.length; pi++) {
2.35 - Project project = projects[pi];
2.36 - Sources sources = ProjectUtils.getSources(project);
2.37 - SourceGroup[] group =sources.getSourceGroups(Sources.TYPE_GENERIC);
2.38 - Arrays.sort(group, new Comparator() {
2.39 - public int compare(Object o1, Object o2) {
2.40 - return ((SourceGroup) o1).getDisplayName().compareToIgnoreCase(((SourceGroup) o2).getDisplayName());
2.41 - }
2.42 - });
2.43 -
2.44 - for (int i=0; i<group.length; i++) {
2.45 - FileObject folder = group[i].getRootFolder();
2.46 - if (folder.isFolder() == false) continue;
2.47 - kids.add(new Node[] {new FolderNode(folder, group[i])});
2.48 - if (icons == null) {
2.49 - try {
2.50 - DataObject dobj = DataObject.find(folder);
2.51 - icons = dobj.getNodeDelegate();
2.52 - } catch (DataObjectNotFoundException e) {
2.53 - // ignore
2.54 - }
2.55 - }
2.56 - }
2.57 - }
2.58 - final Node content = new AbstractNode(kids) {
2.59 - public void setName(String name) {
2.60 - super.setName(name);
2.61 - super.setIconBase("org/netbeans/modules/tasklist/docscan/repository"); // NOI18N
2.62 - }
2.63 - };
2.64 -
2.65 - content.setName(Util.getString("projects"));
2.66 - return content;
2.67 - }
2.68 -
2.69 - /** Visualizes folder structure. */
2.70 - private static class FolderNode extends AbstractNode {
2.71 -
2.72 - private final FileObject fileObject;
2.73 - private SourceGroup group;
2.74 -
2.75 - public FolderNode(FileObject fileObject, SourceGroup root) {
2.76 - super(new FolderContent(fileObject, root), Lookups.singleton(fileObject));
2.77 - this.fileObject = fileObject;
2.78 - group = root;
2.79 - }
2.80 -
2.81 - public FolderNode(FileObject fileObject) {
2.82 - super(new FolderContent(fileObject), Lookups.singleton(fileObject));
2.83 - this.fileObject = fileObject;
2.84 - }
2.85 -
2.86 - public String getDisplayName() {
2.87 - if (group != null) {
2.88 - return group.getDisplayName();
2.89 - } else {
2.90 - return fileObject.getName();
2.91 - }
2.92 - }
2.93 -
2.94 - public Image getIcon(int type) {
2.95 -
2.96 - // XXX how to convert icon to image?
2.97 -// if (group != null) {
2.98 -// Icon icon = group.getIcon(false);
2.99 -// if (icon != null) {
2.100 -// return icon.
2.101 -// }
2.102 -// }
2.103 -
2.104 - // XXX how to dynamically get icon (that is subject to L&F)
2.105 - if (icons != null) {
2.106 - return icons.getIcon(type);
2.107 - } else {
2.108 - return super.getIcon(type);
2.109 - }
2.110 - }
2.111 -
2.112 - public Image getOpenedIcon(int type) {
2.113 - // XXX how to dynamically get icon (that is subject to L&F)
2.114 - if (icons != null) {
2.115 - return icons.getOpenedIcon(type);
2.116 - } else {
2.117 - return super.getOpenedIcon(type);
2.118 - }
2.119 - }
2.120 -
2.121 - private static class FolderContent extends Children.Keys {
2.122 -
2.123 - private final FileObject fileObject;
2.124 - private final SourceGroup group;
2.125 -
2.126 - public FolderContent(FileObject fileObject) {
2.127 - this(fileObject, null);
2.128 - }
2.129 -
2.130 - public FolderContent(FileObject fileObject, SourceGroup group) {
2.131 - this.fileObject = fileObject;
2.132 - this.group = group;
2.133 - }
2.134 -
2.135 - protected void addNotify() {
2.136 - FileObject[] fo = fileObject.getChildren();
2.137 - Arrays.sort(fo, new Comparator() {
2.138 - public int compare(Object o1, Object o2) {
2.139 - return ((FileObject) o1).getNameExt().compareToIgnoreCase(((FileObject) o2).getNameExt());
2.140 - }
2.141 - });
2.142 - setKeys(Arrays.asList(fo));
2.143 - }
2.144 -
2.145 - protected void removeNotify() {
2.146 - setKeys(Collections.EMPTY_SET);
2.147 - }
2.148 -
2.149 - protected Node[] createNodes(Object key) {
2.150 - FileObject fo = (FileObject) key;
2.151 - if (fo.isFolder() && (group == null || group.contains(fo))) {
2.152 - return new Node[] {new FolderNode(fo)};
2.153 - } else {
2.154 - return new Node[0];
2.155 - }
2.156 - }
2.157 + Choosers.icons = null;
2.158 }
2.159 }
2.160