KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > nodes > ABENodeChildren


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.Comparator JavaDoc;
25 import java.util.List JavaDoc;
26 import org.netbeans.modules.xml.axi.AXIComponent;
27 import org.netbeans.modules.xml.axi.AXIContainer;
28 import org.netbeans.modules.xml.axi.AXIDocument;
29 import org.netbeans.modules.xml.axi.AbstractElement;
30 import org.netbeans.modules.xml.axi.AnyElement;
31 import org.netbeans.modules.xml.axi.Compositor;
32 import org.netbeans.modules.xml.axi.ContentModel;
33 import org.netbeans.modules.xml.axi.Element;
34 import org.netbeans.modules.xml.xam.ComponentEvent;
35 import org.netbeans.modules.xml.xam.ComponentListener;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.Node;
38 import org.openide.util.WeakListeners;
39
40 /**
41  *
42  * @author Chris Webster
43  */

44 public class ABENodeChildren extends Children.Keys
45         implements ComponentListener {
46     private AXIComponent component;
47     
48     /** Creates a new instance of ABENodeChildren */
49     public ABENodeChildren(AXIComponent component) {
50         this.component = component;
51     }
52     
53     protected Node[] createNodes(Object JavaDoc key) {
54         if (key instanceof Compositor) {
55             return new Node[] {new CompositorNode((Compositor) key)};
56         }
57         
58         if(key instanceof AnyElement)
59             return new Node[] {new AnyElementNode((AnyElement)key)};
60         else if (key instanceof AbstractElement) {
61             return new Node[] {new ElementNode((AbstractElement)key)};
62         }
63         
64         if (key instanceof ContentModel) {
65             return new Node[] {new ContentModelNode((ContentModel)key)};
66         }
67         
68         return new Node[0];
69     }
70     
71     private void refreshChildren() {
72         setKeys(sortComponents(component.getChildren()));
73     }
74     
75     protected void addNotify() {
76         super.addNotify();
77         refreshChildren();
78         ComponentListener cl = (ComponentListener)
79         WeakListeners.create(ComponentListener.class, this,
80                 component.getModel());
81         component.getModel().addComponentListener(cl);
82     }
83     
84     protected void removeNotify() {
85         super.removeNotify();
86         setKeys(Collections.emptyList());
87     }
88     
89     public void valueChanged(ComponentEvent evt) {
90     }
91     
92     public void childrenDeleted(ComponentEvent evt) {
93         if (evt.getSource() == component) {
94             refreshChildren();
95         }
96     }
97     
98     public void childrenAdded(ComponentEvent evt) {
99         if (evt.getSource() == component) {
100             refreshChildren();
101         }
102     }
103     
104     private List JavaDoc sortComponents(List JavaDoc<AXIComponent> list){
105         //sort only for AXIDocument.
106
if(! (this.component instanceof AXIDocument) )
107             return list;
108         //separate out elements and CMs
109
List JavaDoc<AXIContainer> el = new ArrayList JavaDoc<AXIContainer>();
110         List JavaDoc<AXIContainer> cml = new ArrayList JavaDoc<AXIContainer>();
111         for(AXIComponent comp: list){
112             if(comp instanceof AbstractElement){
113                 el.add((AXIContainer) comp);
114             }else if(comp instanceof ContentModel){
115                 if( ((ContentModel)comp).getType() == ContentModel.ContentModelType.COMPLEX_TYPE)
116                     cml.add((AXIContainer)comp);
117             }
118         }
119         //sort GEs
120
Collections.sort(el,
121                 new Comparator JavaDoc<AXIContainer>() {
122             public int compare(AXIContainer e1, AXIContainer e2) {
123                 return e1.getName().compareTo(e2.getName());
124             }
125             
126         });
127         //sort GCTs
128
Collections.sort(cml,
129                 new Comparator JavaDoc<AXIContainer>() {
130             public int compare(AXIContainer e1, AXIContainer e2) {
131                 return e1.getName().compareTo(e2.getName());
132             }
133             
134         });
135         List JavaDoc<AXIContainer> result = new ArrayList JavaDoc<AXIContainer>();
136         //club both arrays
137
result.addAll(el);
138         result.addAll(cml);
139         return result;
140     }
141 }
142
Popular Tags