KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > SectionNode


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.xml.multiview;
20
21 import org.netbeans.modules.xml.multiview.ui.BoxPanel;
22 import org.netbeans.modules.xml.multiview.ui.SectionInnerPanel;
23 import org.netbeans.modules.xml.multiview.ui.SectionNodePanel;
24 import org.netbeans.modules.xml.multiview.ui.SectionNodeView;
25 import org.netbeans.modules.xml.multiview.ui.SectionNodeInnerPanel;
26 import org.openide.nodes.AbstractNode;
27 import org.openide.nodes.Children;
28 import org.openide.nodes.Node;
29 import org.openide.util.HelpCtx;
30
31 import java.util.List JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.awt.*;
34
35 /**
36  * This class represents a section node. In other words, this class represents
37  * a node that in turn represents a section.
38  *
39  * @author pfiala
40  *
41  * @see org.netbeans.modules.xml.multiview.ui.SectionNodePanel
42  * @see org.netbeans.modules.xml.multiview.ui.SectionNodeView
43  * @see org.netbeans.modules.xml.multiview.ui.SectionNodeInnerPanel
44  *
45  */

46 public class SectionNode extends AbstractNode {
47
48     protected final Object JavaDoc key;
49     private boolean expanded = false;
50     private SectionNodePanel sectionPanel = null;
51     private final String JavaDoc iconBase;
52     private final SectionNodeView sectionNodeView;
53     protected boolean helpProvider = false;
54
55     /**
56      * Create a new section node with a given child set.
57      *
58      * @param children the children for this node.
59      * @param key the key by which this node is identified
60      * @param title the title for the node
61      * @param iconBase base resource for icons (without initial slash)
62      */

63     protected SectionNode(SectionNodeView sectionNodeView, Children children, Object JavaDoc key, String JavaDoc title,
64                                                          String JavaDoc iconBase) {
65         super(children);
66         this.sectionNodeView = sectionNodeView;
67         this.key = key;
68         super.setDisplayName(title);
69         super.setIconBase(iconBase);
70         this.iconBase = iconBase;
71         sectionNodeView.registerNode(this);
72     }
73
74     public SectionNodeView getSectionNodeView() {
75         return sectionNodeView;
76     }
77
78     public Object JavaDoc getKey() {
79         return key;
80     }
81
82     public void addChild(SectionNode node) {
83         getChildren().add(new Node[]{node});
84     }
85
86     /**
87      * Creates an inner panel for this and populates
88      * it with the children of this node (if there are any).
89      */

90     public SectionNodeInnerPanel createInnerPanel() {
91         Children children = getChildren();
92         if (children.getNodesCount() == 0) {
93             return createNodeInnerPanel();
94         } else {
95             BoxPanel boxPanel = new BoxPanel(sectionNodeView);
96             populateBoxPanel(boxPanel);
97             return boxPanel;
98         }
99     }
100
101     /**
102      * Populates the associated inner panel with the children
103      * of this.
104      */

105     public void populateBoxPanel() {
106         SectionInnerPanel innerPanel = getSectionNodePanel().getInnerPanel();
107         if (innerPanel instanceof BoxPanel) {
108             populateBoxPanel((BoxPanel) innerPanel);
109         }
110     }
111     
112     /**
113      * Populates the given box panel with the children
114      * of this.
115      * @param boxPanel the panel to be populated
116      */

117     public void populateBoxPanel(BoxPanel boxPanel) {
118         List JavaDoc nodeList = new LinkedList JavaDoc();
119         SectionInnerPanel nodeInnerPanel = createNodeInnerPanel();
120         if (nodeInnerPanel != null) {
121             nodeList.add(nodeInnerPanel);
122         }
123         Node[] nodes = getChildren().getNodes();
124         for (int i = 0; i < nodes.length; i++) {
125             nodeList.add(((SectionNode) nodes[i]).getSectionNodePanel());
126         }
127         boxPanel.setComponents((Component[]) nodeList.toArray(new Component[0]));
128     }
129
130
131     public HelpCtx getHelpCtx() {
132         if (helpProvider) {
133             return new HelpCtx(getClass());
134         }
135         final Node parentNode = getParentNode();
136         if (parentNode instanceof SectionNode) {
137             return ((SectionNode) parentNode).getHelpCtx();
138         } else {
139             return new HelpCtx(sectionNodeView.getClass());
140         }
141     }
142
143     public boolean canDestroy() {
144         return true;
145     }
146
147     /**
148      * Creates appropriate SectionNodeInnerPanel. Override in
149      * subclasses, default implementation just returns null.
150      */

151     protected SectionNodeInnerPanel createNodeInnerPanel() {
152         return null;
153     }
154
155     public boolean isExpanded() {
156         return expanded;
157     }
158
159     public void setExpanded(boolean expanded) {
160         this.expanded = expanded;
161     }
162
163     public SectionNodePanel getSectionNodePanel() {
164         if (sectionPanel == null) {
165             sectionPanel = new SectionNodePanel(this);
166         }
167         return sectionPanel;
168     }
169
170     public String JavaDoc getIconBase() {
171         return iconBase;
172     }
173
174     public boolean equals(Object JavaDoc obj) {
175         if (getClass() == obj.getClass()) {
176             if (key.equals(((SectionNode) obj).key)) {
177                 return true;
178             }
179         }
180         return false;
181     }
182
183     public int hashCode() {
184         return key.hashCode();
185     }
186                  
187     public final void dataModelPropertyChange(Object JavaDoc source, String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
188         if (sectionPanel != null) {
189             SectionInnerPanel innerPanel = sectionPanel.getInnerPanel();
190             if (innerPanel != null) {
191                 innerPanel.dataModelPropertyChange(source, propertyName, oldValue, newValue);
192             }
193         }
194         Children children = getChildren();
195         if (children != null) {
196             Node[] nodes = children.getNodes();
197             for (int i = 0; i < nodes.length; i++) {
198                 Node node = nodes[i];
199                 if (node instanceof SectionNode) {
200                     ((SectionNode) node).dataModelPropertyChange(source, propertyName, oldValue, newValue);
201                 }
202             }
203         }
204     }
205
206     
207     /**
208      * Recursively refreshes view of the associated inner panel of this node and
209      * all its children nodes.
210      */

211     public void refreshSubtree() {
212         if (sectionPanel != null) {
213             SectionInnerPanel innerPanel = sectionPanel.getInnerPanel();
214             if (innerPanel != null) {
215                 innerPanel.refreshView();
216             }
217         }
218         Children children = getChildren();
219         if (children != null) {
220             Node[] nodes = children.getNodes();
221             for (int i = 0; i < nodes.length; i++) {
222                 Node node = nodes[i];
223                 if (node instanceof SectionNode) {
224                     ((SectionNode) node).refreshSubtree();
225                 }
226             }
227         }
228     }
229
230     /**
231      * Gets the node associated with the given <code>element</code>. Searches
232      * recursively from the children nodes.
233      * @param element the object representing the key of the node that we're looking
234      * for
235      */

236     public SectionNode getNodeForElement(Object JavaDoc element) {
237         if (key.equals(element)) {
238             return this;
239         } else {
240             final Node[] nodes = getChildren().getNodes();
241             for (int i = 0; i < nodes.length; i++) {
242                 Node node = nodes[i];
243                 if (node instanceof SectionNode) {
244                     final SectionNode nodeForElement = ((SectionNode) node).getNodeForElement(element);
245                     if (nodeForElement != null) {
246                         return nodeForElement;
247                     }
248                 }
249             }
250             return null;
251         }
252     }
253 }
254
Popular Tags