KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > motif > MotifSplitPaneDivider


1 /*
2  * @(#)MotifSplitPaneDivider.java 1.17 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.motif;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.JSplitPane JavaDoc;
13 import javax.swing.UIManager JavaDoc;
14 import javax.swing.plaf.basic.BasicSplitPaneUI JavaDoc;
15 import javax.swing.plaf.basic.BasicSplitPaneDivider JavaDoc;
16
17
18 /**
19  * Divider used for Motif split pane.
20  * <p>
21  * <strong>Warning:</strong>
22  * Serialized objects of this class will not be compatible with
23  * future Swing releases. The current serialization support is appropriate
24  * for short term storage or RMI between applications running the same
25  * version of Swing. A future release of Swing will provide support for
26  * long term persistence.
27  *
28  * @version 1.17 12/19/03
29  * @author Jeff Dinkins
30  */

31 public class MotifSplitPaneDivider extends BasicSplitPaneDivider JavaDoc
32 {
33     /**
34      * Default cursor, supers is package private, so we have to have one
35      * too.
36      */

37     private static final Cursor defaultCursor =
38                             Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
39
40
41     public static final int minimumThumbSize = 6;
42     public static final int defaultDividerSize = 18;
43
44     protected static final int pad = 6;
45
46     private int hThumbOffset = 30;
47     private int vThumbOffset = 40;
48     protected int hThumbWidth = 12;
49     protected int hThumbHeight = 18;
50     protected int vThumbWidth = 18;
51     protected int vThumbHeight = 12;
52
53     protected Color highlightColor;
54     protected Color shadowColor;
55     protected Color focusedColor;
56
57     /**
58      * Creates a new Motif SplitPaneDivider
59      */

60     public MotifSplitPaneDivider(BasicSplitPaneUI JavaDoc ui) {
61         super(ui);
62         highlightColor = UIManager.getColor("SplitPane.highlight");
63         shadowColor = UIManager.getColor("SplitPane.shadow");
64         focusedColor = UIManager.getColor("SplitPane.activeThumb");
65         setDividerSize(hThumbWidth + pad);
66     }
67
68     /**
69      * overrides to hardcode the size of the divider
70      * PENDING(jeff) - rewrite JSplitPane so that this ins't needed
71      */

72     public void setDividerSize(int newSize) {
73     Insets insets = getInsets();
74     int borderSize = 0;
75     if (getBasicSplitPaneUI().getOrientation() ==
76         JSplitPane.HORIZONTAL_SPLIT) {
77         if (insets != null) {
78         borderSize = insets.left + insets.right;
79         }
80     }
81     else if (insets != null) {
82         borderSize = insets.top + insets.bottom;
83     }
84         if (newSize < pad + minimumThumbSize + borderSize) {
85             setDividerSize(pad + minimumThumbSize + borderSize);
86         } else {
87             vThumbHeight = hThumbWidth = newSize - pad - borderSize;
88             super.setDividerSize(newSize);
89         }
90     }
91
92     /**
93       * Paints the divider.
94       */

95     // PENDING(jeff) - the thumb's location and size is currently hard coded.
96
// It should be dynamic.
97
public void paint(Graphics g) {
98         Color bgColor = getBackground();
99         Dimension size = getSize();
100
101         // fill
102
g.setColor(getBackground());
103         g.fillRect(0, 0, size.width, size.height);
104
105         if(getBasicSplitPaneUI().getOrientation() ==
106            JSplitPane.HORIZONTAL_SPLIT) {
107             int center = size.width/2;
108             int x = center - hThumbWidth/2;
109             int y = hThumbOffset;
110
111             // split line
112
g.setColor(shadowColor);
113             g.drawLine(center-1, 0, center-1, size.height);
114
115             g.setColor(highlightColor);
116             g.drawLine(center, 0, center, size.height);
117
118             // draw thumb
119
g.setColor((splitPane.hasFocus()) ? focusedColor :
120                                                 getBackground());
121             g.fillRect(x+1, y+1, hThumbWidth-2, hThumbHeight-1);
122
123             g.setColor(highlightColor);
124             g.drawLine(x, y, x+hThumbWidth-1, y); // top
125
g.drawLine(x, y+1, x, y+hThumbHeight-1); // left
126

127             g.setColor(shadowColor);
128             g.drawLine(x+1, y+hThumbHeight-1,
129                        x+hThumbWidth-1, y+hThumbHeight-1); // bottom
130
g.drawLine(x+hThumbWidth-1, y+1,
131                        x+hThumbWidth-1, y+hThumbHeight-2); // right
132

133         } else {
134             int center = size.height/2;
135             int x = size.width - vThumbOffset;
136             int y = size.height/2 - vThumbHeight/2;
137
138             // split line
139
g.setColor(shadowColor);
140             g.drawLine(0, center-1, size.width, center-1);
141
142             g.setColor(highlightColor);
143             g.drawLine(0, center, size.width, center);
144
145             // draw thumb
146
g.setColor((splitPane.hasFocus()) ? focusedColor :
147                                                 getBackground());
148             g.fillRect(x+1, y+1, vThumbWidth-1, vThumbHeight-1);
149
150             g.setColor(highlightColor);
151             g.drawLine(x, y, x+vThumbWidth, y); // top
152
g.drawLine(x, y+1, x, y+vThumbHeight); // left
153

154             g.setColor(shadowColor);
155             g.drawLine(x+1, y+vThumbHeight,
156                        x+vThumbWidth, y+vThumbHeight); // bottom
157
g.drawLine(x+vThumbWidth, y+1,
158                        x+vThumbWidth, y+vThumbHeight-1); // right
159
}
160         super.paint(g);
161
162     }
163
164     /**
165       * The minimums size is the same as the preferredSize
166       */

167     public Dimension getMinimumSize() {
168         return getPreferredSize();
169     }
170
171     /**
172      * Sets the SplitPaneUI that is using the receiver. This is completely
173      * overriden from super to create a different MouseHandler.
174      */

175     public void setBasicSplitPaneUI(BasicSplitPaneUI JavaDoc newUI) {
176         if (splitPane != null) {
177             splitPane.removePropertyChangeListener(this);
178            if (mouseHandler != null) {
179                splitPane.removeMouseListener(mouseHandler);
180                splitPane.removeMouseMotionListener(mouseHandler);
181            removeMouseListener(mouseHandler);
182            removeMouseMotionListener(mouseHandler);
183                mouseHandler = null;
184            }
185         }
186         splitPaneUI = newUI;
187         if (newUI != null) {
188             splitPane = newUI.getSplitPane();
189             if (splitPane != null) {
190                 if (mouseHandler == null) mouseHandler=new MotifMouseHandler();
191                 splitPane.addMouseListener(mouseHandler);
192                 splitPane.addMouseMotionListener(mouseHandler);
193         addMouseListener(mouseHandler);
194         addMouseMotionListener(mouseHandler);
195                 splitPane.addPropertyChangeListener(this);
196                 if (splitPane.isOneTouchExpandable()) {
197                     oneTouchExpandableChanged();
198                 }
199             }
200         }
201         else {
202             splitPane = null;
203         }
204     }
205
206     /**
207      * Returns true if the point at <code>x</code>, <code>y</code>
208      * is inside the thumb.
209      */

210     private boolean isInThumb(int x, int y) {
211         Dimension size = getSize();
212     int thumbX;
213     int thumbY;
214     int thumbWidth;
215     int thumbHeight;
216
217     if (getBasicSplitPaneUI().getOrientation() ==
218         JSplitPane.HORIZONTAL_SPLIT) {
219             int center = size.width/2;
220             thumbX = center - hThumbWidth/2;
221             thumbY = hThumbOffset;
222         thumbWidth = hThumbWidth;
223         thumbHeight = hThumbHeight;
224     }
225     else {
226             int center = size.height/2;
227             thumbX = size.width - vThumbOffset;
228             thumbY = size.height/2 - vThumbHeight/2;
229         thumbWidth = vThumbWidth;
230         thumbHeight = vThumbHeight;
231     }
232     return (x >= thumbX && x < (thumbX + thumbWidth) &&
233         y >= thumbY && y < (thumbY + thumbHeight));
234     }
235
236     //
237
// Two methods are exposed so that MotifMouseHandler can see the
238
// superclass protected ivars
239
//
240

241     private DragController getDragger() {
242     return dragger;
243     }
244
245     private JSplitPane JavaDoc getSplitPane() {
246     return splitPane;
247     }
248
249
250     /**
251      * MouseHandler is subclassed to only pass off to super if the mouse
252      * is in the thumb. Motif only allows dragging when the thumb is clicked
253      * in.
254      */

255     private class MotifMouseHandler extends MouseHandler {
256         public void mousePressed(MouseEvent e) {
257         // Constrain the mouse pressed to the thumb.
258
if (e.getSource() == MotifSplitPaneDivider.this &&
259         getDragger() == null && getSplitPane().isEnabled() &&
260         isInThumb(e.getX(), e.getY())) {
261         super.mousePressed(e);
262         }
263     }
264
265         public void mouseMoved(MouseEvent e) {
266         if (getDragger() != null) {
267         return;
268         }
269         if (!isInThumb(e.getX(), e.getY())) {
270         if (getCursor() != defaultCursor) {
271             setCursor(defaultCursor);
272         }
273         return;
274         }
275         super.mouseMoved(e);
276     }
277     }
278 }
279
Popular Tags