KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > piagetproject > gridsplit > GridSplitDivider


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

6
7 package org.netbeans.modules.piagetproject.gridsplit;
8 import java.awt.Color JavaDoc;
9 import java.awt.Dimension JavaDoc;
10 import java.awt.Graphics JavaDoc;
11 import java.awt.Point JavaDoc;
12 import java.awt.Rectangle JavaDoc;
13 import java.awt.event.MouseEvent JavaDoc;
14 import javax.swing.BorderFactory JavaDoc;
15 import javax.swing.border.Border JavaDoc;
16
17 /**
18  *
19  * @author aubrecht
20  */

21 public class GridSplitDivider {
22     
23     GridSplitPane splitPane;
24     Rectangle JavaDoc rect = new Rectangle JavaDoc();
25     GridSplitCell first;
26     GridSplitCell second;
27     int size;
28     static Border JavaDoc border = BorderFactory.createRaisedBevelBorder();
29     
30     Point JavaDoc currentDragLocation;
31     int dragMin;
32     int dragMax;
33
34     public GridSplitDivider( GridSplitPane parent, int size, GridSplitCell first, GridSplitCell second ) {
35         assert null != parent;
36         assert null != first;
37         assert null != second;
38         this.splitPane = parent;
39         this.size = size;
40         this.first = first;
41         this.second = second;
42     }
43
44     boolean isHorizontal() {
45         return first.getParent().isHorizontalSplitter();
46     }
47
48     boolean isVertical() {
49         return first.getParent().isVerticalSplitter();
50     }
51     
52     int getDividerSize() {
53         if( isHorizontal() )
54             return rect.width;
55         return rect.height;
56     }
57
58     boolean containsPoint( Point JavaDoc p ) {
59         return rect.contains( p );
60     }
61     
62     int getSize() {
63         return size;
64     }
65     
66     void paint( Graphics JavaDoc g ) {
67         if( first.getParent().isCollapsed() )
68             return;
69         
70         border.paintBorder( splitPane, g, rect.x, rect.y, rect.width, rect.height );
71         
72         paintCollapseButtons( g );
73         
74         if( null != currentDragLocation ) {
75             Color JavaDoc oldColor = g.getColor();
76             g.setColor( Color.BLACK );
77             if( isHorizontal() ) {
78                 if( currentDragLocation.x != rect.x ) {
79                     g.fillRect( currentDragLocation.x, rect.y, rect.width, rect.height );
80                 }
81             } else {
82                 if( currentDragLocation.y != rect.y ) {
83                     g.fillRect( rect.x, currentDragLocation.y, rect.width, rect.height );
84                 }
85             }
86             g.setColor( oldColor );
87         }
88     }
89     
90     void paintCollapseButtons( Graphics JavaDoc g ) {
91         Color JavaDoc oldColor = g.getColor();
92         
93         g.setColor( Color.RED );
94         Rectangle JavaDoc collapseRect = getCollapseFirstRect();
95         g.fillRect( collapseRect.x, collapseRect.y, collapseRect.width, collapseRect.height );
96
97         g.setColor( Color.GREEN );
98         collapseRect = getCollapseSecondRect();
99         g.fillRect( collapseRect.x, collapseRect.y, collapseRect.width, collapseRect.height );
100         
101         g.setColor( oldColor );
102     }
103     
104     Rectangle JavaDoc getCollapseFirstRect() {
105         if( first.getParent().isCollapsed() )
106             return new Rectangle JavaDoc( 0,0,0,0 );
107         Rectangle JavaDoc res = new Rectangle JavaDoc( rect );
108         if( isHorizontal() ) {
109             res.y += res.width;
110             res.height = 2*res.width;
111         } else {
112             res.x += res.height;
113             res.width = 2*res.height;
114         }
115         return res;
116     }
117     
118     Rectangle JavaDoc getCollapseSecondRect() {
119         if( first.getParent().isCollapsed() )
120             return new Rectangle JavaDoc( 0,0,0,0 );
121         Rectangle JavaDoc res = getCollapseFirstRect();
122         if( isHorizontal() ) {
123             res.y += 4*res.width;
124         } else {
125             res.x += 4*res.height;
126         }
127         return res;
128     }
129     
130     void startDragging( Point JavaDoc p ) {
131         currentDragLocation = new Point JavaDoc( rect.x, rect.y );
132         
133         initDragMinMax();
134     }
135     
136     void dragTo( Point JavaDoc p ) {
137         if( isHorizontal() ) {
138             if( p.x < dragMin )
139                 p.x = dragMin;
140             if( p.x > dragMax )
141                 p.x = dragMax;
142         } else {
143             if( p.y < dragMin )
144                 p.y = dragMin;
145             if( p.y > dragMax )
146                 p.y = dragMax;
147         }
148         
149         Point JavaDoc prevDragLocation = currentDragLocation;
150         currentDragLocation = p;
151         
152         repaintSplitPane( prevDragLocation );
153         repaintSplitPane( currentDragLocation );
154     }
155     
156     private void repaintSplitPane( Point JavaDoc location ) {
157         if( isHorizontal() ) {
158             splitPane.repaint( location.x, rect.y, rect.width, rect.height );
159         } else {
160             splitPane.repaint( rect.x, location.y, rect.width, rect.height );
161         }
162     }
163     
164     void finishDraggingTo( Point JavaDoc p ) {
165         if( isHorizontal() ) {
166             if( p.x < dragMin )
167                 p.x = dragMin;
168             if( p.x > dragMax )
169                 p.x = dragMax;
170         } else {
171             if( p.y < dragMin )
172                 p.y = dragMin;
173             if( p.y > dragMax )
174                 p.y = dragMax;
175         }
176         currentDragLocation = null;
177     
178         int dividerSize = getDividerSize();
179         first.setCollapsed( false );
180         second.setCollapsed( false );
181         
182         if( isHorizontal() ) {
183             int delta = p.x - rect.x;
184             Dimension JavaDoc d = first.getDimension( dividerSize );
185             Point JavaDoc location = first.getLocation();
186             d.width += delta;
187             d.height = rect.height;
188             first.resize( d.width, d.height, dividerSize );
189             first.setLocation( location.x, location.y, dividerSize );
190             
191             d = second.getDimension( dividerSize );
192             location = second.getLocation();
193             d.width -= delta;
194             d.height = rect.height;
195             location.x += delta;
196             second.resize( d.width, d.height, dividerSize );
197             second.setLocation( location.x, location.y, dividerSize );
198             
199             rect.x = p.x;
200         } else {
201             int delta = p.y - rect.y;
202             Dimension JavaDoc d = first.getDimension( dividerSize );
203             Point JavaDoc location = first.getLocation();
204             d.height += delta;
205             d.width = rect.width;
206             first.resize( d.width, d.height, dividerSize );
207             first.setLocation( location.x, location.y, dividerSize );
208             
209             d = second.getDimension( dividerSize );
210             location = second.getLocation();
211             d.height -= delta;
212             d.width = rect.width;
213             location.y += delta;
214             second.resize( d.width, d.height, dividerSize );
215             second.setLocation( location.x, location.y, dividerSize );
216
217             rect.y = p.y;
218         }
219         splitPane.validate();//invalidate();
220
}
221     
222     private void initDragMinMax() {
223         Point JavaDoc firstLocation = first.getLocation();
224         int dividerSize = getDividerSize();
225         Dimension JavaDoc firstDim = first.getDimension( dividerSize );
226         Dimension JavaDoc secondDim = second.getDimension( dividerSize );
227         Dimension JavaDoc firstMinSize = first.getMinimumSize( dividerSize );
228         Dimension JavaDoc secondMinSize = second.getMinimumSize( dividerSize );
229         
230         if( isHorizontal() ) {
231             dragMin = rect.x;
232             dragMax = rect.x;
233             
234             if( firstDim.width >= firstMinSize.width ) {
235                 dragMin -= firstDim.width-firstMinSize.width;//-firstLocation.x;
236
}
237             if( secondDim.width >= secondMinSize.width ) {
238                 dragMax += secondDim.width-secondMinSize.width;
239             }
240         } else {
241             dragMin = rect.y;
242             dragMax = rect.y;
243             if( firstDim.height >= firstMinSize.height ) {
244                 dragMin -= firstDim.height-firstMinSize.height;//-firstLocation.y;
245
}
246             if( secondDim.height >= secondMinSize.height ) {
247                 dragMax += secondDim.height-secondMinSize.height;
248             }
249         }
250     }
251     
252     void reshape() {
253         if( first.getParent().isCollapsed() ) {
254             rect.x = 0;
255             rect.y = 0;
256             rect.width = 0;
257             rect.height = 0;
258             return;
259         }
260         Dimension JavaDoc d = first.getParent().getDimension( getDividerSize() );
261         Point JavaDoc location = second.getLocation();
262
263         if( isHorizontal() ) {
264             rect.x = location.x-getSize();
265             rect.y = location.y;
266             rect.width = getSize();
267             rect.height = d.height;
268         } else {
269             rect.x = location.x;
270             rect.y = location.y-getSize();
271             rect.width = d.width;
272             rect.height = getSize();
273         }
274     }
275     
276     boolean isOnCollapseButton( Point JavaDoc p ) {
277         return getCollapseFirstRect().contains( p ) || getCollapseSecondRect().contains( p );
278     }
279     
280     boolean collapse( Point JavaDoc p ) {
281         Rectangle JavaDoc firstRect = getCollapseFirstRect();
282         Rectangle JavaDoc secondRect = getCollapseSecondRect();
283         
284         Dimension JavaDoc d = first.getParent().getDimension( getDividerSize() );
285         Point JavaDoc location = first.getParent().getLocation();
286         boolean res = false;
287         if( firstRect.contains( p ) ) {
288             first.setCollapsed( !first.isCollapsed() );
289             second.setCollapsed( false );
290             res = true;
291         }
292         if( secondRect.contains( p ) ) {
293             second.setCollapsed( !second.isCollapsed() );
294             first.setCollapsed( false );
295             res = true;
296         }
297         if( res ) {
298 // first.getParent().resize( d.width, d.height, getDividerSize() );
299
// first.getParent().setLocation( location.x, location.y, getDividerSize() );
300
splitPane.invalidate();
301         }
302         return res;
303     }
304 }
305
Popular Tags