diff options
author | David Gilbert <david.gilbert@object-refinery.com> | 2005-07-21 14:13:36 +0000 |
---|---|---|
committer | David Gilbert <david.gilbert@object-refinery.com> | 2005-07-21 14:13:36 +0000 |
commit | eab0be4420ffff52b390bce5d2092f779eefc8d7 (patch) | |
tree | 623ff3fd9f90e22cc703ea9f44e0572c459b75fc | |
parent | 5153e4e7cef58cf1132177651b4ed7f2b4a805b8 (diff) | |
download | classpath-eab0be4420ffff52b390bce5d2092f779eefc8d7.tar.gz |
2005-07-21 David Gilbert <david.gilbert@object-refinery.com>
* javax/swing/plaf/basic/BasicSliderUI.java:
(thumbHeight): removed,
(thumbWidth): removed,
(tickHeight): removed,
(installDefaults): deleted initialisation of thumbHeight, thumbWidth
and thumbRect,
(getPreferredHorizontalSize): changed source of thumb height and width,
(getPreferredVerticalSize): likewise,
(getMinimumHorizontalSize): reimplemented,
(getMinimumVerticalSize): reimplemented,
(getMinimumSize): reimplemented,
(getMaximumSize): reimplemented,
(calculateThumbSize): use getThumbSize(),
(calculateThumbLocation): use trackRect not contentRect,
(calculateTrackBuffer): use half the thumbRect,
(getThumbSize): use constant size,
(calculateTrackRect): move track down to middle of contentRect,
(getTickLength): return constant,
(paintTrack): removed unused local variables,
(paintTicks): apply a translation to g temporarily before calling
methods to draw ticks, add 0.5 to solve rounding problem,
(paintMinorTickForHorizSlider): modified to account for translation
already applied to g,
(paintMajorTickForHorizSlider): likewise,
(paintMinorTickForVertSlider): likewise,
(paintMajorTickForVertSlider): likewise,
(xPositionForValue): subtract 1 from width,
(yPositionForValue): likewise,
*javax/swing/plaf/metal/MetalLookAndFeel.java
(initComponentDefaults): add slider defaults,
*javax/swing/plaf/metal/MetalSliderUI.java: implemented missing
methods,
* examples/gnu/classpath/examples/swing/Demo.java
(mkSliders): added minor ticks and labels.
-rw-r--r-- | ChangeLog | 47 | ||||
-rw-r--r-- | examples/gnu/classpath/examples/swing/Demo.java | 2 | ||||
-rw-r--r-- | javax/swing/plaf/basic/BasicSliderUI.java | 221 | ||||
-rw-r--r-- | javax/swing/plaf/metal/MetalLookAndFeel.java | 9 | ||||
-rw-r--r-- | javax/swing/plaf/metal/MetalSliderUI.java | 263 |
5 files changed, 440 insertions, 102 deletions
@@ -1,3 +1,40 @@ +2005-07-21 David Gilbert <david.gilbert@object-refinery.com> + + * javax/swing/plaf/basic/BasicSliderUI.java: + (thumbHeight): removed, + (thumbWidth): removed, + (tickHeight): removed, + (installDefaults): deleted initialisation of thumbHeight, thumbWidth + and thumbRect, + (getPreferredHorizontalSize): changed source of thumb height and width, + (getPreferredVerticalSize): likewise, + (getMinimumHorizontalSize): reimplemented, + (getMinimumVerticalSize): reimplemented, + (getMinimumSize): reimplemented, + (getMaximumSize): reimplemented, + (calculateThumbSize): use getThumbSize(), + (calculateThumbLocation): use trackRect not contentRect, + (calculateTrackBuffer): use half the thumbRect, + (getThumbSize): use constant size, + (calculateTrackRect): move track down to middle of contentRect, + (getTickLength): return constant, + (paintTrack): removed unused local variables, + (paintTicks): apply a translation to g temporarily before calling + methods to draw ticks, add 0.5 to solve rounding problem, + (paintMinorTickForHorizSlider): modified to account for translation + already applied to g, + (paintMajorTickForHorizSlider): likewise, + (paintMinorTickForVertSlider): likewise, + (paintMajorTickForVertSlider): likewise, + (xPositionForValue): subtract 1 from width, + (yPositionForValue): likewise, + *javax/swing/plaf/metal/MetalLookAndFeel.java + (initComponentDefaults): add slider defaults, + *javax/swing/plaf/metal/MetalSliderUI.java: implemented missing + methods, + * examples/gnu/classpath/examples/swing/Demo.java + (mkSliders): added minor ticks and labels. + 2005-07-21 Roman Kennke <roman@kennke.org> * javax/swing/AbstractButton.java @@ -55,15 +92,15 @@ set the size and make the InternalFrame visible. This is done in createInternalFrame. -2005-07-20 David Gilbert <david.gilbert@object-refinery.com> +2005-07-21 David Gilbert <david.gilbert@object-refinery.com> * javax/swing/DefaultListSelectionModel.java: API doc fixes. -2005-07-20 David Gilbert <david.gilbert@object-refinery.com> +2005-07-21 David Gilbert <david.gilbert@object-refinery.com> * javax/swing/AbstractButton.java: API doc fixes all over. -2005-07-20 David Gilbert <david.gilbert@object-refinery.com> +2005-07-21 David Gilbert <david.gilbert@object-refinery.com> * javax/swing/UIDefaults.java (getFont(Object, Locale)): matched argument name to API doc comment, @@ -76,11 +113,11 @@ (getInsets(Object, Locale)): likewise, (getDimension(Object, Locale)): likewise. -2005-07-20 David Gilbert <david.gilbert@object-refinery.com> +2005-07-21 David Gilbert <david.gilbert@object-refinery.com> * javax/swing/JComponent.java: API doc fixes all over. -2005-07-20 David Gilbert <david.gilbert@object-refinery.com> +2005-07-21 David Gilbert <david.gilbert@object-refinery.com> * java/net/URLClassLoader.java: reordered some API doc comments to suppress Eclipse warnings, and fixed API doc link. diff --git a/examples/gnu/classpath/examples/swing/Demo.java b/examples/gnu/classpath/examples/swing/Demo.java index 4471be155..eef300619 100644 --- a/examples/gnu/classpath/examples/swing/Demo.java +++ b/examples/gnu/classpath/examples/swing/Demo.java @@ -658,6 +658,8 @@ public class Demo slider.setPaintTrack(true); slider.setPaintTicks(true); slider.setMajorTickSpacing(30); + slider.setMinorTickSpacing(5); + slider.setPaintLabels(true); slider.setInverted(false); JProgressBar progress = new JProgressBar(); BoundedRangeModel model = new DefaultBoundedRangeModel(10, 1, 0, 100); diff --git a/javax/swing/plaf/basic/BasicSliderUI.java b/javax/swing/plaf/basic/BasicSliderUI.java index 2b87fac1c..0b4058429 100644 --- a/javax/swing/plaf/basic/BasicSliderUI.java +++ b/javax/swing/plaf/basic/BasicSliderUI.java @@ -470,15 +470,6 @@ public class BasicSliderUI extends SliderUI } } - /** The preferred height of the thumb. */ - private transient int thumbHeight; - - /** The preferred width of the thumb. */ - private transient int thumbWidth; - - /** The preferred height of the tick rectangle. */ - private transient int tickHeight; - /** Listener for changes from the model. */ protected ChangeListener changeListener; @@ -698,11 +689,6 @@ public class BasicSliderUI extends SliderUI focusColor = defaults.getColor("Slider.focus"); slider.setBorder(defaults.getBorder("Slider.border")); slider.setOpaque(true); - - thumbHeight = defaults.getInt("Slider.thumbHeight"); - thumbWidth = defaults.getInt("Slider.thumbWidth"); - tickHeight = defaults.getInt("Slider.tickHeight"); - focusInsets = defaults.getInsets("Slider.focusInsets"); } @@ -899,11 +885,11 @@ public class BasicSliderUI extends SliderUI width += insets.left + insets.right + focusInsets.left + focusInsets.right; // Height is determined by the thumb, the ticks and the labels. - int height = thumbHeight; + int height = getThumbSize().height; if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 || slider.getMinorTickSpacing() > 0) - height += tickHeight; + height += getTickLength(); if (slider.getPaintLabels()) height += getHeightOfTallestLabel(); @@ -934,11 +920,11 @@ public class BasicSliderUI extends SliderUI height += insets.top + insets.bottom + focusInsets.top + focusInsets.bottom; - int width = thumbHeight; + int width = getThumbSize().width; if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 || slider.getMinorTickSpacing() > 0) - width += tickHeight; + width += getTickLength(); if (slider.getPaintLabels()) width += getWidthOfWidestLabel(); @@ -956,7 +942,21 @@ public class BasicSliderUI extends SliderUI */ public Dimension getMinimumHorizontalSize() { - return getPreferredHorizontalSize(); + Insets insets = slider.getInsets(); + // Height is determined by the thumb, the ticks and the labels. + int height = getThumbSize().height; + + if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 + || slider.getMinorTickSpacing() > 0) + height += getTickLength(); + + if (slider.getPaintLabels()) + height += getHeightOfTallestLabel(); + + height += insets.top + insets.bottom + focusInsets.top + + focusInsets.bottom; + + return new Dimension(36, height); } /** @@ -967,7 +967,19 @@ public class BasicSliderUI extends SliderUI */ public Dimension getMinimumVerticalSize() { - return getPreferredVerticalSize(); + Insets insets = slider.getInsets(); + int width = getThumbSize().width; + + if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 + || slider.getMinorTickSpacing() > 0) + width += getTickLength(); + + if (slider.getPaintLabels()) + width += getWidthOfWidestLabel(); + + width += insets.left + insets.right + focusInsets.left + focusInsets.right; + + return new Dimension(width, 36); } /** @@ -999,15 +1011,14 @@ public class BasicSliderUI extends SliderUI public Dimension getMinimumSize(JComponent c) { if (slider.getOrientation() == JSlider.HORIZONTAL) - return getPreferredHorizontalSize(); + return getMinimumHorizontalSize(); else - return getPreferredVerticalSize(); + return getMinimumVerticalSize(); } /** * This method returns the maximum size for this {@link JSlider} for this - * look and feel. If it returns null, then it is up to the Layout Manager - * to give the {@link JComponent} a size. + * look and feel. * * @param c The {@link JComponent} to find a maximum size for. * @@ -1015,10 +1026,40 @@ public class BasicSliderUI extends SliderUI */ public Dimension getMaximumSize(JComponent c) { + Insets insets = slider.getInsets(); if (slider.getOrientation() == JSlider.HORIZONTAL) - return getPreferredHorizontalSize(); + { + // Height is determined by the thumb, the ticks and the labels. + int height = getThumbSize().height; + + if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 + || slider.getMinorTickSpacing() > 0) + height += getTickLength(); + + if (slider.getPaintLabels()) + height += getHeightOfTallestLabel(); + + height += insets.top + insets.bottom + focusInsets.top + + focusInsets.bottom; + + return new Dimension(32767, height); + } else - return getPreferredVerticalSize(); + { + int width = getThumbSize().width; + + if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 + || slider.getMinorTickSpacing() > 0) + width += getTickLength(); + + if (slider.getPaintLabels()) + width += getWidthOfWidestLabel(); + + width += insets.left + insets.right + focusInsets.left + + focusInsets.right; + + return new Dimension(width, 32767); + } } /** @@ -1045,7 +1086,6 @@ public class BasicSliderUI extends SliderUI { insetCache = slider.getInsets(); focusRect = SwingUtilities.calculateInnerArea(slider, focusRect); - if (focusRect.width < 0) focusRect.width = 0; if (focusRect.height < 0) @@ -1058,30 +1098,13 @@ public class BasicSliderUI extends SliderUI */ protected void calculateThumbSize() { + Dimension d = getThumbSize(); + thumbRect.width = d.width; + thumbRect.height = d.height; if (slider.getOrientation() == JSlider.HORIZONTAL) - { - if (thumbWidth > contentRect.width) - thumbRect.width = contentRect.width / 4; - else - thumbRect.width = thumbWidth; - if (thumbHeight > contentRect.height) - thumbRect.height = contentRect.height; - else - thumbRect.height = thumbHeight; - } + thumbRect.y = trackRect.y; else - { - // The thumb gets flipped when inverted, so thumbWidth - // actually is the height and vice versa. - if (thumbWidth > contentRect.height) - thumbRect.height = contentRect.height / 4; - else - thumbRect.height = thumbWidth; - if (thumbHeight > contentRect.width) - thumbRect.width = contentRect.width; - else - thumbRect.width = thumbHeight; - } + thumbRect.x = trackRect.x; } /** @@ -1092,9 +1115,10 @@ public class BasicSliderUI extends SliderUI { contentRect.x = focusRect.x + focusInsets.left; contentRect.y = focusRect.y + focusInsets.top; + contentRect.width = focusRect.width - focusInsets.left - focusInsets.right; - contentRect.height = focusRect.height - focusInsets.top - - focusInsets.bottom; + contentRect.height = focusRect.height - focusInsets.top + - focusInsets.bottom; if (contentRect.width < 0) contentRect.width = 0; @@ -1113,11 +1137,11 @@ public class BasicSliderUI extends SliderUI if (slider.getOrientation() == JSlider.HORIZONTAL) { thumbRect.x = xPositionForValue(value) - thumbRect.width / 2; - thumbRect.y = contentRect.y; + thumbRect.y = trackRect.y; } else { - thumbRect.x = contentRect.x; + thumbRect.x = trackRect.x; thumbRect.y = yPositionForValue(value) - thumbRect.height / 2; } } @@ -1129,9 +1153,9 @@ public class BasicSliderUI extends SliderUI protected void calculateTrackBuffer() { if (slider.getOrientation() == JSlider.HORIZONTAL) - trackBuffer = thumbRect.width; + trackBuffer = thumbRect.width / 2; else - trackBuffer = thumbRect.height; + trackBuffer = thumbRect.height / 2; } /** @@ -1141,9 +1165,11 @@ public class BasicSliderUI extends SliderUI */ protected Dimension getThumbSize() { - // This is really just the bounds box for the thumb. - // The thumb will actually be pointed (like a rectangle + triangle at bottom) - return thumbRect.getSize(); + // TODO: shouldn't create new objects every time + if (slider.getOrientation() == JSlider.HORIZONTAL) + return new Dimension(11, 20); + else + return new Dimension(20, 11); } /** @@ -1155,13 +1181,21 @@ public class BasicSliderUI extends SliderUI if (slider.getOrientation() == JSlider.HORIZONTAL) { trackRect.x = contentRect.x + trackBuffer; - trackRect.y = contentRect.y; + int h = getThumbSize().height; + if (slider.getPaintTicks() && (slider.getMajorTickSpacing() > 0 + || slider.getMinorTickSpacing() > 0)) + h += getTickLength(); + trackRect.y = contentRect.y + (contentRect.height - h) / 2 - 1; trackRect.width = contentRect.width - 2 * trackBuffer; trackRect.height = thumbRect.height; } else { - trackRect.x = contentRect.x; + int w = getThumbSize().width; + if (slider.getPaintTicks() && (slider.getMajorTickSpacing() > 0 + || slider.getMinorTickSpacing() > 0)) + w += getTickLength(); + trackRect.x = contentRect.x + (contentRect.width - w) / 2 - 1; trackRect.y = contentRect.y + trackBuffer; trackRect.width = thumbRect.width; trackRect.height = contentRect.height - 2 * trackBuffer; @@ -1180,7 +1214,7 @@ public class BasicSliderUI extends SliderUI */ protected int getTickLength() { - return tickHeight; + return 8; } /** @@ -1536,9 +1570,6 @@ public class BasicSliderUI extends SliderUI Point c = new Point(a); Point d = new Point(a); - Polygon high; - Polygon shadow; - if (slider.getOrientation() == JSlider.HORIZONTAL) { width = trackRect.width; @@ -1591,74 +1622,78 @@ public class BasicSliderUI extends SliderUI { if (slider.getOrientation() == JSlider.HORIZONTAL) { - double loc = tickRect.x; + double loc = tickRect.x + 0.5; double increment = (max == min) ? 0 - : majorSpace * (double) tickRect.width / (max - - min); - if (drawInverted()) + : majorSpace * (double) (tickRect.width - 1) / (max - min); + if (drawInverted()) { loc += tickRect.width; increment *= -1; } + g.translate(0, tickRect.y); for (int i = min; i <= max; i += majorSpace) { paintMajorTickForHorizSlider(g, tickRect, (int) loc); loc += increment; } + g.translate(0, -tickRect.y); } else { - double loc = tickRect.height + tickRect.y; + double loc = tickRect.height + tickRect.y + 0.5; double increment = (max == min) ? 0 - : -majorSpace * (double) tickRect.height / (max - - min); + : -majorSpace * (double) (tickRect.height - 1) / (max - min); if (drawInverted()) { - loc = tickRect.y; + loc = tickRect.y + 0.5; increment *= -1; } + g.translate(tickRect.x, 0); for (int i = min; i <= max; i += majorSpace) { paintMajorTickForVertSlider(g, tickRect, (int) loc); loc += increment; } + g.translate(-tickRect.x, 0); } } if (minorSpace > 0) { if (slider.getOrientation() == JSlider.HORIZONTAL) { - double loc = tickRect.x; + double loc = tickRect.x + 0.5; double increment = (max == min) ? 0 - : minorSpace * (double) tickRect.width / (max - - min); + : minorSpace * (double) (tickRect.width - 1) / (max - min); if (drawInverted()) { loc += tickRect.width; increment *= -1; } + g.translate(0, tickRect.y); for (int i = min; i <= max; i += minorSpace) { paintMinorTickForHorizSlider(g, tickRect, (int) loc); loc += increment; } + g.translate(0, -tickRect.y); } else { - double loc = tickRect.height + tickRect.y; + double loc = tickRect.height + tickRect.y + 0.5; double increment = (max == min) ? 0 - : -minorSpace * (double) tickRect.height / (max - - min); + : -minorSpace * (double) (tickRect.height - 1) / (max - min); if (drawInverted()) { - loc = tickRect.y; + loc = tickRect.y + 0.5; increment *= -1; } + g.translate(tickRect.x, 0); for (int i = min; i <= max; i += minorSpace) { paintMinorTickForVertSlider(g, tickRect, (int) loc); loc += increment; } + g.translate(-tickRect.x, 0); } } } @@ -1680,7 +1715,7 @@ public class BasicSliderUI extends SliderUI protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) { - int y = tickRect.y + tickRect.height / 4; + int y = tickRect.height / 4; Color saved = g.getColor(); g.setColor(Color.BLACK); @@ -1699,7 +1734,7 @@ public class BasicSliderUI extends SliderUI protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) { - int y = tickRect.y + tickRect.height / 4; + int y = tickRect.height / 4; Color saved = g.getColor(); g.setColor(Color.BLACK); @@ -1718,7 +1753,7 @@ public class BasicSliderUI extends SliderUI protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) { - int x = tickRect.x + tickRect.width / 4; + int x = tickRect.width / 4; Color saved = g.getColor(); g.setColor(Color.BLACK); @@ -1737,7 +1772,7 @@ public class BasicSliderUI extends SliderUI protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) { - int x = tickRect.x + tickRect.width / 4; + int x = tickRect.width / 4; Color saved = g.getColor(); g.setColor(Color.BLACK); @@ -2074,8 +2109,7 @@ public class BasicSliderUI extends SliderUI { int min = slider.getMinimum(); int max = slider.getMaximum(); - int extent = slider.getExtent(); - int len = trackRect.width; + int len = trackRect.width - 1; int xPos = (max == min) ? 0 : (value - min) * len / (max - min); @@ -2083,7 +2117,7 @@ public class BasicSliderUI extends SliderUI xPos += trackRect.x; else { - xPos = trackRect.width - xPos; + xPos = len - xPos; xPos += trackRect.x; } return xPos; @@ -2100,14 +2134,13 @@ public class BasicSliderUI extends SliderUI { int min = slider.getMinimum(); int max = slider.getMaximum(); - int extent = slider.getExtent(); - int len = trackRect.height; + int len = trackRect.height - 1; int yPos = (max == min) ? 0 : (value - min) * len / (max - min); if (! drawInverted()) { - yPos = trackRect.height - yPos; + yPos = len - yPos; yPos += trackRect.y; } else @@ -2132,8 +2165,9 @@ public class BasicSliderUI extends SliderUI int value; - // If the length is 0, you shouldn't be able to even see where the slider is. - // This really shouldn't ever happen, but just in case, we'll return the middle. + // If the length is 0, you shouldn't be able to even see where the slider + // is. This really shouldn't ever happen, but just in case, we'll return + // the middle. if (len == 0) return ((max - min) / 2); @@ -2167,8 +2201,9 @@ public class BasicSliderUI extends SliderUI int value; - // If the length is 0, you shouldn't be able to even see where the slider is. - // This really shouldn't ever happen, but just in case, we'll return the middle. + // If the length is 0, you shouldn't be able to even see where the slider + // is. This really shouldn't ever happen, but just in case, we'll return + // the middle. if (len == 0) return ((max - min) / 2); diff --git a/javax/swing/plaf/metal/MetalLookAndFeel.java b/javax/swing/plaf/metal/MetalLookAndFeel.java index 52315af59..36d7d6cda 100644 --- a/javax/swing/plaf/metal/MetalLookAndFeel.java +++ b/javax/swing/plaf/metal/MetalLookAndFeel.java @@ -47,6 +47,7 @@ import javax.swing.UIDefaults; import javax.swing.plaf.ColorUIResource; import javax.swing.plaf.FontUIResource; import javax.swing.plaf.IconUIResource; +import javax.swing.plaf.InsetsUIResource; import javax.swing.plaf.basic.BasicLookAndFeel; /** @@ -813,6 +814,14 @@ public class MetalLookAndFeel extends BasicLookAndFeel "SplitPane.highlight", new ColorUIResource(getControlHighlight()), + "Slider.focusInsets", new InsetsUIResource(0, 0, 0, 0), + "Slider.horizontalThumbIcon", + MetalIconFactory.getHorizontalSliderThumbIcon(), + "Slider.verticalThumbIcon", + MetalIconFactory.getVerticalSliderThumbIcon(), + "Slider.trackWidth", new Integer(7), + "Slider.majorTickLength", new Integer(6), + "Tree.openIcon", MetalIconFactory.getTreeFolderIcon(), "Tree.closedIcon", MetalIconFactory.getTreeFolderIcon(), "Tree.leafIcon", MetalIconFactory.getTreeLeafIcon(), diff --git a/javax/swing/plaf/metal/MetalSliderUI.java b/javax/swing/plaf/metal/MetalSliderUI.java index a857d6a9d..4b52c4b00 100644 --- a/javax/swing/plaf/metal/MetalSliderUI.java +++ b/javax/swing/plaf/metal/MetalSliderUI.java @@ -38,25 +38,72 @@ exception statement from your version. */ package javax.swing.plaf.metal; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Rectangle; +import java.beans.PropertyChangeListener; import java.util.HashMap; +import javax.swing.Icon; import javax.swing.JComponent; +import javax.swing.JSlider; +import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; +import javax.swing.plaf.basic.BasicGraphicsUtils; import javax.swing.plaf.basic.BasicSliderUI; +/** + * A UI delegate for the {@link JSlider} component. + */ public class MetalSliderUI extends BasicSliderUI { + // TODO: find a use for this + protected static Color thumbColor; + + // TODO: find a use for this + protected static Color highlightColor; + + // TODO: find a use for this + protected static Color darkShadowColor; + + /** The track width. */ + protected static int trackWidth = UIManager.getInt("Slider.trackWidth"); + + /** The length of the major tick marks. */ + protected static int tickLength = UIManager.getInt("Slider.majorTickLength"); + + /** The icon used for the thumb control of horizontally oriented sliders. */ + protected static Icon horizThumbIcon = UIManager.getIcon( + "Slider.horizontalThumbIcon"); + + /** The icon used for the thumb control of vertically oriented sliders. */ + protected static Icon vertThumbIcon = UIManager.getIcon( + "Slider.verticalThumbIcon"); + /** The gap between the track and the tick marks. */ + protected final int TICK_BUFFER = 4; + + /** + * A flag that controls whether or not the track is filled up to the value + * of the slider. + */ + protected boolean filledSlider; + + /** A key to look up the filledSlider setting in the {@link UIManager}. */ + protected final String SLIDER_FILL = "JSlider.isFilled"; + /** The UI instances for MetalSliderUIs */ private static HashMap instances; /** - * Constructs a new instance of MetalSliderUI. + * Constructs a new instance. */ public MetalSliderUI() { super(null); + filledSlider = UIManager.getBoolean(SLIDER_FILL); } /** @@ -71,17 +118,225 @@ public class MetalSliderUI if (instances == null) instances = new HashMap(); - Object o = instances.get(component); MetalSliderUI instance; if (o == null) { - instance = new MetalSliderUI(); - instances.put(component, instance); + instance = new MetalSliderUI(); + instances.put(component, instance); } else instance = (MetalSliderUI) o; return instance; } + + public void installUI(JComponent c) + { + super.installUI(c); + Boolean b = (Boolean) c.getClientProperty(SLIDER_FILL); + if (b != null) + filledSlider = b.booleanValue(); + } + + /** + * Paints the thumb icon for the slider. + * + * @param g the graphics device. + */ + public void paintThumb(Graphics g) + { + if (slider.getOrientation() == JSlider.HORIZONTAL) + horizThumbIcon.paintIcon(slider, g, thumbRect.x, thumbRect.y); + else + vertThumbIcon.paintIcon(slider, g, thumbRect.x, thumbRect.y); + } + + /** + * Creates a property change listener for the slider. + * + * @param slider the slider. + */ + protected PropertyChangeListener createPropertyChangeListener(JSlider slider) + { + // TODO: try to figure out why it might be necessary to override this + // method as is done in Sun's implementation + return super.createPropertyChangeListener(slider); + } + + /** + * Paints the track along which the thumb control moves. + * + * @param g the graphics device. + */ + public void paintTrack(Graphics g) + { + if (slider.getOrientation() == JSlider.HORIZONTAL) + { + if (filledSlider) + { + // TODO: fill the track + } + BasicGraphicsUtils.drawEtchedRect(g, trackRect.x, trackRect.y + + (trackRect.height - getTrackWidth()) / 2, trackRect.width - 1, + getTrackWidth(), Color.darkGray, Color.gray, Color.darkGray, + Color.white); + } + else + { + if (filledSlider) + { + // TODO: fill the track + } + BasicGraphicsUtils.drawEtchedRect(g, trackRect.x + (trackRect.width + - getTrackWidth()) / 2, trackRect.y, getTrackWidth(), + trackRect.height - 1, Color.darkGray, Color.gray, Color.darkGray, + Color.white); + } + } + + /** + * Draws the focus rectangle for the slider. The Metal look and feel + * indicates that the {@link JSlider} has the focus by changing the color of + * the thumb control - this is handled elsewhere and so this method is empty + * (it overrides the method in the {@link BasicSliderUI} class to prevent + * a default focus highlight from being drawn). + * + * @param g the graphics device. + */ + public void paintFocus(Graphics g) + { + // do nothing as focus is shown by different color on thumb control + } + + /** + * Returns the size of the thumb icon. + * + * @return The size of the thumb icon. + */ + protected Dimension getThumbSize() + { + if (slider.getOrientation() == JSlider.HORIZONTAL) + return new Dimension(horizThumbIcon.getIconWidth(), + horizThumbIcon.getIconHeight()); + else + return new Dimension(vertThumbIcon.getIconWidth(), + vertThumbIcon.getIconHeight()); + } + + /** + * Returns the length of the major tick marks. + * + * @return The length of the major tick marks. + */ + public int getTickLength() + { + return tickLength + TICK_BUFFER; + } + + /** + * Returns the track width. + * + * @return The track width. + */ + protected int getTrackWidth() + { + return trackWidth; + } + + /** + * Returns the track length. + * + * @return The track length. + */ + protected int getTrackLength() + { + return (slider.getOrientation() == JSlider.HORIZONTAL + ? tickRect.width : tickRect.height); + } + + /** + * Returns the thumb overhang. + * + * @return The thumb overhang. + */ + protected int getThumbOverhang() + { + // TODO: figure out what this is used for + return 0; + } + + protected void scrollDueToClickInTrack(int dir) + { + super.scrollDueToClickInTrack(dir); + } + + /** + * Paints the minor ticks for a slider with a horizontal orientation. + * + * @param g the graphics device. + * @param tickBounds the tick bounds. + * @param x the x value for the tick. + */ + protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, + int x) + { + // Note the incoming 'g' has a translation in place to get us to the + // start of the tick rect already... + // TODO: get color from UIManager... + g.setColor(new Color(153, 153, 204)); + g.drawLine(x, TICK_BUFFER, x, TICK_BUFFER + tickLength / 2); + } + + /** + * Paints the major ticks for a slider with a horizontal orientation. + * + * @param g the graphics device. + * @param tickBounds the tick bounds. + * @param x the x value for the tick. + */ + protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, + int x) + { + // Note the incoming 'g' has a translation in place to get us to the + // start of the tick rect already... + // TODO: get color from UIManager... + g.setColor(new Color(153, 153, 204)); + g.drawLine(x, TICK_BUFFER, x, TICK_BUFFER + tickLength); + } + + /** + * Paints the minor ticks for a slider with a vertical orientation. + * + * @param g the graphics device. + * @param tickBounds the tick bounds. + * @param y the y value for the tick. + */ + protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds, + int y) + { + // Note the incoming 'g' has a translation in place to get us to the + // start of the tick rect already... + // TODO: get color from UIManager... + g.setColor(new Color(153, 153, 204)); + g.drawLine(TICK_BUFFER - 1, y, TICK_BUFFER - 1 + tickLength / 2, y); + } + + /** + * Paints the major ticks for a slider with a vertical orientation. + * + * @param g the graphics device. + * @param tickBounds the tick bounds. + * @param y the y value for the tick. + */ + protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, + int y) + { + // Note the incoming 'g' has a translation in place to get us to the + // start of the tick rect already... + // TODO: get color from UIManager... + g.setColor(new Color(153, 153, 204)); + g.drawLine(TICK_BUFFER - 1, y, TICK_BUFFER - 1 + tickLength, y); + } + } |