Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
MutableDisplayGroupNode |
|
| 2.875;2.875 |
1 | /* |
|
2 | Wotonomy: OpenStep design patterns for pure Java applications. |
|
3 | Copyright (C) 2001 Intersect Software Corporation |
|
4 | ||
5 | This library is free software; you can redistribute it and/or |
|
6 | modify it under the terms of the GNU Lesser General Public |
|
7 | License as published by the Free Software Foundation; either |
|
8 | version 2.1 of the License, or (at your option) any later version. |
|
9 | ||
10 | This library is distributed in the hope that it will be useful, |
|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 | Lesser General Public License for more details. |
|
14 | ||
15 | You should have received a copy of the GNU Lesser General Public |
|
16 | License along with this library; if not, see http://www.gnu.org |
|
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 | 0 | super( aParentAssociation, aParentGroup, anObject ); |
57 | 0 | } |
58 | ||
59 | public int getIndex(TreeNode node) |
|
60 | { |
|
61 | 0 | return getIndex( (DisplayGroupNode) node ); |
62 | } |
|
63 | ||
64 | public TreeNode getChildAt(int childIndex) |
|
65 | { |
|
66 | 0 | return (TreeNode) getChildNodeAt( childIndex ); |
67 | } |
|
68 | ||
69 | public TreeNode getParent() |
|
70 | { |
|
71 | 0 | Object parent = getParentGroup(); |
72 | 0 | if ( parent instanceof TreeNode ) |
73 | { |
|
74 | 0 | return (TreeNode) parent; |
75 | } |
|
76 | 0 | return null; |
77 | } |
|
78 | ||
79 | public void insert(MutableTreeNode aChild, int anIndex) |
|
80 | { |
|
81 | 0 | if ( aChild instanceof DisplayGroupNode ) |
82 | { |
|
83 | 0 | insertObjectAtIndex( |
84 | 0 | ((DisplayGroupNode)aChild).object(), anIndex ); |
85 | 0 | } |
86 | else // not a display group node |
|
87 | { |
|
88 | 0 | throw new WotonomyException( |
89 | 0 | "Cannot insert nodes of type: " + aChild ); |
90 | } |
|
91 | 0 | } |
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 | 0 | if ( node instanceof DisplayGroupNode ) |
100 | { |
|
101 | 0 | remove((DisplayGroupNode)node); |
102 | 0 | } |
103 | else // not a display group node |
|
104 | { |
|
105 | 0 | throw new WotonomyException( |
106 | 0 | "Cannot insert nodes of type: " + node ); |
107 | } |
|
108 | 0 | } |
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 | 0 | if ( newParent instanceof DisplayGroupNode ) |
119 | { |
|
120 | 0 | setParent((DisplayGroupNode)newParent); |
121 | 0 | } |
122 | else // not a display group node |
|
123 | { |
|
124 | 0 | throw new WotonomyException( |
125 | 0 | "Cannot set parent to nodes of type: " + newParent ); |
126 | } |
|
127 | 0 | } |
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 | 0 | if ( !( parentAssociation.object() instanceof JTree ) ) |
137 | { |
|
138 | 0 | super.fireEventsForChanges( oldObjects, newObjects ); |
139 | 0 | return; |
140 | } |
|
141 | ||
142 | 0 | JTree tree = (JTree) parentAssociation.object(); |
143 | 0 | Map expansionMap = new HashMap(); |
144 | DisplayGroupNode node; |
|
145 | TreePath path; |
|
146 | 0 | for ( int i = 0; i < oldObjects.length; i++ ) |
147 | { |
|
148 | 0 | node = (DisplayGroupNode) |
149 | 0 | getChildNodeForObject( oldObjects[i] ); |
150 | 0 | if ( node != null && ! node.isLeaf() ) |
151 | { |
|
152 | 0 | expansionMap.put( node, new Boolean( |
153 | 0 | tree.isExpanded( node.treePath() ) ) ); |
154 | } |
|
155 | } |
|
156 | ||
157 | 0 | super.fireEventsForChanges( oldObjects, newObjects ); |
158 | ||
159 | Object value; |
|
160 | 0 | Iterator iterator = new LinkedList( childNodes.values() ).iterator(); |
161 | 0 | while ( iterator.hasNext() ) |
162 | { |
|
163 | 0 | node = (DisplayGroupNode) iterator.next(); |
164 | 0 | value = expansionMap.get( node ); |
165 | 0 | if ( value != null ) |
166 | { |
|
167 | 0 | if ( Boolean.TRUE.equals( value ) ) |
168 | { |
|
169 | 0 | tree.expandPath( node.treePath() ); |
170 | 0 | } |
171 | else |
|
172 | { |
|
173 | 0 | tree.collapsePath( node.treePath() ); |
174 | } |
|
175 | 0 | } |
176 | } |
|
177 | 0 | } |
178 | } |
|
179 | /* |
|
180 | * $Log$ |
|
181 | * Revision 1.2 2006/02/18 23:19:05 cgruber |
|
182 | * Update imports and maven dependencies. |
|
183 | * |
|
184 | * Revision 1.1 2006/02/16 13:22:22 cgruber |
|
185 | * Check in all sources in eclipse-friendly maven-enabled packages. |
|
186 | * |
|
187 | * Revision 1.8 2003/08/06 23:07:52 chochos |
|
188 | * general code cleanup (mostly, removing unused imports) |
|
189 | * |
|
190 | * Revision 1.7 2002/04/23 19:12:28 mpowers |
|
191 | * Reimplemented fireEventsForChanges. Fitter and happier. |
|
192 | * |
|
193 | * Revision 1.6 2002/04/10 21:20:04 mpowers |
|
194 | * Better handling for tree nodes when working with editing contexts. |
|
195 | * Better handling for invalidation. No longer broadcasting events |
|
196 | * when nodes have not been "registered" in the tree. |
|
197 | * |
|
198 | * Revision 1.5 2002/04/03 20:01:47 mpowers |
|
199 | * Now remembers expanded state. |
|
200 | * |
|
201 | * Revision 1.4 2002/03/08 23:19:07 mpowers |
|
202 | * Added getParentGroup to DisplayGroupNode. |
|
203 | * |
|
204 | * Revision 1.3 2002/02/27 23:19:17 mpowers |
|
205 | * Refactoring of TreeAssociation to create TreeModelAssociation parent. |
|
206 | * |
|
207 | * Revision 1.2 2001/04/22 23:05:33 mpowers |
|
208 | * Totally revised DisplayGroupNode so each object gets its own node |
|
209 | * (so the nodes are no longer fixed by index). |
|
210 | * |
|
211 | * Revision 1.1 2001/04/21 23:05:56 mpowers |
|
212 | * Contributing the tree-specific concrete subclass of DisplayGroupNode. |
|
213 | * |
|
214 | * |
|
215 | */ |
|
216 |