KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > nodes > 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.abe.nodes;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.modules.xml.axi.AXIComponent;
26 import org.netbeans.modules.xml.axi.AXIDocument;
27 import org.netbeans.modules.xml.axi.AXIModel;
28 import org.netbeans.modules.xml.axi.Attribute;
29 import org.netbeans.modules.xml.axi.ContentModel;
30 import org.netbeans.modules.xml.axi.Element;
31 import org.netbeans.modules.xml.axi.datatype.CustomDatatype;
32 import org.netbeans.modules.xml.axi.datatype.Datatype;
33 import org.netbeans.modules.xml.xam.ComponentEvent;
34 import org.netbeans.modules.xml.xam.ComponentListener;
35 import org.openide.nodes.Children;
36 import org.openide.nodes.Node;
37
38 /**
39  *
40  * @author Chris Webster
41  */

42 public class CategorizedChildren extends Children.Keys
43         implements ComponentListener {
44     private ABEUIContext context;
45     private AXIDocument document;
46     private List JavaDoc<Class JavaDoc> childFilters;
47     
48     /** Creates a new instance of ABENodeChildren */
49     public CategorizedChildren(ABEUIContext context, AXIDocument document,
50             List JavaDoc<Class JavaDoc> childFilters) {
51         this.context = context;
52         this.document = document;
53         this.childFilters = childFilters;
54     }
55     
56     /**
57      *
58      *
59      */

60     private boolean isChildAllowed(Class JavaDoc componentClass) {
61         // If no filters are specified, allow the child
62
if (getChildFilters()==null)
63             return true;
64         
65         for (Class JavaDoc clazz: getChildFilters()) {
66             if (clazz.isAssignableFrom(componentClass))
67                 return true;
68         }
69         
70         return false;
71     }
72     
73     /**
74      *
75      *
76      */

77     protected java.util.List JavaDoc<Node> createKeys() {
78         List JavaDoc<Node> keys=new ArrayList JavaDoc<Node>();
79         
80         // categorize only for schema node
81
if(document instanceof AXIDocument) {
82             if(isChildAllowed(Datatype.class))
83                 keys.add(new PrimitiveSimpleTypesNode(getContext()));
84             if(isChildAllowed(ContentModel.class))
85                 keys.add(new GlobalContentModelsNode(getContext(), document));
86             if(isChildAllowed(CustomDatatype.class))
87                 keys.add(new SimpleTypesNode(getContext(), document));
88             if(isChildAllowed(Attribute.class))
89                 keys.add(new GlobalAttributesNode(getContext(), document));
90             if(isChildAllowed(Element.class))
91                 keys.add(new GlobalElementsNode(getContext(), document));
92             List JavaDoc<AXIModel> refModels = getContext().getModel().
93                     getReferencedModels();
94             if(refModels != null && refModels.size() != 0) {
95                 keys.add(new ReferencedSchemasNode(getContext(),
96                         document, childFilters));
97             }
98         } else {
99             // add nodes in lexical order
100
for (AXIComponent child: document.getChildren()) {
101                 Node node=getContext().getFactory().createNode(getNode(), child);
102                 keys.add(node);
103             }
104         }
105         
106         return keys;
107     }
108     
109     /**
110      *
111      *
112      */

113     @Override JavaDoc
114     protected Node[] createNodes(Object JavaDoc key) {
115         Node[] result=null;
116         
117         if (key instanceof Node)
118             result=new Node[] { (Node)key };
119         
120         return result;
121     }
122     
123     private ABEUIContext getContext() {
124         return context;
125     }
126     
127     private void refreshChildren() {
128         setKeys(createKeys());
129     }
130     
131     protected void addNotify() {
132         super.addNotify();
133         refreshChildren();
134 // ComponentListener cl = (ComponentListener)
135
// WeakListeners.create(ComponentListener.class, this,
136
// parentComponent.getModel());
137
// parentComponent.getModel().addComponentListener(cl);
138
}
139     
140     protected void removeNotify() {
141         super.removeNotify();
142         setKeys(Collections.emptyList());
143     }
144     
145     public void valueChanged(ComponentEvent evt) {
146     }
147     
148     public void childrenDeleted(ComponentEvent evt) {
149         if (evt.getSource() == document) {
150             refreshChildren();
151         }
152     }
153     
154     public void childrenAdded(ComponentEvent evt) {
155         if (evt.getSource() == document) {
156             refreshChildren();
157         }
158     }
159     
160     private List JavaDoc<Class JavaDoc> getChildFilters() {
161         return childFilters;
162     }
163 }
164
Popular Tags