KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > categorized > CategorizedChildren


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.ui.nodes.categorized;
21
22 import java.awt.Image JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.LinkedHashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import org.openide.nodes.Node;
29 import org.netbeans.modules.xml.xam.Named;
30 import org.netbeans.modules.xml.schema.model.Annotation;
31 import org.netbeans.modules.xml.schema.model.Notation;
32 import org.netbeans.modules.xml.schema.model.SchemaModelReference;
33 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
34 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
35 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
36 import org.netbeans.modules.xml.schema.model.GlobalElement;
37 import org.netbeans.modules.xml.schema.model.GlobalGroup;
38 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
39 import org.netbeans.modules.xml.schema.model.Schema;
40 import org.netbeans.modules.xml.schema.model.SchemaComponent;
41 import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
42 import org.netbeans.modules.xml.schema.model.SchemaModel;
43 import org.netbeans.modules.xml.schema.ui.nodes.FilteredSchemaComponentNodeChildren;
44 import org.netbeans.modules.xml.schema.ui.nodes.ReadOnlyCookie;
45 import org.netbeans.modules.xml.schema.ui.nodes.RefreshableChildren;
46 import org.netbeans.modules.xml.schema.ui.nodes.SchemaUIContext;
47 import org.netbeans.modules.xml.schema.ui.nodes.StructuralSchemaNodeFactory;
48 import org.openide.filesystems.FileObject;
49 import org.openide.filesystems.Repository;
50 import org.openide.loaders.DataObject;
51 import org.openide.loaders.DataObjectNotFoundException;
52 import org.openide.util.Utilities;
53
54 /**
55  *
56  * @author Todd Fast, todd.fast@sun.com
57  */

