KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > model > DesignTreeNode


1 package org.oddjob.designer.model;
2
3 import java.awt.Component JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Vector JavaDoc;
6
7 import javax.swing.tree.TreeNode JavaDoc;
8
9 import org.oddjob.Structural;
10 import org.oddjob.designer.view.FormFactory;
11 import org.oddjob.structural.StructuralEvent;
12 import org.oddjob.structural.StructuralListener;
13
14 /**
15  * This class wraps a DesignComponent allowing it to
16  * act as a TreeNode.
17  *
18  * @author Rob Gordon
19  */

20
21
22 public class DesignTreeNode
23         implements TreeNode JavaDoc, StructuralListener {
24
25     /** For list of children */
26     private final Vector JavaDoc nodeList = new Vector JavaDoc();
27
28     /** Parent node */
29     final private DesignTreeNode parent;
30
31     /** Save the JobTreeModel. */
32     final private DesignTreeModel model;
33
34     /** The design component. */
35     private final DesignComponent designComponent;
36
37     private Component JavaDoc detailView;
38     
39     /**
40      * Constructor.
41      *
42      * @param model The tree model this node belongs to.
43      * @param parent The parent node.
44      * @param node The structure node this is modelling.
45      */

46     public DesignTreeNode(DesignTreeModel model,
47             DesignTreeNode parent, DesignComponent node) {
48     
49         this.model = model;
50         this.parent = parent;
51         this.designComponent = node;
52     }
53     
54     public void build() {
55         if (designComponent instanceof Structural) {
56             ((Structural)designComponent).addStructuralListener(this);
57         }
58     }
59         
60     public DesignComponent getDesignComponent() {
61         return designComponent;
62     }
63         
64     // TreeNode methods
65

66     public Enumeration JavaDoc children() {
67         return nodeList.elements();
68     }
69
70     public boolean getAllowsChildren() {
71         return true;
72     }
73         
74     public TreeNode JavaDoc getChildAt(int index) {
75         return (DesignTreeNode)nodeList.elementAt(index);
76     }
77
78     public int getChildCount() {
79         return nodeList.size();
80     }
81
82     public boolean isLeaf() {
83         return nodeList.size() == 0 ? true : false;
84     }
85
86     public int getIndex(TreeNode JavaDoc child) {
87
88         return nodeList.indexOf(child);
89     }
90
91     public TreeNode JavaDoc getParent() {
92         
93         return parent;
94     }
95
96     public String JavaDoc toString() {
97         
98         return designComponent.name();
99     }
100
101     public void childAdded(StructuralEvent e) {
102
103         int index = e.getIndex();
104         Object JavaDoc o = e.getChild();
105         if (!(o instanceof DesignComponent)) {
106             throw new IllegalStateException JavaDoc("[" + o + "] is not a DesignComponent at in [" + designComponent + "]" );
107         }
108         DesignComponent childComponent = (DesignComponent) o;
109         DesignTreeNode child = new DesignTreeNode(model, this, childComponent);
110
111         synchronized (nodeList) {
112             nodeList.insertElementAt(child, index);
113         }
114
115         model.fireTreeNodesInserted(this, child, index);
116         child.build();
117     }
118     
119     public void childRemoved(StructuralEvent e) {
120         
121         int index = e.getIndex();
122         DesignTreeNode child = null;
123         synchronized (nodeList) {
124             child = (DesignTreeNode)nodeList.elementAt(index);
125             nodeList.removeElementAt(index);
126         }
127         child.destroy();
128
129         model.fireTreeNodesRemoved(this, child, index);
130     }
131     
132     public void destroy() {
133         while (nodeList.size() > 0) {
134             int index= nodeList.size() - 1;
135             DesignTreeNode child = (DesignTreeNode)nodeList.remove(index);
136             child.destroy();
137             model.fireTreeNodesRemoved(this, child, index);
138         }
139     
140         if (designComponent instanceof Structural) {
141             ((Structural)designComponent).removeStructuralListener(this);
142         }
143     }
144     
145     /**
146      * @return Returns the detailView.
147      */

148     public Component JavaDoc getDetailView() {
149         if (detailView == null) {
150             detailView = FormFactory.createForm(designComponent.form());
151         }
152         return detailView;
153     }
154 }
155
Popular Tags