KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalSplitPaneDivider


1 /*
2  * @(#)MetalSplitPaneDivider.java 1.21 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 javax.swing.plaf.metal;
9
10 import java.awt.*;
11 import javax.swing.*;
12 import javax.swing.border.*;
13 import javax.swing.plaf.basic.*;
14
15
16 /**
17  * Metal's split pane divider
18  * <p>
19  * <strong>Warning:</strong>
20  * Serialized objects of this class will not be compatible with
21  * future Swing releases. The current serialization support is
22  * appropriate for short term storage or RMI between applications running
23  * the same version of Swing. As of 1.4, support for long term storage
24  * of all JavaBeans<sup><font size="-2">TM</font></sup>
25  * has been added to the <code>java.beans</code> package.
26  * Please see {@link java.beans.XMLEncoder}.
27  *
28  * @version 1.21 12/19/03
29  * @author Steve Wilson
30  * @author Ralph kar
31  */

32 class MetalSplitPaneDivider extends BasicSplitPaneDivider
33 {
34     private MetalBumps JavaDoc bumps = new MetalBumps JavaDoc(10, 10,
35                  MetalLookAndFeel.getControlHighlight(),
36                  MetalLookAndFeel.getControlDarkShadow(),
37                  MetalLookAndFeel.getControl() );
38
39     private MetalBumps JavaDoc focusBumps = new MetalBumps JavaDoc(10, 10,
40                  MetalLookAndFeel.getPrimaryControlHighlight(),
41                  MetalLookAndFeel.getPrimaryControlDarkShadow(),
42                  UIManager.getColor("SplitPane.dividerFocusColor"));
43
44     private int inset = 2;
45
46     private Color controlColor = MetalLookAndFeel.getControl();
47     private Color primaryControlColor = UIManager.getColor(
48                                 "SplitPane.dividerFocusColor");
49
50     public MetalSplitPaneDivider(BasicSplitPaneUI ui) {
51         super(ui);
52     }
53
54     public void paint(Graphics g) {
55     MetalBumps JavaDoc usedBumps;
56     if (splitPane.hasFocus()) {
57         usedBumps = focusBumps;
58         g.setColor(primaryControlColor);
59     }
60     else {
61         usedBumps = bumps;
62         g.setColor(controlColor);
63     }
64     Rectangle clip = g.getClipBounds();
65     Insets insets = getInsets();
66     g.fillRect(clip.x, clip.y, clip.width, clip.height);
67         Dimension size = getSize();
68         size.width -= inset * 2;
69         size.height -= inset * 2;
70     int drawX = inset;
71     int drawY = inset;
72     if (insets != null) {
73         size.width -= (insets.left + insets.right);
74         size.height -= (insets.top + insets.bottom);
75         drawX += insets.left;
76         drawY += insets.top;
77     }
78         usedBumps.setBumpArea(size);
79         usedBumps.paintIcon(this, g, drawX, drawY);
80         super.paint(g);
81     }
82
83     /**
84      * Creates and return an instance of JButton that can be used to
85      * collapse the left component in the metal split pane.
86      */

87     protected JButton createLeftOneTouchButton() {
88         JButton b = new JButton() {
89             // Sprite buffer for the arrow image of the left button
90
int[][] buffer = {{0, 0, 0, 2, 2, 0, 0, 0, 0},
91                                   {0, 0, 2, 1, 1, 1, 0, 0, 0},
92                                   {0, 2, 1, 1, 1, 1, 1, 0, 0},
93                                   {2, 1, 1, 1, 1, 1, 1, 1, 0},
94                                   {0, 3, 3, 3, 3, 3, 3, 3, 3}};
95
96             public void setBorder(Border b) {
97             }
98
99             public void paint(Graphics g) {
100                 JSplitPane splitPane = getSplitPaneFromSuper();
101                 if(splitPane != null) {
102                     int oneTouchSize = getOneTouchSizeFromSuper();
103                     int orientation = getOrientationFromSuper();
104                     int blockSize = Math.min(getDividerSize(),
105                                                      oneTouchSize);
106
107                     // Initialize the color array
108
Color[] colors = {
109                             this.getBackground(),
110                             MetalLookAndFeel.getPrimaryControlDarkShadow(),
111                             MetalLookAndFeel.getPrimaryControlInfo(),
112                             MetalLookAndFeel.getPrimaryControlHighlight()};
113
114                     // Fill the background first ...
115
g.setColor(this.getBackground());
116                     if (isOpaque()) {
117                         g.fillRect(0, 0, this.getWidth(),
118                                    this.getHeight());
119                     }
120
121                     // ... then draw the arrow.
122
if (getModel().isPressed()) {
123                             // Adjust color mapping for pressed button state
124
colors[1] = colors[2];
125                     }
126                     if(orientation == JSplitPane.VERTICAL_SPLIT) {
127                             // Draw the image for a vertical split
128
for (int i=1; i<=buffer[0].length; i++) {
129                                     for (int j=1; j<blockSize; j++) {
130                                             if (buffer[j-1][i-1] == 0) {
131                                                     continue;
132                                             }
133                                             else {
134                                                 g.setColor(
135                                                     colors[buffer[j-1][i-1]]);
136                                             }
137                                             g.drawLine(i, j, i, j);
138                                     }
139                             }
140                     }
141                     else {
142                             // Draw the image for a horizontal split
143
// by simply swaping the i and j axis.
144
// Except the drawLine() call this code is
145
// identical to the code block above. This was done
146
// in order to remove the additional orientation
147
// check for each pixel.
148
for (int i=1; i<=buffer[0].length; i++) {
149                                     for (int j=1; j<blockSize; j++) {
150                                             if (buffer[j-1][i-1] == 0) {
151                                                     // Nothing needs
152
// to be drawn
153
continue;
154                                             }
155                                             else {
156                                                     // Set the color from the
157
// color map
158
g.setColor(
159                                                     colors[buffer[j-1][i-1]]);
160                                             }
161                                             // Draw a pixel
162
g.drawLine(j, i, j, i);
163                                     }
164                             }
165                     }
166                 }
167             }
168
169         // Don't want the button to participate in focus traversable.
170
public boolean isFocusTraversable() {
171         return false;
172         }
173         };
174         b.setRequestFocusEnabled(false);
175     b.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
176         b.setFocusPainted(false);
177         b.setBorderPainted(false);
178         maybeMakeButtonOpaque(b);
179         return b;
180     }
181
182     /**
183      * If necessary <code>c</code> is made opaque.
184      */

185     private void maybeMakeButtonOpaque(JComponent c) {
186         Object JavaDoc opaque = UIManager.get("SplitPane.oneTouchButtonsOpaque");
187         if (opaque != null) {
188             c.setOpaque(((Boolean JavaDoc)opaque).booleanValue());
189         }
190     }
191
192     /**
193      * Creates and return an instance of JButton that can be used to
194      * collapse the right component in the metal split pane.
195      */

196     protected JButton createRightOneTouchButton() {
197         JButton b = new JButton() {
198             // Sprite buffer for the arrow image of the right button
199
int[][] buffer = {{2, 2, 2, 2, 2, 2, 2, 2},
200                                   {0, 1, 1, 1, 1, 1, 1, 3},
201                                   {0, 0, 1, 1, 1, 1, 3, 0},
202                                   {0, 0, 0, 1, 1, 3, 0, 0},
203                                   {0, 0, 0, 0, 3, 0, 0, 0}};
204
205             public void setBorder(Border border) {
206             }
207
208             public void paint(Graphics g) {
209                 JSplitPane splitPane = getSplitPaneFromSuper();
210                 if(splitPane != null) {
211                     int oneTouchSize = getOneTouchSizeFromSuper();
212                     int orientation = getOrientationFromSuper();
213                     int blockSize = Math.min(getDividerSize(),
214                                                      oneTouchSize);
215
216                     // Initialize the color array
217
Color[] colors = {
218                             this.getBackground(),
219                             MetalLookAndFeel.getPrimaryControlDarkShadow(),
220                             MetalLookAndFeel.getPrimaryControlInfo(),
221                             MetalLookAndFeel.getPrimaryControlHighlight()};
222
223                     // Fill the background first ...
224
g.setColor(this.getBackground());
225                     if (isOpaque()) {
226                         g.fillRect(0, 0, this.getWidth(),
227                                    this.getHeight());
228                     }
229
230                     // ... then draw the arrow.
231
if (getModel().isPressed()) {
232                             // Adjust color mapping for pressed button state
233
colors[1] = colors[2];
234                     }
235                     if(orientation == JSplitPane.VERTICAL_SPLIT) {
236                             // Draw the image for a vertical split
237
for (int i=1; i<=buffer[0].length; i++) {
238                                     for (int j=1; j<blockSize; j++) {
239                                             if (buffer[j-1][i-1] == 0) {
240                                                     continue;
241                                             }
242                                             else {
243                                                 g.setColor(
244                                                     colors[buffer[j-1][i-1]]);
245                                             }
246                                             g.drawLine(i, j, i, j);
247                                     }
248                             }
249                     }
250                     else {
251                             // Draw the image for a horizontal split
252
// by simply swaping the i and j axis.
253
// Except the drawLine() call this code is
254
// identical to the code block above. This was done
255
// in order to remove the additional orientation
256
// check for each pixel.
257
for (int i=1; i<=buffer[0].length; i++) {
258                                     for (int j=1; j<blockSize; j++) {
259                                             if (buffer[j-1][i-1] == 0) {
260                                                     // Nothing needs
261
// to be drawn
262
continue;
263                                             }
264                                             else {
265                                                     // Set the color from the
266
// color map
267
g.setColor(
268                                                     colors[buffer[j-1][i-1]]);
269                                             }
270                                             // Draw a pixel
271
g.drawLine(j, i, j, i);
272                                     }
273                             }
274                     }
275                 }
276             }
277
278         // Don't want the button to participate in focus traversable.
279
public boolean isFocusTraversable() {
280         return false;
281         }
282         };
283     b.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
284         b.setFocusPainted(false);
285         b.setBorderPainted(false);
286         b.setRequestFocusEnabled(false);
287         maybeMakeButtonOpaque(b);
288         return b;
289     }
290
291     /**
292      * Used to layout a MetalSplitPaneDivider. Layout for the divider
293      * involves appropriately moving the left/right buttons around.
294      * <p>
295      * This class should be treated as a &quot;protected&quot; inner class.
296      * Instantiate it only within subclasses of MetalSplitPaneDivider.
297      */

298     public class MetalDividerLayout implements LayoutManager {
299
300         // NOTE NOTE NOTE NOTE NOTE
301
// This class is no longer used, the functionality has
302
// been rolled into BasicSplitPaneDivider.DividerLayout as a
303
// defaults property
304

305         public void layoutContainer(Container c) {
306             JButton leftButton = getLeftButtonFromSuper();
307             JButton rightButton = getRightButtonFromSuper();
308             JSplitPane splitPane = getSplitPaneFromSuper();
309             int orientation = getOrientationFromSuper();
310             int oneTouchSize = getOneTouchSizeFromSuper();
311             int oneTouchOffset = getOneTouchOffsetFromSuper();
312         Insets insets = getInsets();
313
314             // This layout differs from the one used in BasicSplitPaneDivider.
315
// It does not center justify the oneTouchExpadable buttons.
316
// This was necessary in order to meet the spec of the Metal
317
// splitpane divider.
318
if (leftButton != null && rightButton != null &&
319                 c == MetalSplitPaneDivider.this) {
320                 if (splitPane.isOneTouchExpandable()) {
321                     if (orientation == JSplitPane.VERTICAL_SPLIT) {
322             int extraY = (insets != null) ? insets.top : 0;
323             int blockSize = getDividerSize();
324
325             if (insets != null) {
326                 blockSize -= (insets.top + insets.bottom);
327             }
328             blockSize = Math.min(blockSize, oneTouchSize);
329                         leftButton.setBounds(oneTouchOffset, extraY,
330                                              blockSize * 2, blockSize);
331                         rightButton.setBounds(oneTouchOffset +
332                                               oneTouchSize * 2, extraY,
333                                               blockSize * 2, blockSize);
334                     }
335                     else {
336             int blockSize = getDividerSize();
337             int extraX = (insets != null) ? insets.left : 0;
338
339             if (insets != null) {
340                 blockSize -= (insets.left + insets.right);
341             }
342             blockSize = Math.min(blockSize, oneTouchSize);
343                         leftButton.setBounds(extraX, oneTouchOffset,
344                                              blockSize, blockSize * 2);
345                         rightButton.setBounds(extraX, oneTouchOffset +
346                                               oneTouchSize * 2, blockSize,
347                                               blockSize * 2);
348                     }
349                 }
350                 else {
351                     leftButton.setBounds(-5, -5, 1, 1);
352                     rightButton.setBounds(-5, -5, 1, 1);
353                 }
354             }
355         }
356
357         public Dimension minimumLayoutSize(Container c) {
358             return new Dimension(0,0);
359         }
360
361         public Dimension preferredLayoutSize(Container c) {
362             return new Dimension(0, 0);
363         }
364
365         public void removeLayoutComponent(Component c) {}
366
367         public void addLayoutComponent(String JavaDoc string, Component c) {}
368     }
369
370     /*
371      * The following methods only exist in order to be able to access protected
372      * members in the superclass, because these are otherwise not available
373      * in any inner class.
374      */

375
376     int getOneTouchSizeFromSuper() {
377         return super.ONE_TOUCH_SIZE;
378     }
379
380     int getOneTouchOffsetFromSuper() {
381         return super.ONE_TOUCH_OFFSET;
382     }
383
384     int getOrientationFromSuper() {
385         return super.orientation;
386     }
387
388     JSplitPane getSplitPaneFromSuper() {
389         return super.splitPane;
390     }
391
392     JButton getLeftButtonFromSuper() {
393         return super.leftButton;
394     }
395
396     JButton getRightButtonFromSuper() {
397         return super.rightButton;
398     }
399 }
400
Popular Tags