View Javadoc

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              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 // not a display group node
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 // not a display group node
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 // not a display group node
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  * $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