KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)MetalSliderUI.java 1.36 05/10/31
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 javax.swing.plaf.basic.BasicSliderUI JavaDoc;
11
12 import java.awt.Component JavaDoc;
13 import java.awt.Container JavaDoc;
14 import java.awt.Graphics JavaDoc;
15 import java.awt.Dimension JavaDoc;
16 import java.awt.Rectangle JavaDoc;
17 import java.awt.Point JavaDoc;
18 import java.awt.Insets JavaDoc;
19 import java.awt.Color JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.awt.IllegalComponentStateException JavaDoc;
22 import java.awt.Polygon JavaDoc;
23 import java.beans.*;
24
25 import javax.swing.border.AbstractBorder JavaDoc;
26
27 import javax.swing.*;
28 import javax.swing.event.*;
29 import javax.swing.plaf.*;
30
31 /**
32  * A Java L&F implementation of SliderUI.
33  * <p>
34  * <strong>Warning:</strong>
35  * Serialized objects of this class will not be compatible with
36  * future Swing releases. The current serialization support is
37  * appropriate for short term storage or RMI between applications running
38  * the same version of Swing. As of 1.4, support for long term storage
39  * of all JavaBeans<sup><font size="-2">TM</font></sup>
40  * has been added to the <code>java.beans</code> package.
41  * Please see {@link java.beans.XMLEncoder}.
42  *
43  * @version 1.36 10/31/05
44  * @author Tom Santos
45  */

46 public class MetalSliderUI extends BasicSliderUI JavaDoc {
47
48     protected final int TICK_BUFFER = 4;
49     protected boolean filledSlider = false;
50     // NOTE: these next three variables are currently unused.
51
protected static Color JavaDoc thumbColor;
52     protected static Color JavaDoc highlightColor;
53     protected static Color JavaDoc darkShadowColor;
54     protected static int trackWidth;
55     protected static int tickLength;
56     
57     protected static Icon horizThumbIcon;
58     
59     protected static Icon vertThumbIcon;
60
61     private static Icon SAFE_HORIZ_THUMB_ICON;
62     private static Icon SAFE_VERT_THUMB_ICON;
63
64
65     protected final String JavaDoc SLIDER_FILL = "JSlider.isFilled";
66
67     public static ComponentUI createUI(JComponent c) {
68         return new MetalSliderUI JavaDoc();
69     }
70
71     public MetalSliderUI() {
72         super( null );
73     }
74
75     private static Icon getHorizThumbIcon() {
76         if (System.getSecurityManager() != null) {
77             return SAFE_HORIZ_THUMB_ICON;
78         } else {
79             return horizThumbIcon;
80         }
81     }
82
83     private static Icon getVertThumbIcon() {
84         if (System.getSecurityManager() != null) {
85             return SAFE_VERT_THUMB_ICON;
86         } else {
87             return vertThumbIcon;
88         }
89     }
90
91     public void installUI( JComponent c ) {
92         trackWidth = ((Integer JavaDoc)UIManager.get( "Slider.trackWidth" )).intValue();
93         tickLength = ((Integer JavaDoc)UIManager.get( "Slider.majorTickLength" )).intValue();
94         horizThumbIcon = SAFE_HORIZ_THUMB_ICON =
95                 UIManager.getIcon( "Slider.horizontalThumbIcon" );
96         vertThumbIcon = SAFE_VERT_THUMB_ICON =
97                 UIManager.getIcon( "Slider.verticalThumbIcon" );
98
99     super.installUI( c );
100
101         thumbColor = UIManager.getColor("Slider.thumb");
102         highlightColor = UIManager.getColor("Slider.highlight");
103         darkShadowColor = UIManager.getColor("Slider.darkShadow");
104
105         scrollListener.setScrollByBlock( false );
106
107         Object JavaDoc sliderFillProp = c.getClientProperty( SLIDER_FILL );
108         if ( sliderFillProp != null ) {
109             filledSlider = ((Boolean JavaDoc)sliderFillProp).booleanValue();
110         }
111     }
112
113     protected PropertyChangeListener createPropertyChangeListener( JSlider slider ) {
114         return new MetalPropertyListener();
115     }
116
117     protected class MetalPropertyListener extends BasicSliderUI.PropertyChangeHandler JavaDoc {
118         public void propertyChange( PropertyChangeEvent e ) { // listen for slider fill
119
super.propertyChange( e );
120
121         String JavaDoc name = e.getPropertyName();
122         if ( name.equals( SLIDER_FILL ) ) {
123             if ( e.getNewValue() != null ) {
124             filledSlider = ((Boolean JavaDoc)e.getNewValue()).booleanValue();
125         }
126         else {
127             filledSlider = false;
128         }
129         }
130     }
131     }
132
133     public void paintThumb(Graphics JavaDoc g) {
134         Rectangle JavaDoc knobBounds = thumbRect;
135
136         g.translate( knobBounds.x, knobBounds.y );
137
138         if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
139             getHorizThumbIcon().paintIcon( slider, g, 0, 0 );
140         }
141         else {
142             getVertThumbIcon().paintIcon( slider, g, 0, 0 );
143         }
144
145         g.translate( -knobBounds.x, -knobBounds.y );
146     }
147
148     /**
149      * If <code>chooseFirst</code>is true, <code>c1</code> is returned,
150      * otherwise <code>c2</code>.
151      */

