KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > docscan > Choosers


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.docscan;
21
22 import java.awt.Image JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Collections JavaDoc;
26 import javax.swing.Icon JavaDoc;
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 /**
42  * Utility class defining project chooser.
43  *
44  * @author Petr Kuzel
45  */

46 class Choosers {
47     // handle select folder life-time
48
public static Node icons = null;
49
50     /**
51      * Logical view over opened projects
52      */

53     public static Node projectView() {
54
55         Children.SortedArray kids = new Children.SortedArray();
56         kids.setComparator(new Comparator JavaDoc() {
57             public int compare(Object JavaDoc o1, Object JavaDoc 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             // here we work with assumption that if project has only one
71
// source group it is project folder itself and that the project
72
// folder source group is named by the project
73

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 JavaDoc name) {
86                 super.setName(name);
87                 super.setIconBase("org/netbeans/modules/tasklist/docscan/repository"); // NOI18N
88
}
89         };
90
91         content.setName(Util.getString("projects"));
92         return content;
93     }
94
95     /** Hack no way how to get L&F specifics folder icons. Get it from random folder node. */
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                 // ignore
103
}
104         }
105     }
106
107     /**
108      * Used for project with multiple generic source groups
109      */

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 JavaDoc() {
123                 public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc() {
131                 public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc getDisplayName() {
145             return ProjectUtils.getInformation(project).getDisplayName();
146         }
147
148         public Image JavaDoc getIcon(int type) {
149             return Utilities.icon2Image(ProjectUtils.getInformation(project).getIcon());
150         }
151
152         public Image JavaDoc getOpenedIcon(int type) {
153             return getIcon(type);
154         }
155
156     }
157
158     /**
159      * Visualizes folder structure.
160      */

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 JavaDoc getDisplayName() {
178             if (group != null) {
179                 return group.getDisplayName();
180             } else {
181                 return fileObject.getName();
182             }
183         }
184
185         public Image JavaDoc getIcon(int type) {
186
187             if (group != null) {
188                 Icon JavaDoc icon = group.getIcon(false);
189                 if (icon != null) {
190                     return Utilities.icon2Image(icon);
191                 }
192             }
193
194             // XXX how to dynamically get icon (that is subject to L&F)
195
if (icons != null) {
196                 return icons.getIcon(type);
197             } else {
198                 return super.getIcon(type);
199             }
200         }
201
202         public Image JavaDoc getOpenedIcon(int type) {
203
204             if (group != null) {
205                 Icon JavaDoc icon = group.getIcon(true);
206                 if (icon != null) {
207                     return Utilities.icon2Image(icon);
208                 }
209             }
210
211             // XXX how to dynamically get icon (that is subject to L&F)
212
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 JavaDoc() {
236                     public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc 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