KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > gridsplit > GridSplitModel


1 /*
2  * ComponentHierarchyModel.java
3  *
4  * Created on 19 November 2004, 16:15
5  */

6
7 package org.netbeans.swing.gridsplit;
8
9 import java.awt.Component JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Collection JavaDoc;
13
14 /**
15  * Hierarchical model for split cells.
16  *
17  * @author aubrecht
18  */

19 public class GridSplitModel {
20     
21     private GridSplitCell root;
22     private ArrayList JavaDoc listeners = new ArrayList JavaDoc();
23     
24     /** Creates a new instance of ComponentHierarchyModel */
25     public GridSplitModel( Component JavaDoc rootComp, Dimension JavaDoc preferredSize, double resizeWeight ) {
26         if( null != root ) {
27             throw new IllegalArgumentException JavaDoc( "The hierarchy root is already set." );
28         }
29         
30         root = new GridSplitCell( rootComp, resizeWeight, preferredSize );
31     }
32     
33     /**
34      * The topmost cell in the hierarchy.
35      */

36     public GridSplitCell getRootCell() {
37         return root;
38     }
39     
40     /**
41      * Add a component to the topmost cell (i.e. attach a component to the given SplitPane's side).
42      */

43     public void addToRoot( Component JavaDoc compToAdd, int side ) {
44         addToRoot( compToAdd, side, 0.5 );
45     }
46     
47     /**
48      * Add a component to the topmost cell (i.e. attach a component to the given SplitPane's side).
49      * @param initialSize The portion of the root cell that will be used as new component's initial size.
50      */

51     public void addToRoot( Component JavaDoc compToAdd, int side, double initialSize ) {
52         addToRoot( compToAdd, side, initialSize, 0.0 );
53     }
54     
55     /**
56      * Add a component to the topmost cell (i.e. attach a component to the given SplitPane's side).
57      * @param initialSize The portion of the root cell that will be used as new component's initial size.
58      * @param resizeWeight Component's resize weight, use 0.0 to preserve original component's size as much as possible.
59      */

60     public void addToRoot( Component JavaDoc compToAdd, int side, double initialSize, double resizeWeight ) {
61         GridSplitCell newCell = root.addToSide( compToAdd, side, initialSize, resizeWeight );
62         
63         fireCellAdded( newCell );
64     }
65     
66     /**
67      * Append a component to given side of given existing model's component.
68      */

69     public void addToSide( Component JavaDoc neighbor, Component JavaDoc compToAdd, int side ) {
70         addToSide( neighbor, compToAdd, side, 0.5 );
71     }
72
73     /**
74      * Append a component to given side of given existing model's component.
75      * @param initialSize The portion of the existing component's size that will be used as new component's initial size.
76      */

77     public void addToSide( Component JavaDoc neighbor, Component JavaDoc compToAdd, int side, double initialSize ) {
78         addToSide( neighbor, compToAdd, side, initialSize, 0.0 );
79     }
80     
81     /**
82      * Append a component to given side of given existing model's component.
83      * @param initialSize The portion of the existing component's size that will be used as new component's initial size.
84      * @param resizeWeight New component's resize weight, use 0.0 to preserve original component's size as much as possible.
85      */

86     public void addToSide( Component JavaDoc neighbor, Component JavaDoc compToAdd, int side, double initialSize, double resizeWeight ) {
87         GridSplitCell cell = findCellFor( neighbor );
88         if( null == cell ) {
89             throw new IllegalArgumentException JavaDoc( "Component not in hierarchy: " + neighbor );
90         }
91         
92         GridSplitCell newCell = cell.addToSide( compToAdd, side, initialSize, resizeWeight );
93         
94         fireCellAdded( newCell );
95     }
96
97     /**
98      * Remove component from model's hierarchy. Remaining cells may rearrange to merge
99      * cells with the same split sides into one.
100      */

101     public void remove( Component JavaDoc c ) {
102         GridSplitCell cell = findCellFor( c );
103         if( null == cell ) {
104             throw new IllegalArgumentException JavaDoc( "Component not in hierarchy: " + c );
105         }
106         
107         if( cell == root ) {
108             //what about this?
109
throw new IllegalArgumentException JavaDoc( "Cannot remove the root cell" );
110         }
111         
112         cell.getParent().remove( cell );
113         
114         fireCellRemoved( cell );
115     }
116
117     /**
118      * Show/hide cell in the hierarchy. A hidden component is not visible in the
119      * split pane but it remembers its original position and size in the hierarchy
120      * so it can 're-appear' at the some position when shown again.
121      */

122     public void setHidden( Component JavaDoc c, boolean hidden ) {
123         GridSplitCell cell = findCellFor( c );
124         if( null == cell ) {
125             throw new IllegalArgumentException JavaDoc( "Component not in hierarchy: " + c );
126         }
127         
128         if( cell.isHidden() == hidden )
129             return;
130         
131         GridSplitCell oldValue = new GridSplitCell( cell );
132                 
133         cell.setHidden( hidden );
134         
135         fireCellModified( oldValue, cell );
136     }
137     
138     /**
139      * @return True if given component is hidden in model's hierarchy.
140      */

141     public boolean isHidden( Component JavaDoc c ) {
142         GridSplitCell cell = findCellFor( c );
143         if( null == cell ) {
144             throw new IllegalArgumentException JavaDoc( "Component not in hierarchy: " + c );
145         }
146         
147         return cell.isHidden();
148     }
149     
150     public void addChangeListener( GridSplitModelListener listener ) {
151         synchronized( listeners ) {
152             listeners.add( listener );
153         }
154     }
155     
156     public void removeChangeListener( GridSplitModelListener listener ) {
157         synchronized( listeners ) {
158             listeners.remove( listener );
159         }
160     }
161     
162     void fireCellAdded( GridSplitCell cell ) {
163         GridSplitModelListener[] arr;
164         synchronized( listeners ) {
165             arr = new GridSplitModelListener[listeners.size()];
166             listeners.toArray( arr );
167         }
168         for( int i=0; i<arr.length; i++ ) {
169             arr[i].cellAdded( cell );
170         }
171     }
172     
173     void fireCellRemoved( GridSplitCell cell ) {
174         GridSplitModelListener[] arr;
175         synchronized( listeners ) {
176             arr = new GridSplitModelListener[listeners.size()];
177             listeners.toArray( arr );
178         }
179         for( int i=0; i<arr.length; i++ ) {
180             arr[i].cellRemoved( cell );
181         }
182     }
183     
184     void fireCellModified( GridSplitCell oldCell, GridSplitCell newCell ) {
185         GridSplitModelListener[] arr;
186         synchronized( listeners ) {
187             arr = new GridSplitModelListener[listeners.size()];
188             listeners.toArray( arr );
189         }
190         for( int i=0; i<arr.length; i++ ) {
191             arr[i].cellModified( oldCell, newCell );
192         }
193     }
194     
195     GridSplitCell findCellFor( Component JavaDoc c ) {
196         if( null == root )
197             return null;
198         return findCell( root, c );
199     }
200     
201     /**
202      * Collection of all components this model contains.
203      */

204     Collection JavaDoc getComponents() {
205         ArrayList JavaDoc res = new ArrayList JavaDoc();
206         collectComponents( getRootCell(), res );
207         return res;
208     }
209     
210     private void collectComponents( GridSplitCell cell, Collection JavaDoc components ) {
211         if( cell.isSplit() ) {
212             for( int i=0; i<cell.count(); i++ ) {
213                 collectComponents( cell.cellAt( i ), components );
214             }
215         } else {
216             components.add( cell.getComponent() );
217         }
218     }
219     
220     private static GridSplitCell findCell( GridSplitCell cell, Component JavaDoc c ) {
221         if( cell.isSplit() ) {
222             for( int i=0; i<cell.count(); i++ ) {
223                 GridSplitCell child = cell.cellAt( i );
224                 GridSplitCell res = findCell( child, c );
225                 if( null != res )
226                     return res;
227             }
228             return null;
229         } else {
230             if( c == cell.getComponent() )
231                 return cell;
232         }
233         return null;
234     }
235 }
236
Popular Tags