KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticArrowButton


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.plastic;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Rectangle JavaDoc;
36
37 import javax.swing.ButtonModel JavaDoc;
38 import javax.swing.UIManager JavaDoc;
39 import javax.swing.plaf.metal.MetalScrollButton JavaDoc;
40
41 /**
42  * Renders the arrow buttons in scroll bars and spinners.
43  *
44  * @author Karsten Lentzsch
45  * @version $Revision: 1.6 $
46  */

47 class PlasticArrowButton extends MetalScrollButton JavaDoc {
48     
49     private static Color JavaDoc shadowColor;
50     private static Color JavaDoc highlightColor;
51     
52     protected boolean isFreeStanding;
53
54
55     public PlasticArrowButton(int direction, int width, boolean freeStanding) {
56         super(direction, width, freeStanding);
57         shadowColor = UIManager.getColor("ScrollBar.darkShadow");
58         highlightColor = UIManager.getColor("ScrollBar.highlight");
59         isFreeStanding = freeStanding;
60     }
61
62
63     public void setFreeStanding(boolean freeStanding) {
64         super.setFreeStanding(freeStanding);
65         isFreeStanding = freeStanding;
66     }
67     
68
69     public void paint(Graphics JavaDoc g) {
70         boolean leftToRight = PlasticUtils.isLeftToRight(this);
71         boolean isEnabled = getParent().isEnabled();
72         boolean isPressed = getModel().isPressed();
73
74         Color JavaDoc arrowColor = isEnabled
75                 ? PlasticLookAndFeel.getControlInfo()
76                 : PlasticLookAndFeel.getControlDisabled();
77         int width = getWidth();
78         int height = getHeight();
79         int w = width;
80         int h = height;
81         int arrowHeight = calculateArrowHeight(height, width);
82         int arrowOffset = calculateArrowOffset();
83         boolean paintNorthBottom = isPaintingNorthBottom();
84
85         g.setColor(isPressed ? PlasticLookAndFeel.getControlShadow() : getBackground());
86         g.fillRect(0, 0, width, height);
87
88         if (getDirection() == NORTH) {
89             paintNorth(g, leftToRight, isEnabled, arrowColor, isPressed,
90                 width, height, w, h, arrowHeight, arrowOffset, paintNorthBottom);
91         } else if (getDirection() == SOUTH) {
92             paintSouth(g, leftToRight, isEnabled, arrowColor, isPressed,
93                 width, height, w, h, arrowHeight, arrowOffset);
94         } else if (getDirection() == EAST) {
95             paintEast(g, isEnabled, arrowColor, isPressed,
96                 width, height, w, h, arrowHeight);
97         } else if (getDirection() == WEST) {
98             paintWest(g, isEnabled, arrowColor, isPressed,
99                 width, height, w, h, arrowHeight);
100         }
101         if (PlasticUtils.is3D("ScrollBar."))
102             paint3D(g);
103     }
104
105     /**
106      * Computes and returns the arrow height based on the specified
107      * buttons height and width.
108      *
109      * @param height the height of the button to be used for calculation.
110      * @param width the width of the button to be used for calculation.
111      * @return the height of the arrow
112      */

113     protected int calculateArrowHeight(int height, int width) {
114         return (height + 1) / 4;
115     }
116     
117     protected int calculateArrowOffset() {
118         return 0;
119     }
120     
121     protected boolean isPaintingNorthBottom() {
122         return false;
123     }
124     
125
126     private void paintWest(Graphics JavaDoc g, boolean isEnabled, Color JavaDoc arrowColor,
127         boolean isPressed, int width, int height, int w, int h, int arrowHeight) {
128             
129         if (!isFreeStanding) {
130             height += 2;
131             width += 1;
132             g.translate(-1, 0);
133         }
134         
135         // Draw the arrow
136
g.setColor(arrowColor);
137         
138         int startX = (((w + 1) - arrowHeight) / 2);
139         int startY = (h / 2);
140         
141         for (int line = 0; line < arrowHeight; line++) {
142             g.drawLine(
143                 startX + line,
144                 startY - line,
145                 startX + line,
146                 startY + line + 1);
147         }
148         
149         if (isEnabled) {
150             g.setColor(highlightColor);
151         
152             if (!isPressed) {
153                 g.drawLine(1, 1, width - 1, 1);
154                 g.drawLine(1, 1, 1, height - 3);
155             }
156             g.drawLine(1, height - 1, width - 1, height - 1);
157         
158             g.setColor(shadowColor);
159             g.drawLine(0, 0, width - 1, 0);
160             g.drawLine(0, 0, 0, height - 2);
161             g.drawLine(1, height - 2, width - 1, height - 2);
162         } else {
163             PlasticUtils.drawDisabledBorder(g, 0, 0, width + 1, height);
164         }
165         
166         if (!isFreeStanding) {
167             height -= 2;
168             width -= 1;
169             g.translate(1, 0);
170         }
171     }
172
173
174     private void paintEast(Graphics JavaDoc g, boolean isEnabled, Color JavaDoc arrowColor,
175         boolean isPressed, int width, int height, int w, int h, int arrowHeight) {
176         if (!isFreeStanding) {
177             height += 2;
178             width += 1;
179         }
180         
181         // Draw the arrow
182
g.setColor(arrowColor);
183         
184         int startX = (((w + 1) - arrowHeight) / 2) + arrowHeight - 1;
185         int startY = (h / 2);
186         for (int line = 0; line < arrowHeight; line++) {
187             g.drawLine(
188                 startX - line,
189                 startY - line,
190                 startX - line,
191                 startY + line + 1);
192         }
193         
194         if (isEnabled) {
195             g.setColor(highlightColor);
196             if (!isPressed) {
197                 g.drawLine(0, 1, width - 3, 1);
198                 g.drawLine(0, 1, 0, height - 3);
199             }
200             g.drawLine(width - 1, 1, width - 1, height - 1);
201             g.drawLine(0, height - 1, width - 1, height - 1);
202         
203             g.setColor(shadowColor);
204             g.drawLine(0, 0, width - 2, 0);
205             g.drawLine(width - 2, 1, width - 2, height - 2);
206             g.drawLine(0, height - 2, width - 2, height - 2);
207         } else {
208             PlasticUtils.drawDisabledBorder(g, -1, 0, width + 1, height);
209         }
210         if (!isFreeStanding) {
211             height -= 2;
212             width -= 1;
213         }
214     }
215
216
217     protected void paintSouth(Graphics JavaDoc g, boolean leftToRight, boolean isEnabled,
218         Color JavaDoc arrowColor, boolean isPressed,
219         int width, int height, int w, int h, int arrowHeight, int arrowOffset) {
220             
221         if (!isFreeStanding) {
222             height += 1;
223             if (!leftToRight) {
224                 width += 1;
225                 g.translate(-1, 0);
226             } else {
227                 width += 2;
228             }
229         }
230         
231         // Draw the arrow
232
g.setColor(arrowColor);
233         
234         int startY = (((h + 0) - arrowHeight) / 2) + arrowHeight - 1; // KL was h + 1
235
int startX = w / 2;
236         
237         // System.out.println( "startX2 :" + startX + " startY2 :"+startY);
238

239         for (int line = 0; line < arrowHeight; line++) {
240             g.fillRect(startX - line - arrowOffset, startY - line, 2*(line + 1), 1);
241         }
242         
243         if (isEnabled) {
244             g.setColor(highlightColor);
245             if (!isPressed) {
246                 g.drawLine(1, 0, width - 3, 0);
247                 g.drawLine(1, 0, 1, height - 3);
248             }
249             g.drawLine(0, height - 1, width - 1, height - 1);
250             g.drawLine(width - 1, 0, width - 1, height - 1);
251         
252             g.setColor(shadowColor);
253             g.drawLine(0, 0, 0, height - 2);
254             g.drawLine(width - 2, 0, width - 2, height - 2);
255             g.drawLine(1, height - 2, width - 2, height - 2);
256         } else {
257             PlasticUtils.drawDisabledBorder(g, 0, -1, width, height + 1);
258         }
259         
260         if (!isFreeStanding) {
261             height -= 1;
262             if (!leftToRight) {
263                 width -= 1;
264                 g.translate(1, 0);
265             } else {
266                 width -= 2;
267             }
268         }
269     }
270
271
272     protected void paintNorth(Graphics JavaDoc g, boolean leftToRight, boolean isEnabled,
273         Color JavaDoc arrowColor, boolean isPressed,
274         int width, int height, int w, int h, int arrowHeight, int arrowOffset,
275         boolean paintBottom) {
276         if (!isFreeStanding) {
277             height += 1;
278             g.translate(0, -1);
279             if (!leftToRight) {
280                 width += 1;
281                 g.translate(-1, 0);
282             } else {
283                 width += 2;
284             }
285         }
286         
287         // Draw the arrow
288
g.setColor(arrowColor);
289         int startY = ((h + 1) - arrowHeight) / 2; // KL was (h + 1)
290
int startX = w / 2;
291         // System.out.println( "startX :" + startX + " startY :"+startY);
292
for (int line = 0; line < arrowHeight; line++) {
293             g.fillRect(startX - line - arrowOffset, startY + line, 2*(line + 1), 1);
294         }
295         
296         if (isEnabled) {
297             g.setColor(highlightColor);
298         
299             if (!isPressed) {
300                 g.drawLine(1, 1, width - 3, 1);
301                 g.drawLine(1, 1, 1, height - 1);
302             }
303         
304             g.drawLine(width - 1, 1, width - 1, height - 1);
305         
306             g.setColor(shadowColor);
307             g.drawLine(0, 0, width - 2, 0);
308             g.drawLine(0, 0, 0, height - 1);
309             g.drawLine(width - 2, 1, width - 2, height - 1);
310             if (paintBottom) {
311                 g.fillRect(0, height - 1, width - 1, 1);
312             }
313         } else {
314             PlasticUtils.drawDisabledBorder(g, 0, 0, width, height + 1);
315             if (paintBottom) {
316                 g.setColor(PlasticLookAndFeel.getControlShadow());
317                 g.fillRect(0, height - 1, width - 1, 1);
318             }
319         }
320         if (!isFreeStanding) {
321             height -= 1;
322             g.translate(0, 1);
323             if (!leftToRight) {
324                 width -= 1;
325                 g.translate(1, 0);
326             } else {
327                 width -= 2;
328             }
329         }
330     }
331     
332
333     private void paint3D(Graphics JavaDoc g) {
334         ButtonModel JavaDoc buttonModel = getModel();
335         if (buttonModel.isArmed() && buttonModel.isPressed() || buttonModel.isSelected())
336             return;
337             
338         int width = getWidth();
339         int height = getHeight();
340         if (getDirection() == EAST)
341             width -= 2;
342         else if (getDirection() == SOUTH)
343             height -= 2;
344
345         Rectangle JavaDoc r = new Rectangle JavaDoc(1, 1, width, height);
346         boolean isHorizontal = (getDirection() == EAST || getDirection() == WEST);
347         PlasticUtils.addLight3DEffekt(g, r, isHorizontal);
348     }
349 }
Popular Tags