KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > windows > WindowsSplitPaneDivider


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.windows;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Component JavaDoc;
35 import java.awt.Container JavaDoc;
36 import java.awt.Dimension JavaDoc;
37 import java.awt.Graphics JavaDoc;
38 import java.awt.LayoutManager JavaDoc;
39
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JSplitPane JavaDoc;
42 import javax.swing.UIManager JavaDoc;
43 import javax.swing.border.Border JavaDoc;
44 import javax.swing.plaf.basic.BasicSplitPaneUI JavaDoc;
45
46 import com.jgoodies.looks.plastic.PlasticSplitPaneUI;
47
48 /**
49  * Paints nicely rendered one touch triangles.
50  *
51  * @author Karsten Lentzsch
52  * @version $Revision: 1.2 $
53  *
54  * @see PlasticSplitPaneUI
55  */

56 final class WindowsSplitPaneDivider extends com.sun.java.swing.plaf.windows.WindowsSplitPaneDivider {
57
58     private static final int EXT_ONE_TOUCH_SIZE = 5;
59     private static final int EXT_ONE_TOUCH_OFFSET = 2;
60     private static final int EXT_BLOCKSIZE = 6;
61
62     /**
63      * Used to lay out a WindowsSplitPaneDivider. Layout for the divider
64      * involves appropriately moving the left/right buttons around.
65      * <p>
66      * This inner class is marked &quot;public&quot; due to a compiler bug.
67      * This class should be treated as a &quot;protected&quot; inner class.
68      * Instantiate it only within subclasses of MetalSplitPaneDivider.
69      */

70     public final class ExtWindowsDividerLayout implements LayoutManager JavaDoc {
71         public void layoutContainer(Container JavaDoc c) {
72             JButton JavaDoc theLeftButton = getLeftButtonFromSuper();
73             JButton JavaDoc theRightButton = getRightButtonFromSuper();
74             JSplitPane JavaDoc theSplitPane = getSplitPaneFromSuper();
75             int theOrientation = getOrientationFromSuper();
76             int oneTouchSize = getOneTouchSize();
77             int oneTouchOffset = getOneTouchOffset();
78             int blockSize = 5;
79             //getBlockSize(); //Math.min(getDividerSize(), oneTouchSize);
80

81             // This layout differs from the one used in BasicSplitPaneDivider.
82
// It does not center justify the oneTouchExpadable buttons.
83
// This was necessary in order to meet the spec of the Metal
84
// splitpane divider.
85
if (theLeftButton != null
86                 && theRightButton != null
87                 && c == WindowsSplitPaneDivider.this) {
88                 if (theSplitPane.isOneTouchExpandable()) {
89                     if (theOrientation == JSplitPane.VERTICAL_SPLIT) {
90                         theLeftButton.setBounds(
91                             oneTouchOffset,
92                             0,
93                             blockSize * 2,
94                             blockSize);
95                         theRightButton.setBounds(
96                             oneTouchOffset + oneTouchSize * 2,
97                             0,
98                             blockSize * 2,
99                             blockSize);
100                     } else {
101                         theLeftButton.setBounds(
102                             0,
103                             oneTouchOffset,
104                             blockSize,
105                             blockSize * 2);
106                         theRightButton.setBounds(
107                             0,
108                             oneTouchOffset + oneTouchSize * 2,
109                             blockSize,
110                             blockSize * 2);
111                     }
112                 } else {
113                     theLeftButton.setBounds(-5, -5, 1, 1);
114                     theRightButton.setBounds(-5, -5, 1, 1);
115                 }
116             }
117         }
118
119         public Dimension JavaDoc minimumLayoutSize(Container JavaDoc c) {
120             return new Dimension JavaDoc(0, 0);
121         }
122         public Dimension JavaDoc preferredLayoutSize(Container JavaDoc c) {
123             return new Dimension JavaDoc(0, 0);
124         }
125         public void removeLayoutComponent(Component JavaDoc c) {
126             // Unused method; implements LayoutManager.
127
}
128         public void addLayoutComponent(String JavaDoc string, Component JavaDoc c) {
129             // Unused method; implements LayoutManager.
130
}
131     }
132
133     public WindowsSplitPaneDivider(BasicSplitPaneUI JavaDoc ui) {
134         super(ui);
135         setLayout(new ExtWindowsDividerLayout());
136     }
137
138     /**
139      * Creates and return an instance of JButton that can be used to
140      * collapse the left component in the metal split pane.
141      */

142     protected JButton JavaDoc createLeftOneTouchButton() {
143         JButton JavaDoc b = new JButton JavaDoc() {
144                 // Sprite buffer for the arrow image of the left button
145
int[][] buffer = { { 0, 0, 0, 2, 2, 0, 0, 0, 0 }, {
146                     0, 0, 2, 1, 1, 1, 0, 0, 0 }, {
147                     0, 2, 1, 1, 1, 1, 1, 0, 0 }, {
148                     2, 1, 1, 1, 1, 1, 1, 1, 0 }, {
149                     0, 3, 3, 3, 3, 3, 3, 3, 3 }
150             };
151
152             public void setBorder(Border JavaDoc border) {
153                 // Ignore borders
154
}
155
156             public void paint(Graphics JavaDoc g) {
157                 JSplitPane JavaDoc theSplitPane = getSplitPaneFromSuper();
158                 if (theSplitPane != null) {
159                     int theOrientation = getOrientationFromSuper();
160                     int blockSize = buffer.length + 1;
161                     //Math.min(getDividerSize(), oneTouchSize);
162

163                     // Initialize the color array
164
Color JavaDoc[] colors =
165                         {
166                             this.getBackground(),
167                             UIManager.getColor("controlDkShadow"),
168                             Color.black,
169                         //UIManager.getColor(),
170
UIManager.getColor("controlLtHighlight")};
171
172                     // Fill the background first ...
173
g.setColor(this.getBackground());
174                     g.fillRect(0, 0, this.getWidth(), this.getHeight());
175
176                     // ... then draw the arrow.
177
if (getModel().isPressed()) {
178                         // Adjust color mapping for pressed button state
179
colors[1] = colors[2];
180                     }
181                     if (theOrientation == JSplitPane.VERTICAL_SPLIT) {
182                         // Draw the image for a vertical split
183
for (int i = 1; i <= buffer[0].length; i++) {
184                             for (int j = 1; j < blockSize; j++) {
185                                 if (buffer[j - 1][i - 1] == 0) {
186                                     continue;
187                                 } else {
188                                     g.setColor(colors[buffer[j - 1][i - 1]]);
189                                 }
190                                 g.drawLine(i - 1, j, i - 1, j);
191                             }
192                         }
193                     } else {
194                         // Draw the image for a horizontal split
195
// by simply swaping the i and j axis.
196
// Except the drawLine() call this code is
197
// identical to the code block above. This was done
198
// in order to remove the additional orientation
199
// check for each pixel.
200
for (int i = 1; i <= buffer[0].length; i++) {
201                             for (int j = 1; j < blockSize; j++) {
202                                 if (buffer[j - 1][i - 1] == 0) {
203                                     // Nothing needs
204
// to be drawn
205
continue;
206                                 } else {
207                                     // Set the color from the
208
// color map
209
g.setColor(colors[buffer[j - 1][i - 1]]);
210                                 }
211                                 // Draw a pixel
212
g.drawLine(j - 1, i, j - 1, i);
213                             }
214                         }
215                     }
216                 }
217             }
218
219         };
220         b.setFocusPainted(false);
221         b.setBorderPainted(false);
222         b.setFocusable(false);
223         return b;
224     }
225
226     /**
227      * Creates and return an instance of JButton that can be used to
228      * collapse the right component in the metal split pane.
229      */

230     protected JButton JavaDoc createRightOneTouchButton() {
231         JButton JavaDoc b = new JButton JavaDoc() {
232                 // Sprite buffer for the arrow image of the right button
233
int[][] buffer = { { 2, 2, 2, 2, 2, 2, 2, 2 }, {
234                     0, 1, 1, 1, 1, 1, 1, 3 }, {
235                     0, 0, 1, 1, 1, 1, 3, 0 }, {
236                     0, 0, 0, 1, 1, 3, 0, 0 }, {
237                     0, 0, 0, 0, 3, 0, 0, 0 }
238             };
239
240             public void setBorder(Border JavaDoc border) {
241                 // Ignore borders
242
}
243
244             public void paint(Graphics JavaDoc g) {
245                 JSplitPane JavaDoc theSplitPane = getSplitPaneFromSuper();
246                 if (theSplitPane != null) {
247                     int theOrientation = getOrientationFromSuper();
248                     int blockSize = buffer.length + 1;
249                     //Math.min(getDividerSize(), oneTouchSize);
250

251                     // Initialize the color array
252
Color JavaDoc[] colors =
253                         {
254                             this.getBackground(),
255                             UIManager.getColor("controlDkShadow"),
256                             Color.black,
257                         //UIManager.getColor("controlDkShadow"),
258
UIManager.getColor("controlLtHighlight")};
259
260                     // Fill the background first ...
261
g.setColor(this.getBackground());
262                     g.fillRect(0, 0, this.getWidth(), this.getHeight());
263
264                     // ... then draw the arrow.
265
if (getModel().isPressed()) {
266                         // Adjust color mapping for pressed button state
267
colors[1] = colors[2];
268                     }
269                     if (theOrientation == JSplitPane.VERTICAL_SPLIT) {
270                         // Draw the image for a vertical split
271
for (int i = 1; i <= buffer[0].length; i++) {
272                             for (int j = 1; j < blockSize; j++) {
273                                 if (buffer[j - 1][i - 1] == 0) {
274                                     continue;
275                                 } else {
276                                     g.setColor(colors[buffer[j - 1][i - 1]]);
277                                 }
278                                 g.drawLine(i, j, i, j);
279                             }
280                         }
281                     } else {
282                         // Draw the image for a horizontal split
283
// by simply swaping the i and j axis.
284
// Except the drawLine() call this code is
285
// identical to the code block above. This was done
286
// in order to remove the additional orientation
287
// check for each pixel.
288
for (int i = 1; i <= buffer[0].length; i++) {
289                             for (int j = 1; j < blockSize; j++) {
290                                 if (buffer[j - 1][i - 1] == 0) {
291                                     // Nothing needs
292
// to be drawn
293
continue;
294                                 } else {
295                                     // Set the color from the
296
// color map
297
g.setColor(colors[buffer[j - 1][i - 1]]);
298                                 }
299                                 // Draw a pixel
300
g.drawLine(j - 1, i, j - 1, i);
301                             }
302                         }
303                     }
304                 }
305             }
306         };
307         b.setFocusPainted(false);
308         b.setBorderPainted(false);
309         b.setFocusable(false);
310         return b;
311     }
312
313     int getBlockSize() {
314         return EXT_BLOCKSIZE;
315     }
316     
317     int getOneTouchOffset() {
318         return EXT_ONE_TOUCH_OFFSET;
319     }
320     
321     int getOneTouchSize() {
322         return EXT_ONE_TOUCH_SIZE;
323     }
324     
325     int getOrientationFromSuper() {
326         return super.orientation;
327     }
328     
329     JButton JavaDoc getLeftButtonFromSuper() {
330         return super.leftButton;
331     }
332     
333     JButton JavaDoc getRightButtonFromSuper() {
334         return super.rightButton;
335     }
336     
337     JSplitPane JavaDoc getSplitPaneFromSuper() {
338         return super.splitPane;
339     }
340     
341 }
Popular Tags