KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > MultiSplitDivider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.view.ui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Point JavaDoc;
26 import java.awt.Rectangle JavaDoc;
27 import java.util.Locale JavaDoc;
28 import javax.accessibility.Accessible JavaDoc;
29 import javax.accessibility.AccessibleContext JavaDoc;
30 import javax.accessibility.AccessibleRole JavaDoc;
31 import javax.accessibility.AccessibleState JavaDoc;
32 import javax.accessibility.AccessibleStateSet JavaDoc;
33 import javax.accessibility.AccessibleValue JavaDoc;
34
35
36 /**
37  * Wrapper class for MultiSplitPane's split divider rectangle.
38  */

39 public class MultiSplitDivider implements Accessible JavaDoc {
40     
41     MultiSplitPane splitPane;
42     Rectangle JavaDoc rect = new Rectangle JavaDoc();
43     MultiSplitCell first;
44     MultiSplitCell second;
45     
46     Point JavaDoc currentDragLocation;
47     int dragMin;
48     int dragMax;
49     int cursorPositionCompensation;
50     
51     private AccessibleContext JavaDoc accessibleContext;
52
53     public MultiSplitDivider( MultiSplitPane parent, MultiSplitCell first, MultiSplitCell second ) {
54         assert null != parent;
55         assert null != first;
56         assert null != second;
57         this.splitPane = parent;
58         this.first = first;
59         this.second = second;
60         
61         reshape();
62     }
63
64     boolean isHorizontal() {
65         return splitPane.isHorizontalSplit();
66     }
67
68     boolean isVertical() {
69         return splitPane.isVerticalSplit();
70     }
71     
72     int getDividerSize() {
73         return splitPane.getDividerSize();
74     }
75
76     boolean containsPoint( Point JavaDoc p ) {
77         return rect.contains( p );
78     }
79     
80     void paint( Graphics JavaDoc g ) {
81         //the split bar does not paint anything when not being dragged
82
//JPanel's background color is used as default
83

84         if( null != currentDragLocation ) {
85             Color JavaDoc oldColor = g.getColor();
86             g.setColor( Color.BLACK );
87             if( isHorizontal() ) {
88                 if( currentDragLocation.x != rect.x ) {
89                     g.fillRect( currentDragLocation.x, rect.y, rect.width, rect.height );
90                 }
91             } else {
92                 if( currentDragLocation.y != rect.y ) {
93                     g.fillRect( rect.x, currentDragLocation.y, rect.width, rect.height );
94                 }
95             }
96             g.setColor( oldColor );
97         }
98     }
99     
100     void startDragging( Point JavaDoc p ) {
101         currentDragLocation = new Point JavaDoc( rect.x, rect.y );
102
103         if( isHorizontal() )
104             cursorPositionCompensation = p.x - rect.x;
105         else
106             cursorPositionCompensation = p.y - rect.y;
107         
108         initDragMinMax();
109     }
110     
111     void dragTo( Point JavaDoc p ) {
112         if( isHorizontal() ) {
113             if( p.x < dragMin )
114                 p.x = dragMin;
115             if( p.x > dragMax )
116                 p.x = dragMax;
117         } else {
118             if( p.y < dragMin )
119                 p.y = dragMin;
120             if( p.y > dragMax )
121                 p.y = dragMax;
122         }
123         
124         Point JavaDoc prevDragLocation = currentDragLocation;
125         currentDragLocation = p;
126         
127         repaintSplitPane( prevDragLocation );
128         repaintSplitPane( currentDragLocation );
129     }
130     
131     private void repaintSplitPane( Point JavaDoc location ) {
132         if( isHorizontal() ) {
133             splitPane.repaint( location.x, rect.y, rect.width, rect.height );
134         } else {
135             splitPane.repaint( rect.x, location.y, rect.width, rect.height );
136         }
137     }
138     
139     void finishDraggingTo( Point JavaDoc p ) {
140         if( isHorizontal() ) {
141             p.x -= cursorPositionCompensation;
142             if( p.x < dragMin )
143                 p.x = dragMin;
144             if( p.x > dragMax )
145                 p.x = dragMax;
146             
147             if( p.x == rect.x ) {
148                 //split bar position didn't change
149
return;
150             }
151         } else {
152             p.y -= cursorPositionCompensation;
153             if( p.y < dragMin )
154                 p.y = dragMin;
155             if( p.y > dragMax )
156                 p.y = dragMax;
157
158             if( p.y == rect.y ) {
159                 //split bar position didn't change
160
return;
161             }
162         }
163         currentDragLocation = null;
164     
165         int dividerSize = getDividerSize();
166         
167         if( isHorizontal() ) {
168             int delta = p.x - rect.x;
169             int x = first.getLocation();
170             int y = 0;
171             int width = first.getSize() + delta;
172             int height = rect.height;
173             first.layout( x, y, width, height );
174             
175             x = second.getLocation() + delta;
176             width = second.getSize() - delta;
177             second.layout( x, y, width, height );
178             
179             rect.x = p.x;
180         } else {
181             int delta = p.y - rect.y;
182             int x = 0;
183             int y = first.getLocation();
184             int width = rect.width;
185             int height = first.getSize() + delta;
186             first.layout( x, y, width, height );
187             
188             y = second.getLocation() + delta;
189             height = second.getSize() - delta;
190             second.layout( x, y, width, height );
191
192             rect.y = p.y;
193         }
194         splitPane.splitterMoved();//invalidate();
195
}
196     
197     private void initDragMinMax() {
198         int dividerSize = getDividerSize();
199         int firstSize = first.getSize();
200         int secondSize = second.getSize();
201         int firstMinSize = first.getMinimumSize();
202         int secondMinSize = second.getMinimumSize();
203         
204         if( isHorizontal() ) {
205             dragMin = rect.x;
206             dragMax = rect.x;
207         } else {
208             dragMin = rect.y;
209             dragMax = rect.y;
210         }
211             
212         if( firstSize >= firstMinSize ) {
213             dragMin -= firstSize-firstMinSize;
214         }
215         if( secondSize >= secondMinSize ) {
216             dragMax += secondSize-secondMinSize;
217         }
218     }
219     
220     void reshape() {
221         Dimension JavaDoc d = splitPane.getSize();
222         int location = second.getLocation();
223
224         if( isHorizontal() ) {
225             rect.x = location-getDividerSize();
226             rect.y = 0;
227             rect.width = getDividerSize();
228             rect.height = d.height;
229         } else {
230             rect.x = 0;
231             rect.y = location-getDividerSize();
232             rect.width = d.width;
233             rect.height = getDividerSize();
234         }
235     }
236     
237     // *************************************************************************
238
// Accessibility
239

240     public AccessibleContext JavaDoc getAccessibleContext() {
241         if( null == accessibleContext ) {
242             accessibleContext = new AccessibleMultiSplitDivider();
243         }
244         return accessibleContext;
245     }
246     
247     protected class AccessibleMultiSplitDivider extends AccessibleContext JavaDoc
248         implements AccessibleValue JavaDoc {
249         
250         public AccessibleMultiSplitDivider() {
251             setAccessibleParent( splitPane );
252         }
253         
254         public Accessible JavaDoc getAccessibleChild(int i) {
255             return null;
256         }
257
258         public int getAccessibleChildrenCount() {
259             return 0;
260         }
261
262         public int getAccessibleIndexInParent() {
263             return splitPane.getDividerAccessibleIndex( MultiSplitDivider.this );
264         }
265
266         public AccessibleRole JavaDoc getAccessibleRole() {
267             return AccessibleRole.SPLIT_PANE;
268         }
269
270         public AccessibleStateSet JavaDoc getAccessibleStateSet() {
271             AccessibleStateSet JavaDoc stateSet = new AccessibleStateSet JavaDoc();
272             if( isHorizontal() ) {
273                 stateSet.add( AccessibleState.HORIZONTAL );
274             } else {
275                 stateSet.add( AccessibleState.VERTICAL );
276             }
277             return stateSet;
278         }
279
280         public Locale JavaDoc getLocale() throws java.awt.IllegalComponentStateException JavaDoc {
281             return Locale.getDefault();
282         }
283         
284         public boolean setCurrentAccessibleValue(Number JavaDoc n) {
285             initDragMinMax();
286             int value = n.intValue();
287             if( value < dragMin || value > dragMax ) {
288                 return false;
289             }
290             if( isHorizontal() ) {
291                 finishDraggingTo( new Point JavaDoc( value, 0 ) );
292             } else {
293                 finishDraggingTo( new Point JavaDoc( 0, value ) );
294             }
295             return true;
296         }
297
298         public Number JavaDoc getMinimumAccessibleValue() {
299             initDragMinMax();
300             return Integer.valueOf( dragMin );
301         }
302
303         public Number JavaDoc getMaximumAccessibleValue() {
304             initDragMinMax();
305             return Integer.valueOf( dragMax );
306         }
307
308         public Number JavaDoc getCurrentAccessibleValue() {
309             if( isHorizontal() )
310                 return Integer.valueOf( rect.x );
311             else
312                 return Integer.valueOf( rect.y );
313         }
314
315         public AccessibleValue JavaDoc getAccessibleValue() {
316             return this;
317         }
318     } //end of AccessibleMultiSplitDivider inner class
319
}
320
Popular Tags