58 public class CategorizedChildren<C extends SchemaComponent>
59     extends RefreshableChildren
60 {
61     /**
62      *
63      *
64      */

65     public CategorizedChildren(SchemaUIContext context,
66         SchemaComponentReference<C> reference)
67     {
68         this(context,reference,null);
69     }
70
71
72     /**
73      *
74      *
75      */

76     public CategorizedChildren(SchemaUIContext context,
77         SchemaComponentReference<C> reference,
78         List JavaDoc<Class JavaDoc<? extends SchemaComponent>> childFilters)
79     {
80         super();
81         this.context=context;
82         this.reference=reference;
83         this.childFilters=childFilters;
84
85         extension = new CategorizedChildrenExtension();
86
87         // Create a context to be used for the structural node view
88
SchemaModel model = reference.get().getModel();
89         structuralContext=new SchemaUIContext(
90             model,
91             new StructuralSchemaNodeFactory(model,
92                 context.getLookup()),
93             context.getLookup());
94
95         // TODO: Need to receive event and then dispatch refresh to category
96
// child directly
97
// context.getModel().addPropertyChangeListener(
98
// new PropertyChangeListener()
99
// {
100
// public void propertyChange(PropertyChangeEvent event)
101
// {
102
// if (event.getSource()==getReference().get())
103
// refreshChildren();
104
// }
105
// });
106
}
107
108
109     /**
110      *
111      *
112      */

113     public SchemaUIContext getContext()
114     {
115         return context;
116     }
117
118
119     /**
120      *
121      *
122      */

123     protected SchemaUIContext getStructuralContext()
124     {
125         return structuralContext;
126     }
127
128     /**
129      *
130      *
131      */

132     public SchemaComponentReference<C> getReference()
133     {
134         return reference;
135     }
136
137
138     /**
139      *
140      *
141      */

142     public List JavaDoc<Class JavaDoc<? extends SchemaComponent>> getChildFilters()
143     {
144         return childFilters;
145     }
146
147
148     /**
149      *
150      *
151      */

152     @Override JavaDoc
153     protected void addNotify()
154     {
155         super.addNotify();
156         refreshChildren();
157     }
158
159
160     /**
161      *
162      *
163      */

164     @Override JavaDoc
165     protected void removeNotify()
166     {
167         super.removeNotify();
168         super.nodes.clear();
169         refresh();
170     }
171
172
173     /**
174      *
175      *
176      */

177     public void refreshChildren()
178     {
179         setKeys(createKeys());
180     }
181
182
183     /**
184      *
185      *
186      */

187     private boolean isChildAllowed(
188         Class JavaDoc<? extends SchemaComponent> componentClass)
189     {
190         // If no filters are specified, allow the child
191
if (getChildFilters()==null)
192             return true;
193
194         for (Class JavaDoc<? extends SchemaComponent> clazz: getChildFilters())
195         {
196             if (clazz.isAssignableFrom(componentClass))
197                 return true;
198         }
199
200         return false;
201     }
202
203
204     /**
205      *
206      *
207      */

208     protected List JavaDoc<Node> createKeys()
209     {
210         C parentComponent=getReference().get();
211
212         List JavaDoc<Node> keys=new ArrayList JavaDoc<Node>();
213
214         ReadOnlyCookie roc = (ReadOnlyCookie) getContext().getLookup().lookup(
215                 ReadOnlyCookie.class);
216         boolean readOnly = roc!=null && roc.isReadOnly();
217 // CustomizerProvider provider = (CustomizerProvider) getNode().
218
// getLookup().lookup(CustomizerProvider.class);
219
// if (provider != null && (roc == null || !roc.isReadOnly()))
220
// {
221
// keys.add(new DetailsNode(getContext(),provider));
222
// }
223
//
224
// add extension node
225
if (!readOnly && getChildFilters() != null &&
226                         isChildAllowed(PrimitiveSimpleType.class)) {
227                     keys.addAll(extension.getExtension(parentComponent, getContext()));
228                 }
229
230         // categorize only for schema node
231
if(parentComponent instanceof Schema)
232         {
233             // add children which are not categorized first
234
for (SchemaComponent child: parentComponent.getChildren(UNCATEGORIZED_TYPES))
235             {
236                 if(isChildAllowed(child.getComponentType()))
237                 {
238                     Node node=getContext().getFactory().createNode(child);
239                     keys.add(node);
240                 }
241             }
242             // add category nodes even if empty
243
for(Class JavaDoc<? extends SchemaComponent> componentType:CATEGORIZED_TYPES.keySet())
244             {
245                 // dont create category if filtered
246
if(!isChildAllowed(componentType)) continue;
247                 // In read-only mode, hide empty categories as there is no
248
// point in showing them (user cannot create new components).
249
if(readOnly && parentComponent.getChildren(componentType).isEmpty())
250                     continue;
251                 CategoryNode node=new CategoryNode(
252                         getContext(),
253                         getReference(),
254                         componentType,
255                         new FilteredSchemaComponentNodeChildren<C>(
256                         getContext(),getReference(),componentType,
257                         new SchemaComponentComparator()));
258
259                 String JavaDoc badge = CATEGORIZED_TYPES.get(componentType);
260                 node.setBadge(badge);
261                 keys.add(node);
262             }
263         }
264         else
265         {
266             // add nodes in lexical order
267
for (SchemaComponent child: parentComponent.getChildren())
268             {
269                 if(isChildAllowed(child.getComponentType()))
270                 {
271                     Node node=getContext().getFactory().createNode(child);
272                     keys.add(node);
273                 }
274             }
275         }
276
277         return keys;
278     }
279
280
281     /**
282      *
283      *
284      */

285     @Override JavaDoc
286     protected Node[] createNodes(Object JavaDoc key)
287     {
288         Node[] result=null;
289
290         if (key instanceof Node)
291             result=new Node[] { (Node)key };
292
293         return result;
294     }
295
296
297
298
299     ////////////////////////////////////////////////////////////////////////////
300
// Inner class
301
////////////////////////////////////////////////////////////////////////////
302

303     /**
304      *
305      *
306      */

307     private static class SchemaComponentComparator
308         implements Comparator JavaDoc<SchemaComponent>, Serializable JavaDoc {
309             private static final long serialVersionUID = 1L;
310
311         /**
312          *
313          *
314          */

315         public int compare(SchemaComponent comp1,
316             SchemaComponent comp2)
317         {
318             boolean ref1Named=comp1 instanceof Named;
319             boolean ref2Named=comp2 instanceof Named;
320
321             if (ref1Named && ref2Named)
322             {
323                 String JavaDoc ref1Name=((Named)comp1).getName();
324                 String JavaDoc ref2Name=((Named)comp2).getName();
325
326                 if (ref1Name!=null && ref2Name!=null)
327                     return ref1Name.compareTo(ref2Name);
328                 else
329                 {
330                     // Non-null names always sort before null names
331
if (ref1Name!=null)
332                         return -1;
333                     else
334                         return 1;
335                 }
336             }
337             else
338             {
339                 // Named components come before unnamed components
340
if (ref1Named)
341                     return -1;
342                 else
343                     return 1;
344             }
345         }
346     }
347
348
349
350         private static Node getFolderNode() {
351         FileObject fo =
352         Repository.getDefault().getDefaultFileSystem().getRoot();
353         Node n = null;
354         try {
355         DataObject dobj = DataObject.find(fo);
356         n = dobj.getNodeDelegate();
357         } catch (DataObjectNotFoundException ex) {
358         // cannot get the node for this, this shouldn't happen
359
// so just ignore
360
}
361         return n;
362     }
363         
364         public static Image JavaDoc getBadgedFolderIcon(int type, Class JavaDoc<? extends SchemaComponent> _class) {
365         Node n = getFolderNode();
366         Image JavaDoc i = null;
367         if (n != null) {
368         i = n.getIcon(type);
369         }
370             String JavaDoc badge = CATEGORIZED_TYPES.get(_class);
371             if(badge != null)
372                 return badgeImage(i, badge);
373             return null;
374     }
375         
376     public static Image JavaDoc getOpenedBadgedFolderIcon(int type, Class JavaDoc<? extends SchemaComponent> _class) {
377         Node n = getFolderNode();
378         Image JavaDoc i = null;
379         if (n != null) {
380         i = n.getOpenedIcon(type);
381         }
382             String JavaDoc badge = CATEGORIZED_TYPES.get(_class);
383             if(badge != null)
384                 return badgeImage(i, badge);
385         return null;
386     }
387         
388         
389         private static Image JavaDoc badgeImage(Image JavaDoc main, String JavaDoc badge) {
390         Image JavaDoc rv = main;
391         if (badge != null) {
392         Image JavaDoc badgeImage = Utilities.loadImage(badge);
393         rv = Utilities.mergeImages(main, badgeImage, 8, 8);
394         }
395         return rv;
396     }
397         
398
399     ////////////////////////////////////////////////////////////////////////////
400
// Instance members
401
////////////////////////////////////////////////////////////////////////////
402

403     private SchemaUIContext context;
404     private SchemaUIContext structuralContext;
405     private SchemaComponentReference<C> reference;
406     private List JavaDoc<Class JavaDoc<? extends SchemaComponent>> childFilters;
407     private CategorizedChildrenExtension extension;
408
409     // categories for schema node
410
// if we need add for more nodes, might be a good idea to create visitor
411
private static final java.util.Map JavaDoc<Class JavaDoc<? extends SchemaComponent>, String JavaDoc> CATEGORIZED_TYPES;
412     static {
413         CATEGORIZED_TYPES = new LinkedHashMap JavaDoc<Class JavaDoc<? extends SchemaComponent>, String JavaDoc>();
414         CATEGORIZED_TYPES.put(GlobalAttribute.class, "org/netbeans/modules/xml/schema/ui/nodes/resources/attribute_badge.png"); // NOI18N
415
CATEGORIZED_TYPES.put(GlobalAttributeGroup.class,"org/netbeans/modules/xml/schema/ui/nodes/resources/attribute_badge.png"); // NOI18N
416
CATEGORIZED_TYPES.put(GlobalComplexType.class,"org/netbeans/modules/xml/schema/ui/nodes/resources/complexType_badge.png"); // NOI18N
417
CATEGORIZED_TYPES.put(GlobalElement.class,"org/netbeans/modules/xml/schema/ui/nodes/resources/element_badge.png"); // NOI18N
418
CATEGORIZED_TYPES.put(GlobalGroup.class,"org/netbeans/modules/xml/schema/ui/nodes/resources/group_badge.png"); // NOI18N
419
CATEGORIZED_TYPES.put(SchemaModelReference.class,"org/netbeans/modules/xml/schema/ui/nodes/resources/referencedSchemas_badge.png"); //NOI18N
420
CATEGORIZED_TYPES.put(GlobalSimpleType.class, "org/netbeans/modules/xml/schema/ui/nodes/resources/simpleType_badge.png"); //NOI18N
421
};
422     public static final List JavaDoc<Class JavaDoc<? extends SchemaComponent>> UNCATEGORIZED_TYPES;
423     static {
424         UNCATEGORIZED_TYPES = new ArrayList JavaDoc<Class JavaDoc<? extends SchemaComponent>>(2);
425         UNCATEGORIZED_TYPES.add(Annotation.class);
426         UNCATEGORIZED_TYPES.add(Notation.class);
427     };
428 }
429
Popular Tags