KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)MetalScrollButton.java 1.25 04/03/03
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.Dimension JavaDoc;
11 import java.awt.Graphics JavaDoc;
12 import java.awt.Color JavaDoc;
13 import java.awt.Polygon JavaDoc;
14
15 import javax.swing.*;
16
17 import javax.swing.plaf.basic.BasicArrowButton JavaDoc;
18
19
20 /**
21  * JButton object for Metal scrollbar arrows.
22  * <p>
23  * <strong>Warning:</strong>
24  * Serialized objects of this class will not be compatible with
25  * future Swing releases. The current serialization support is
26  * appropriate for short term storage or RMI between applications running
27  * the same version of Swing. As of 1.4, support for long term storage
28  * of all JavaBeans<sup><font size="-2">TM</font></sup>
29  * has been added to the <code>java.beans</code> package.
30  * Please see {@link java.beans.XMLEncoder}.
31  *
32  * @version 1.15 08/28/98
33  * @author Tom Santos
34  * @author Steve Wilson
35  */

36 public class MetalScrollButton extends BasicArrowButton JavaDoc
37 {
38   private static Color JavaDoc shadowColor;
39   private static Color JavaDoc highlightColor;
40   private boolean isFreeStanding = false;
41
42   private int buttonWidth;
43
44         public MetalScrollButton( int direction, int width, boolean freeStanding )
45         {
46             super( direction );
47
48         shadowColor = UIManager.getColor("ScrollBar.darkShadow");
49         highlightColor = UIManager.getColor("ScrollBar.highlight");
50
51             buttonWidth = width;
52         isFreeStanding = freeStanding;
53         }
54
55         public void setFreeStanding( boolean freeStanding )
56         {
57         isFreeStanding = freeStanding;
58         }
59
60     public void paint( Graphics JavaDoc g )
61         {
62             boolean leftToRight = MetalUtils.isLeftToRight(this);
63         boolean isEnabled = getParent().isEnabled();
64
65         Color JavaDoc arrowColor = isEnabled ? MetalLookAndFeel.getControlInfo() : MetalLookAndFeel.getControlDisabled();
66         boolean isPressed = getModel().isPressed();
67         int width = getWidth();
68         int height = getHeight();
69         int w = width;
70         int h = height;
71         int arrowHeight = (height+1) / 4;
72         int arrowWidth = (height+1) / 2;
73
74         if ( isPressed )
75         {
76             g.setColor( MetalLookAndFeel.getControlShadow() );
77         }
78         else
79             {
80             g.setColor( getBackground() );
81         }
82
83         g.fillRect( 0, 0, width, height );
84
85         if ( getDirection() == NORTH )
86         {
87             if ( !isFreeStanding ) {
88                     height +=1;
89                     g.translate( 0, -1 );
90                     width += 2;
91                     if ( !leftToRight ) {
92                         g.translate( -1, 0 );
93                     }
94         }
95
96             // Draw the arrow
97
g.setColor( arrowColor );
98         int startY = ((h+1) - arrowHeight) / 2;
99         int startX = (w / 2);
100         // System.out.println( "startX :" + startX + " startY :"+startY);
101
for (int line = 0; line < arrowHeight; line++) {
102             g.drawLine( startX-line, startY+line, startX +line+1, startY+line);
103         }
104     /* g.drawLine( 7, 6, 8, 6 );
105         g.drawLine( 6, 7, 9, 7 );
106         g.drawLine( 5, 8, 10, 8 );
107         g.drawLine( 4, 9, 11, 9 );*/

108             
109         if (isEnabled) {
110             g.setColor( highlightColor );
111
112             if ( !isPressed )
113             {
114             g.drawLine( 1, 1, width - 3, 1 );
115             g.drawLine( 1, 1, 1, height - 1 );
116             }
117
118             g.drawLine( width - 1, 1, width - 1, height - 1 );
119
120             g.setColor( shadowColor );
121             g.drawLine( 0, 0, width - 2, 0 );
122             g.drawLine( 0, 0, 0, height - 1 );
123             g.drawLine( width - 2, 2, width - 2, height - 1 );
124         } else {
125             MetalUtils.drawDisabledBorder(g, 0, 0, width, height+1);
126         }
127             if ( !isFreeStanding ) {
128                     height -= 1;
129                     g.translate( 0, 1 );
130                     width -= 2;
131                     if ( !leftToRight ) {
132                         g.translate( 1, 0 );
133                     }
134         }
135         }
136         else if ( getDirection() == SOUTH )
137         {
138             if ( !isFreeStanding ) {
139             height += 1;
140                     width += 2;
141                     if ( !leftToRight ) {
142                         g.translate( -1, 0 );
143                     }
144         }
145
146             // Draw the arrow
147
g.setColor( arrowColor );
148
149         int startY = (((h+1) - arrowHeight) / 2)+ arrowHeight-1;
150         int startX = (w / 2);
151
152         // System.out.println( "startX2 :" + startX + " startY2 :"+startY);
153

154         for (int line = 0; line < arrowHeight; line++) {
155             g.drawLine( startX-line, startY-line, startX +line+1, startY-line);
156         }
157
158     /* g.drawLine( 4, 5, 11, 5 );
159         g.drawLine( 5, 6, 10, 6 );
160         g.drawLine( 6, 7, 9, 7 );
161         g.drawLine( 7, 8, 8, 8 ); */

162
163         if (isEnabled) {
164             g.setColor( highlightColor );
165
166             if ( !isPressed )
167             {
168             g.drawLine( 1, 0, width - 3, 0 );
169             g.drawLine( 1, 0, 1, height - 3 );
170             }
171
172             g.drawLine( 1, height - 1, width - 1, height - 1 );
173             g.drawLine( width - 1, 0, width - 1, height - 1 );
174
175             g.setColor( shadowColor );
176             g.drawLine( 0, 0, 0, height - 2 );
177             g.drawLine( width - 2, 0, width - 2, height - 2 );
178             g.drawLine( 2, height - 2, width - 2, height - 2 );
179         } else {
180             MetalUtils.drawDisabledBorder(g, 0,-1, width, height+1);
181         }
182
183             if ( !isFreeStanding ) {
184             height -= 1;
185                     width -= 2;
186                     if ( !leftToRight ) {
187                         g.translate( 1, 0 );
188                     }
189         }
190         }
191         else if ( getDirection() == EAST )
192         {
193             if ( !isFreeStanding ) {
194             height += 2;
195             width += 1;
196         }
197
198             // Draw the arrow
199
g.setColor( arrowColor );
200
201         int startX = (((w+1) - arrowHeight) / 2) + arrowHeight-1;
202         int startY = (h / 2);
203
204         //System.out.println( "startX2 :" + startX + " startY2 :"+startY);
205

206         for (int line = 0; line < arrowHeight; line++) {
207             g.drawLine( startX-line, startY-line, startX -line, startY+line+1);
208         }
209
210
211 /* g.drawLine( 5, 4, 5, 11 );
212         g.drawLine( 6, 5, 6, 10 );
213         g.drawLine( 7, 6, 7, 9 );
214         g.drawLine( 8, 7, 8, 8 );*/

215
216         if (isEnabled) {
217             g.setColor( highlightColor );
218
219             if ( !isPressed )
220             {
221                 g.drawLine( 0, 1, width - 3, 1 );
222                 g.drawLine( 0, 1, 0, height - 3 );
223             }
224
225             g.drawLine( width - 1, 1, width - 1, height - 1 );
226             g.drawLine( 0, height - 1, width - 1, height - 1 );
227
228             g.setColor( shadowColor );
229             g.drawLine( 0, 0,width - 2, 0 );
230             g.drawLine( width - 2, 2, width - 2, height - 2 );
231             g.drawLine( 0, height - 2, width - 2, height - 2 );
232         } else {
233             MetalUtils.drawDisabledBorder(g,-1,0, width+1, height);
234         }
235             if ( !isFreeStanding ) {
236             height -= 2;
237             width -= 1;
238         }
239         }
240         else if ( getDirection() == WEST )
241         {
242             if ( !isFreeStanding ) {
243             height += 2;
244             width += 1;
245             g.translate( -1, 0 );
246         }
247
248             // Draw the arrow
249
g.setColor( arrowColor );
250
251         int startX = (((w+1) - arrowHeight) / 2);
252         int startY = (h / 2);
253
254
255         for (int line = 0; line < arrowHeight; line++) {
256             g.drawLine( startX+line, startY-line, startX +line, startY+line+1);
257         }
258
259     /* g.drawLine( 6, 7, 6, 8 );
260         g.drawLine( 7, 6, 7, 9 );
261         g.drawLine( 8, 5, 8, 10 );
262         g.drawLine( 9, 4, 9, 11 );*/

263
264         if (isEnabled) {
265             g.setColor( highlightColor );
266
267
268             if ( !isPressed )
269             {
270                 g.drawLine( 1, 1, width - 1, 1 );
271             g.drawLine( 1, 1, 1, height - 3 );
272             }
273
274             g.drawLine( 1, height - 1, width - 1, height - 1 );
275         
276             g.setColor( shadowColor );
277             g.drawLine( 0, 0, width - 1, 0 );
278             g.drawLine( 0, 0, 0, height - 2 );
279             g.drawLine( 2, height - 2, width - 1, height - 2 );
280         } else {
281             MetalUtils.drawDisabledBorder(g,0,0, width+1, height);
282         }
283
284             if ( !isFreeStanding ) {
285             height -= 2;
286             width -= 1;
287             g.translate( 1, 0 );
288         }
289         }
290         }
291
292         public Dimension JavaDoc getPreferredSize()
293         {
294         if ( getDirection() == NORTH )
295         {
296             return new Dimension JavaDoc( buttonWidth, buttonWidth - 2 );
297         }
298         else if ( getDirection() == SOUTH )
299             {
300             return new Dimension JavaDoc( buttonWidth, buttonWidth - (isFreeStanding ? 1 : 2) );
301         }
302         else if ( getDirection() == EAST )
303             {
304             return new Dimension JavaDoc( buttonWidth - (isFreeStanding ? 1 : 2), buttonWidth );
305         }
306         else if ( getDirection() == WEST )
307             {
308             return new Dimension JavaDoc( buttonWidth - 2, buttonWidth );
309         }
310         else
311             {
312             return new Dimension JavaDoc( 0, 0 );
313         }
314         }
315
316         public Dimension JavaDoc getMinimumSize()
317         {
318             return getPreferredSize();
319         }
320
321         public Dimension JavaDoc getMaximumSize()
322         {
323             return new Dimension JavaDoc( Integer.MAX_VALUE, Integer.MAX_VALUE );
324         }
325     
326         public int getButtonWidth() {
327         return buttonWidth;
328     }
329 }
330
331
Popular Tags