152     private Color JavaDoc chooseColor(boolean chooseFirst, Color JavaDoc c1, Color JavaDoc c2) {
153         if (chooseFirst) {
154             return c2;
155         }
156         return c1;
157     }
158
159     /**
160      * Returns a rectangle enclosing the track that will be painted.
161      */

162     private Rectangle JavaDoc getPaintTrackRect() {
163         int trackLeft = 0, trackRight = 0, trackTop = 0, trackBottom = 0;
164         if (slider.getOrientation() == JSlider.HORIZONTAL) {
165             trackBottom = (trackRect.height - 1) - getThumbOverhang();
166             trackTop = trackBottom - (getTrackWidth() - 1);
167             trackRight = trackRect.width - 1;
168         }
169         else {
170         if (MetalUtils.isLeftToRight(slider)) {
171             trackLeft = (trackRect.width - getThumbOverhang()) -
172                                                  getTrackWidth();
173         trackRight = (trackRect.width - getThumbOverhang()) - 1;
174         }
175         else {
176             trackLeft = getThumbOverhang();
177         trackRight = getThumbOverhang() + getTrackWidth() - 1;
178         }
179             trackBottom = trackRect.height - 1;
180         }
181         return new Rectangle JavaDoc(trackRect.x + trackLeft, trackRect.y + trackTop,
182                              trackRight - trackLeft, trackBottom - trackTop);
183     }
184
185     public void paintTrack(Graphics JavaDoc g) {
186         if (MetalLookAndFeel.usingOcean()) {
187             oceanPaintTrack(g);
188             return;
189         }
190         Color JavaDoc trackColor = !slider.isEnabled() ? MetalLookAndFeel.getControlShadow() :
191                            slider.getForeground();
192
193     boolean leftToRight = MetalUtils.isLeftToRight(slider);
194
195         g.translate( trackRect.x, trackRect.y );
196
197         int trackLeft = 0;
198         int trackTop = 0;
199         int trackRight = 0;
200         int trackBottom = 0;
201
202         // Draw the track
203
if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
204             trackBottom = (trackRect.height - 1) - getThumbOverhang();
205             trackTop = trackBottom - (getTrackWidth() - 1);
206             trackRight = trackRect.width - 1;
207     }
208     else {
209         if (leftToRight) {
210             trackLeft = (trackRect.width - getThumbOverhang()) -
211                                                  getTrackWidth();
212         trackRight = (trackRect.width - getThumbOverhang()) - 1;
213         }
214         else {
215             trackLeft = getThumbOverhang();
216         trackRight = getThumbOverhang() + getTrackWidth() - 1;
217         }
218             trackBottom = trackRect.height - 1;
219     }
220
221     if ( slider.isEnabled() ) {
222         g.setColor( MetalLookAndFeel.getControlDarkShadow() );
223         g.drawRect( trackLeft, trackTop,
224             (trackRight - trackLeft) - 1, (trackBottom - trackTop) - 1 );
225         
226         g.setColor( MetalLookAndFeel.getControlHighlight() );
227         g.drawLine( trackLeft + 1, trackBottom, trackRight, trackBottom );
228         g.drawLine( trackRight, trackTop + 1, trackRight, trackBottom );
229
230         g.setColor( MetalLookAndFeel.getControlShadow() );
231         g.drawLine( trackLeft + 1, trackTop + 1, trackRight - 2, trackTop + 1 );
232         g.drawLine( trackLeft + 1, trackTop + 1, trackLeft + 1, trackBottom - 2 );
233     }
234     else {
235         g.setColor( MetalLookAndFeel.getControlShadow() );
236         g.drawRect( trackLeft, trackTop,
237             (trackRight - trackLeft) - 1, (trackBottom - trackTop) - 1 );
238     }
239
240         // Draw the fill
241
if ( filledSlider ) {
242         int middleOfThumb = 0;
243         int fillTop = 0;
244         int fillLeft = 0;
245         int fillBottom = 0;
246         int fillRight = 0;
247
248         if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
249             middleOfThumb = thumbRect.x + (thumbRect.width / 2);
250         middleOfThumb -= trackRect.x; // To compensate for the g.translate()
251
fillTop = !slider.isEnabled() ? trackTop : trackTop + 1;
252         fillBottom = !slider.isEnabled() ? trackBottom - 1 : trackBottom - 2;
253         
254         if ( !drawInverted() ) {
255             fillLeft = !slider.isEnabled() ? trackLeft : trackLeft + 1;
256             fillRight = middleOfThumb;
257         }
258         else {
259             fillLeft = middleOfThumb;
260             fillRight = !slider.isEnabled() ? trackRight - 1 : trackRight - 2;
261         }
262         }
263         else {
264             middleOfThumb = thumbRect.y + (thumbRect.height / 2);
265         middleOfThumb -= trackRect.y; // To compensate for the g.translate()
266
fillLeft = !slider.isEnabled() ? trackLeft : trackLeft + 1;
267         fillRight = !slider.isEnabled() ? trackRight - 1 : trackRight - 2;
268         
269         if ( !drawInverted() ) {
270             fillTop = middleOfThumb;
271             fillBottom = !slider.isEnabled() ? trackBottom - 1 : trackBottom - 2;
272         }
273         else {
274             fillTop = !slider.isEnabled() ? trackTop : trackTop + 1;
275             fillBottom = middleOfThumb;
276         }
277         }
278         
279         if ( slider.isEnabled() ) {
280             g.setColor( slider.getBackground() );
281         g.drawLine( fillLeft, fillTop, fillRight, fillTop );
282         g.drawLine( fillLeft, fillTop, fillLeft, fillBottom );
283
284         g.setColor( MetalLookAndFeel.getControlShadow() );
285         g.fillRect( fillLeft + 1, fillTop + 1,
286                 fillRight - fillLeft, fillBottom - fillTop );
287         }
288         else {
289             g.setColor( MetalLookAndFeel.getControlShadow() );
290         g.fillRect( fillLeft, fillTop,
291                 fillRight - fillLeft, trackBottom - trackTop );
292         }
293     }
294
295         g.translate( -trackRect.x, -trackRect.y );
296     }
297
298     private void oceanPaintTrack(Graphics JavaDoc g) {
299     boolean leftToRight = MetalUtils.isLeftToRight(slider);
300         boolean drawInverted = drawInverted();
301         Color JavaDoc sliderAltTrackColor = (Color JavaDoc)UIManager.get(
302                                     "Slider.altTrackColor");
303
304         // Translate to the origin of the painting rectangle
305
Rectangle JavaDoc paintRect = getPaintTrackRect();
306         g.translate(paintRect.x, paintRect.y);
307
308         // Width and height of the painting rectangle.
309
int w = paintRect.width;
310         int h = paintRect.height;
311
312         if (!slider.isEnabled()) {
313             g.setColor(MetalLookAndFeel.getControlShadow());
314             g.drawRect(0, 0, w - 1, h - 1);
315         }
316     else if (slider.getOrientation() == JSlider.HORIZONTAL) {
317             int middleOfThumb = thumbRect.x + (thumbRect.width / 2) -
318                                 paintRect.x;
319             int fillMinX;
320             int fillMaxX;
321
322             if (middleOfThumb > 0) {
323                 g.setColor(chooseColor(drawInverted,
324                            MetalLookAndFeel.getPrimaryControlDarkShadow(),
325                            MetalLookAndFeel.getControlDarkShadow()));
326                g.drawRect(0, 0, middleOfThumb - 1, h - 1);
327             }
328             if (middleOfThumb < w) {
329                 g.setColor(chooseColor(drawInverted,
330                            MetalLookAndFeel.getControlDarkShadow(),
331                            MetalLookAndFeel.getPrimaryControlDarkShadow()));
332                 g.drawRect(middleOfThumb, 0, w - middleOfThumb - 1, h - 1);
333             }
334             g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
335             if (drawInverted) {
336                 fillMinX = middleOfThumb;
337                 fillMaxX = w - 2;
338                 g.drawLine(1, 1, middleOfThumb, 1);
339             }
340             else {
341                 fillMinX = 1;
342                 fillMaxX = middleOfThumb;
343                 g.drawLine(middleOfThumb, 1, w - 1, 1);
344             }
345             if (h == 6) {
346                 g.setColor(MetalLookAndFeel.getWhite());
347                 g.drawLine(fillMinX, 1, fillMaxX, 1);
348                 g.setColor(sliderAltTrackColor);
349                 g.drawLine(fillMinX, 2, fillMaxX, 2);
350                 g.setColor(MetalLookAndFeel.getControlShadow());
351                 g.drawLine(fillMinX, 3, fillMaxX, 3);
352                 g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
353                 g.drawLine(fillMinX, 4, fillMaxX, 4);
354             }
355         }
356         else {
357             int middleOfThumb = thumbRect.y + (thumbRect.height / 2) -
358                                 paintRect.y;
359             int fillMinY;
360             int fillMaxY;
361
362             if (middleOfThumb > 0) {
363                 g.setColor(chooseColor(drawInverted,
364                            MetalLookAndFeel.getControlDarkShadow(),
365                            MetalLookAndFeel.getPrimaryControlDarkShadow()));
366                 g.drawRect(0, 0, w - 1, middleOfThumb - 1);
367             }
368             if (middleOfThumb < h) {
369                 g.setColor(chooseColor(drawInverted,
370                            MetalLookAndFeel.getPrimaryControlDarkShadow(),
371                            MetalLookAndFeel.getControlDarkShadow()));
372                 g.drawRect(0, middleOfThumb, w - 1, h - middleOfThumb - 1);
373             }
374             g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
375             if (drawInverted()) {
376                 fillMinY = 1;
377                 fillMaxY = middleOfThumb;
378                 if (leftToRight) {
379                     g.drawLine(1, middleOfThumb, 1, h - 1);
380                 }
381                 else {
382                     g.drawLine(w - 2, middleOfThumb, w - 2, h - 1);
383                 }
384             }
385             else {
386                 fillMinY = middleOfThumb;
387                 fillMaxY = h - 2;
388                 if (leftToRight) {
389                     g.drawLine(1, 1, 1, middleOfThumb);
390                 }
391                 else {
392                     g.drawLine(w - 2, 1, w - 2, middleOfThumb);
393                 }
394             }
395             if (w == 6) {
396                 g.setColor(chooseColor(!leftToRight,
397                            MetalLookAndFeel.getWhite(),
398                            MetalLookAndFeel.getPrimaryControlShadow()));
399                 g.drawLine(1, fillMinY, 1, fillMaxY);
400                 g.setColor(chooseColor(!leftToRight, sliderAltTrackColor,
401                            MetalLookAndFeel.getControlShadow()));
402                 g.drawLine(2, fillMinY, 2, fillMaxY);
403                 g.setColor(chooseColor(!leftToRight,
404                            MetalLookAndFeel.getControlShadow(),
405                            sliderAltTrackColor));
406                 g.drawLine(3, fillMinY, 3, fillMaxY);
407                 g.setColor(chooseColor(!leftToRight,
408                            MetalLookAndFeel.getPrimaryControlShadow(),
409                            MetalLookAndFeel.getWhite()));
410                 g.drawLine(4, fillMinY, 4, fillMaxY);
411             }
412         }
413
414         g.translate(-paintRect.x, -paintRect.y);
415     }
416
417     public void paintFocus(Graphics JavaDoc g) {
418     }
419
420     protected Dimension JavaDoc getThumbSize() {
421         Dimension JavaDoc size = new Dimension JavaDoc();
422
423         if ( slider.getOrientation() == JSlider.VERTICAL ) {
424             size.width = getVertThumbIcon().getIconWidth();
425             size.height = getVertThumbIcon().getIconHeight();
426     }
427     else {
428             size.width = getHorizThumbIcon().getIconWidth();
429             size.height = getHorizThumbIcon().getIconHeight();
430     }
431
432     return size;
433     }
434
435     /**
436      * Gets the height of the tick area for horizontal sliders and the width of the
437      * tick area for vertical sliders. BasicSliderUI uses the returned value to
438      * determine the tick area rectangle.
439      */

