KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xsl > ui > Util


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 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 JavaDoc;
38 import java.util.Arrays JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.awt.*;
41
42 /**
43  *
44  * @author Libor Kramolis
45  * @version 0.2
46  */

47 class Util extends AbstractUtil {
48
49     /** Default and only one instance of this class. */
50     public static final Util THIS = new Util();
51
52     /** Nobody can create instance of it, just me. */
53     private Util () {
54     }
55
56     // copy pasted from SourceTasksView >>>
57

58     /** Logical view over all opened projects */
59     static Node projectView() {
60
61         Children.SortedArray kids = new Children.SortedArray();
62         kids.setComparator(new Comparator JavaDoc() {
63                 public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc() {
74                 public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc name) {
104                 super.setName(name);
105                 super.setIconBase("org/netbeans/modules/xsl/resources/repository"); // NOI18N
106
}
107         };
108
109         content.setName(THIS.getString("projects"));
110         return content;
111     }
112
113     // handle select folder life-time
114
static Node icons = null;
115
116     /** Visualizes folder structure. */
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 JavaDoc 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             // XXX how to convert icon to image?
144
// if (group != null) {
145
// Icon icon = group.getIcon(false);
146
// if (icon != null) {
147
// return icon.
148
// }
149
// }
150

151             // XXX how to dynamically get icon (that is subject to L&F)
152
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             // XXX how to dynamically get icon (that is subject to L&F)
161
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 JavaDoc() {
185                     public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc 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     // copy pasted from SourceTasksView <<<
216

217 }
218
Popular Tags