Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / plaf / metal / MetalSliderUI.java
blobf97717f31e083d1f5a019e9fa38dcc9d262c6aaa
1 /* MetalSliderUI.java
2 Copyright (C) 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.plaf.metal;
41 import java.awt.Color;
42 import java.awt.Dimension;
43 import java.awt.Graphics;
44 import java.awt.Rectangle;
45 import java.beans.PropertyChangeEvent;
46 import java.beans.PropertyChangeListener;
48 import javax.swing.Icon;
49 import javax.swing.JComponent;
50 import javax.swing.JSlider;
51 import javax.swing.UIManager;
52 import javax.swing.plaf.ComponentUI;
53 import javax.swing.plaf.basic.BasicGraphicsUtils;
54 import javax.swing.plaf.basic.BasicSliderUI;
56 /**
57 * A UI delegate for the {@link JSlider} component.
59 public class MetalSliderUI extends BasicSliderUI
61 /**
62 * A property change handler that updates the rendered component in response
63 * to specific property change events. This custom handler is used to
64 * intercept the "JSlider.isFilled" property, which is only recognised by
65 * the {@link MetalLookAndFeel}.
67 protected class MetalPropertyListener
68 extends BasicSliderUI.PropertyChangeHandler
70 /**
71 * Creates a new listener.
73 protected MetalPropertyListener()
75 // Nothing to do here.
78 /**
79 * Handles property change events. Events with the name "JSlider.isFilled"
80 * are handled here, and other events are passed to the superclass.
82 * @param e the property change event.
84 public void propertyChange(PropertyChangeEvent e)
86 if (e.getPropertyName().equals(SLIDER_FILL))
88 Boolean b = (Boolean) e.getNewValue();
89 if (b == null)
90 filledSlider = false;
91 else
92 filledSlider = b.booleanValue();
94 else
95 super.propertyChange(e);
99 /** The thumb color (unused, because an icon is used to draw the thumb). */
100 protected static Color thumbColor;
102 /**
103 * The highlight color used for drawing the track rect when the slider is
104 * enabled.
106 protected static Color highlightColor;
109 * The shadow color used for drawing the track rect when the slider is
110 * enabled.
112 protected static Color darkShadowColor;
114 /** The track width. */
115 protected static int trackWidth = UIManager.getInt("Slider.trackWidth");
117 /** The length of the major tick marks. */
118 protected static int tickLength = UIManager.getInt("Slider.majorTickLength");
120 /** The icon used for the thumb control of horizontally oriented sliders. */
121 protected static Icon horizThumbIcon = UIManager.getIcon(
122 "Slider.horizontalThumbIcon");
124 /** The icon used for the thumb control of vertically oriented sliders. */
125 protected static Icon vertThumbIcon = UIManager.getIcon(
126 "Slider.verticalThumbIcon");
128 /** The gap between the track and the tick marks. */
129 protected final int TICK_BUFFER = 4;
131 /** A key to look up the filledSlider setting in the {@link UIManager}. */
132 protected final String SLIDER_FILL = "JSlider.isFilled";
134 /**
135 * A flag that controls whether or not the track is filled up to the value
136 * of the slider.
138 protected boolean filledSlider;
141 * Constructs a new instance.
143 public MetalSliderUI()
145 super(null);
146 filledSlider = UIManager.getBoolean(SLIDER_FILL);
147 darkShadowColor = MetalLookAndFeel.getControlDarkShadow();
148 highlightColor = MetalLookAndFeel.getControlHighlight();
152 * Returns a new instance of <code>MetalSliderUI</code>.
154 * @param component the component (ignored).
156 * @return A new instance of <code>MetalSliderUI</code>.
158 public static ComponentUI createUI(JComponent component)
160 return new MetalSliderUI();
164 * Installs the default for this UI delegate in the supplied component.
166 * @param c the component.
168 public void installUI(JComponent c)
170 super.installUI(c);
171 Boolean b = (Boolean) c.getClientProperty(SLIDER_FILL);
172 if (b != null)
173 filledSlider = b.booleanValue();
177 * Creates a property change listener for the slider.
179 * @param slider the slider.
181 * @return A new instance of {@link MetalPropertyListener}.
183 protected PropertyChangeListener createPropertyChangeListener(JSlider slider)
185 return new MetalPropertyListener();
189 * Paints the thumb icon for the slider.
191 * @param g the graphics device.
193 public void paintThumb(Graphics g)
195 if (slider.getOrientation() == JSlider.HORIZONTAL)
196 horizThumbIcon.paintIcon(slider, g, thumbRect.x, thumbRect.y);
197 else
198 vertThumbIcon.paintIcon(slider, g, thumbRect.x, thumbRect.y);
202 * Paints the track along which the thumb control moves.
204 * @param g the graphics device.
206 public void paintTrack(Graphics g)
208 Color shadowColor = MetalLookAndFeel.getControlShadow();
209 if (slider.getOrientation() == JSlider.HORIZONTAL)
211 int trackX = trackRect.x;
212 int trackY = trackRect.y + (trackRect.height - getTrackWidth()) / 2;
213 int trackW = trackRect.width;
214 int trackH = getTrackWidth();
216 // draw border
217 if (slider.isEnabled())
218 BasicGraphicsUtils.drawEtchedRect(g, trackX, trackY, trackW, trackH,
219 darkShadowColor, shadowColor, darkShadowColor, highlightColor);
220 else
222 g.setColor(MetalLookAndFeel.getControlShadow());
223 g.drawRect(trackX, trackY, trackW - 2, trackH - 2);
226 // fill track (if required)
227 if (MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme)
229 if (slider.isEnabled())
231 int xPos = xPositionForValue(slider.getValue());
232 int x = (slider.getInverted() ? xPos : trackRect.x);
233 int w = (slider.getInverted() ? trackX + trackW - xPos
234 : xPos - trackRect.x);
235 g.setColor(MetalLookAndFeel.getWhite());
236 g.drawLine(x + 1, trackY + 1, x + w - 3, trackY + 1);
237 g.setColor(UIManager.getColor("Slider.altTrackColor"));
238 g.drawLine(x + 1, trackY + 2, x + w - 3, trackY + 2);
239 g.setColor(MetalLookAndFeel.getControlShadow());
240 g.drawLine(x + 1, trackY + 3, x + w - 3, trackY + 3);
241 g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
242 g.drawLine(x + 1, trackY + 4, x + w - 3, trackY + 4);
245 else if (filledSlider)
247 int xPos = xPositionForValue(slider.getValue());
248 int x = (slider.getInverted() ? xPos : trackRect.x);
249 int w = (slider.getInverted() ? trackX + trackW - xPos
250 : xPos - trackRect.x);
251 g.setColor(MetalLookAndFeel.getControlShadow());
252 g.fillRect(x + 1, trackY + 1, w - 3, getTrackWidth() - 3);
253 if (slider.isEnabled())
255 g.setColor(MetalLookAndFeel.getControl());
256 g.drawLine(x + 1, trackY + 1, x + w - 3, trackY + 1);
257 g.drawLine(x + 1, trackY + 1, x + 1,
258 trackY + getTrackWidth() - 3);
262 else
264 int trackX = trackRect.x + (trackRect.width - getTrackWidth()) / 2;
265 int trackY = trackRect.y;
266 int trackW = getTrackWidth();
267 int trackH = trackRect.height;
268 if (slider.isEnabled())
269 BasicGraphicsUtils.drawEtchedRect(g, trackX, trackY, trackW, trackH,
270 darkShadowColor, shadowColor, darkShadowColor, highlightColor);
271 else
273 g.setColor(MetalLookAndFeel.getControlShadow());
274 g.drawRect(trackX, trackY, trackW - 2, trackH - 2);
277 // Fill track if necessary.
278 if (MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme)
280 if (slider.isEnabled())
282 int yPos = yPositionForValue(slider.getValue());
283 int y = (slider.getInverted() ? trackY : yPos);
284 int h = (slider.getInverted() ? yPos - trackY
285 : trackY + trackH - yPos);
287 g.setColor(MetalLookAndFeel.getWhite());
288 g.drawLine(trackX + 1, y + 1, trackX + 1, y + h - 3);
289 g.setColor(UIManager.getColor("Slider.altTrackColor"));
290 g.drawLine(trackX + 2, y + 1, trackX + 2, y + h - 3);
291 g.setColor(MetalLookAndFeel.getControlShadow());
292 g.drawLine(trackX + 3, y + 1, trackX + 3, y + h - 3);
293 g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
294 g.drawLine(trackX + 4, y + 1, trackX + 4, y + h - 3);
297 else if (filledSlider)
299 int yPos = yPositionForValue(slider.getValue());
300 int y = (slider.getInverted() ? trackY : yPos);
301 int h = (slider.getInverted() ? yPos - trackY
302 : trackY + trackH - yPos);
303 g.setColor(MetalLookAndFeel.getControlShadow());
304 g.fillRect(trackX + 1, y + 1, getTrackWidth() - 3, h - 3);
305 if (slider.isEnabled())
307 g.setColor(MetalLookAndFeel.getControl());
308 g.drawLine(trackX + 1, y + 1, trackX + trackW - 3, y + 1);
309 g.drawLine(trackX + 1, y + 1, trackX + 1, y + h - 3);
316 * Draws the focus rectangle for the slider. The Metal look and feel
317 * indicates that the {@link JSlider} has the focus by changing the color of
318 * the thumb control - this is handled elsewhere and so this method is empty
319 * (it overrides the method in the {@link BasicSliderUI} class to prevent
320 * a default focus highlight from being drawn).
322 * @param g the graphics device.
324 public void paintFocus(Graphics g)
326 // do nothing as focus is shown by different color on thumb control
330 * Returns the size of the thumb icon.
332 * @return The size of the thumb icon.
334 protected Dimension getThumbSize()
336 if (slider.getOrientation() == JSlider.HORIZONTAL)
337 return new Dimension(horizThumbIcon.getIconWidth(),
338 horizThumbIcon.getIconHeight());
339 else
340 return new Dimension(vertThumbIcon.getIconWidth(),
341 vertThumbIcon.getIconHeight());
345 * Returns the length of the major tick marks.
347 * @return The length of the major tick marks.
349 public int getTickLength()
351 return tickLength + TICK_BUFFER;
355 * Returns the track width.
357 * @return The track width.
359 protected int getTrackWidth()
361 return trackWidth;
365 * Returns the track length.
367 * @return The track length.
369 protected int getTrackLength()
371 return (slider.getOrientation() == JSlider.HORIZONTAL
372 ? tickRect.width : tickRect.height);
376 * Returns the thumb overhang.
378 * @return The thumb overhang.
380 protected int getThumbOverhang()
382 // FIXME: for what might this method be used?
383 return 0;
386 protected void scrollDueToClickInTrack(int dir)
388 // FIXME: for what might this method be overridden?
389 super.scrollDueToClickInTrack(dir);
393 * Paints the minor ticks for a slider with a horizontal orientation.
395 * @param g the graphics device.
396 * @param tickBounds the tick bounds.
397 * @param x the x value for the tick.
399 protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds,
400 int x)
402 // Note the incoming 'g' has a translation in place to get us to the
403 // start of the tick rect already...
404 if (slider.isEnabled())
405 g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
406 else
407 g.setColor(MetalLookAndFeel.getControlDisabled());
408 g.drawLine(x, TICK_BUFFER, x, TICK_BUFFER + tickLength / 2);
412 * Paints the major ticks for a slider with a horizontal orientation.
414 * @param g the graphics device.
415 * @param tickBounds the tick bounds.
416 * @param x the x value for the tick.
418 protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds,
419 int x)
421 // Note the incoming 'g' has a translation in place to get us to the
422 // start of the tick rect already...
423 if (slider.isEnabled())
424 g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
425 else
426 g.setColor(MetalLookAndFeel.getControlDisabled());
427 g.drawLine(x, TICK_BUFFER, x, TICK_BUFFER + tickLength);
431 * Paints the minor ticks for a slider with a vertical orientation.
433 * @param g the graphics device.
434 * @param tickBounds the tick bounds.
435 * @param y the y value for the tick.
437 protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds,
438 int y)
440 // Note the incoming 'g' has a translation in place to get us to the
441 // start of the tick rect already...
442 if (slider.isEnabled())
443 g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
444 else
445 g.setColor(MetalLookAndFeel.getControlDisabled());
446 g.drawLine(TICK_BUFFER - 1, y, TICK_BUFFER - 1 + tickLength / 2, y);
450 * Paints the major ticks for a slider with a vertical orientation.
452 * @param g the graphics device.
453 * @param tickBounds the tick bounds.
454 * @param y the y value for the tick.
456 protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds,
457 int y)
459 // Note the incoming 'g' has a translation in place to get us to the
460 // start of the tick rect already...
461 if (slider.isEnabled())
462 g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
463 else
464 g.setColor(MetalLookAndFeel.getControlDisabled());
465 g.drawLine(TICK_BUFFER - 1, y, TICK_BUFFER - 1 + tickLength, y);