1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.ui.swing;
20
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.Map;
25
26 import javax.swing.JTree;
27 import javax.swing.tree.MutableTreeNode;
28 import javax.swing.tree.TreeNode;
29 import javax.swing.tree.TreePath;
30
31 import net.wotonomy.foundation.internal.WotonomyException;
32 import net.wotonomy.ui.EODisplayGroup;
33
34 /***
35 * A DisplayGroupNode that exposes the MutableTreeNode interface.
36 * This was required so that other subclasses of DisplayGroupNode
37 * could opt out of supporting MutableTreeNode (so that they can
38 * implement IlvActivity, for example).
39 *
40 * @author michael@mpowers.net
41 * @author $Author: cgruber $
42 * @version $Revision: 904 $
43 */
44 public class MutableDisplayGroupNode
45 extends DisplayGroupNode implements MutableTreeNode
46 {
47 /***
48 * Constructor for all nodes.
49 * Root node must have a null delegate.
50 */
51 public MutableDisplayGroupNode(
52 TreeModelAssociation aParentAssociation,
53 EODisplayGroup aParentGroup,
54 Object anObject )
55 {
56 super( aParentAssociation, aParentGroup, anObject );
57 }
58
59 public int getIndex(TreeNode node)
60 {
61 return getIndex( (DisplayGroupNode) node );
62 }
63
64 public TreeNode getChildAt(int childIndex)
65 {
66 return (TreeNode) getChildNodeAt( childIndex );
67 }
68
69 public TreeNode getParent()
70 {
71 Object parent = getParentGroup();
72 if ( parent instanceof TreeNode )
73 {
74 return (TreeNode) parent;
75 }
76 return null;
77 }
78
79 public void insert(MutableTreeNode aChild, int anIndex)
80 {
81 if ( aChild instanceof DisplayGroupNode )
82 {
83 insertObjectAtIndex(
84 ((DisplayGroupNode)aChild).object(), anIndex );
85 }
86 else
87 {
88 throw new WotonomyException(
89 "Cannot insert nodes of type: " + aChild );
90 }
91 }
92
93 /***
94 * Removes the node at the index corresponding
95 * to the index of the object.
96 */
97 public void remove(MutableTreeNode node)
98 {
99 if ( node instanceof DisplayGroupNode )
100 {
101 remove((DisplayGroupNode)node);
102 }
103 else
104 {
105 throw new WotonomyException(
106 "Cannot insert nodes of type: " + node );
107 }
108 }
109
110 /***
111 * Removes the value in the parent display group
112 * at the index that corresponds to the index of this node
113 * and add it to the end of the display group that corresponds
114 * to the user value of the specified node.
115 */
116 public void setParent(MutableTreeNode newParent)
117 {
118 if ( newParent instanceof DisplayGroupNode )
119 {
120 setParent((DisplayGroupNode)newParent);
121 }
122 else
123 {
124 throw new WotonomyException(
125 "Cannot set parent to nodes of type: " + newParent );
126 }
127 }
128
129 /***
130 * Overridden to remember expanded state for nodes
131 * after nodes have been rearranged.
132 */
133 protected void fireEventsForChanges(
134 Object[] oldObjects, Object[] newObjects )
135 {
136 if ( !( parentAssociation.object() instanceof JTree ) )
137 {
138 super.fireEventsForChanges( oldObjects, newObjects );
139 return;
140 }
141
142 JTree tree = (JTree) parentAssociation.object();
143 Map expansionMap = new HashMap();
144 DisplayGroupNode node;
145 TreePath path;
146 for ( int i = 0; i < oldObjects.length; i++ )
147 {
148 node = (DisplayGroupNode)
149 getChildNodeForObject( oldObjects[i] );
150 if ( node != null && ! node.isLeaf() )
151 {
152 expansionMap.put( node, new Boolean(
153 tree.isExpanded( node.treePath() ) ) );
154 }
155 }
156
157 super.fireEventsForChanges( oldObjects, newObjects );
158
159 Object value;
160 Iterator iterator = new LinkedList( childNodes.values() ).iterator();
161 while ( iterator.hasNext() )
162 {
163 node = (DisplayGroupNode) iterator.next();
164 value = expansionMap.get( node );
165 if ( value != null )
166 {
167 if ( Boolean.TRUE.equals( value ) )
168 {
169 tree.expandPath( node.treePath() );
170 }
171 else
172 {
173 tree.collapsePath( node.treePath() );
174 }
175 }
176 }
177 }
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216