440     public int getTickLength() {
441         return slider.getOrientation() == JSlider.HORIZONTAL ? tickLength + TICK_BUFFER + 1 :
442         tickLength + TICK_BUFFER + 3;
443     }
444
445     /**
446      * Returns the shorter dimension of the track.
447      */

448     protected int getTrackWidth() {
449         // This strange calculation is here to keep the
450
// track in proportion to the thumb.
451
final double kIdealTrackWidth = 7.0;
452     final double kIdealThumbHeight = 16.0;
453         final double kWidthScalar = kIdealTrackWidth / kIdealThumbHeight;
454
455         if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
456         return (int)(kWidthScalar * thumbRect.height);
457     }
458     else {
459         return (int)(kWidthScalar * thumbRect.width);
460     }
461     }
462
463     /**
464      * Returns the longer dimension of the slide bar. (The slide bar is only the
465      * part that runs directly under the thumb)
466      */

467     protected int getTrackLength() {
468         if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
469             return trackRect.width;
470         }
471         return trackRect.height;
472     }
473
474     /**
475      * Returns the amount that the thumb goes past the slide bar.
476      */

477     protected int getThumbOverhang() {
478         return (int)(getThumbSize().getHeight()-getTrackWidth())/2;
479     }
480
481     protected void scrollDueToClickInTrack( int dir ) {
482         scrollByUnit( dir );
483     }
484
485     protected void paintMinorTickForHorizSlider( Graphics JavaDoc g, Rectangle JavaDoc tickBounds, int x ) {
486         g.setColor( slider.isEnabled() ? slider.getForeground() : MetalLookAndFeel.getControlShadow() );
487         g.drawLine( x, TICK_BUFFER, x, TICK_BUFFER + (tickLength / 2) );
488     }
489
490     protected void paintMajorTickForHorizSlider( Graphics JavaDoc g, Rectangle JavaDoc tickBounds, int x ) {
491         g.setColor( slider.isEnabled() ? slider.getForeground() : MetalLookAndFeel.getControlShadow() );
492         g.drawLine( x, TICK_BUFFER , x, TICK_BUFFER + (tickLength - 1) );
493     }
494
495     protected void paintMinorTickForVertSlider( Graphics JavaDoc g, Rectangle JavaDoc tickBounds, int y ) {
496         g.setColor( slider.isEnabled() ? slider.getForeground() : MetalLookAndFeel.getControlShadow() );
497
498     if (MetalUtils.isLeftToRight(slider)) {
499         g.drawLine( TICK_BUFFER, y, TICK_BUFFER + (tickLength / 2), y );
500     }
501     else {
502         g.drawLine( 0, y, tickLength/2, y );
503     }
504     }
505
506     protected void paintMajorTickForVertSlider( Graphics JavaDoc g, Rectangle JavaDoc tickBounds, int y ) {
507         g.setColor( slider.isEnabled() ? slider.getForeground() : MetalLookAndFeel.getControlShadow() );
508
509     if (MetalUtils.isLeftToRight(slider)) {
510         g.drawLine( TICK_BUFFER, y, TICK_BUFFER + tickLength, y );
511     }
512     else {
513         g.drawLine( 0, y, tickLength, y );
514     }
515     }
516 }
517
Popular Tags