[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[cp-patches] FYI: MetalSliderUI and MetalIconFactory changes
From: |
David Gilbert |
Subject: |
[cp-patches] FYI: MetalSliderUI and MetalIconFactory changes |
Date: |
Wed, 07 Sep 2005 11:24:17 +0000 |
User-agent: |
Mozilla Thunderbird 1.0.6 (X11/20050728) |
I committed this patch. It updates the MetalSliderUI to:
- handle the JSlider.isFilled property;
- fix the rendering of disabled sliders;
- fetch colors from the look and feel;
The MetalIconFactory modification is to draw the slider icons in a disabled
state.
2005-09-07 David Gilbert <address@hidden>
* javax/swing/plaf/metal/MetalIconFactory.java
(HorizontalSliderThumbIcon.paintIcon): handle disabled component state,
and fetch colors from the look and feel,
(VerticalSliderThumbIcon.paintIcon): likewise,
* javax/swing/plaf/metal/MetalSliderUI.java
(MetalPropertyListener): implemented,
(constructor): added color initialisation,
(createUI): reimplemented to return a new instance every time,
(createPropertyChangeListener): return a new instance of
MetalPropertyListener,
(paintTrack): reimplemented to handle track fill option, and the
disabled component state,
(paintMinorTickForHorizSlider): fetch colors from look and feel,
(paintMajorTickForHorizSlider): likewise,
(paintMinorTickForVertSlider): likewise,
(paintMajorTickForVertSlider): likewise.
Regards,
Dave Gilbert
Index: javax/swing/plaf/metal/MetalIconFactory.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalIconFactory.java,v
retrieving revision 1.9
diff -u -r1.9 MetalIconFactory.java
--- javax/swing/plaf/metal/MetalIconFactory.java 6 Sep 2005 19:57:34
-0000 1.9
+++ javax/swing/plaf/metal/MetalIconFactory.java 7 Sep 2005 09:57:48
-0000
@@ -523,13 +523,19 @@
*/
public void paintIcon(Component c, Graphics g, int x, int y)
{
+ boolean enabled = false;
boolean focus = false;
- if (c != null)
- focus = c.hasFocus();
- // TODO: pick up the colors from the look and feel
+ if (c != null)
+ {
+ enabled = c.isEnabled();
+ focus = c.hasFocus();
+ }
// draw the outline
- g.setColor(Color.black);
+ if (enabled)
+ g.setColor(MetalLookAndFeel.getBlack());
+ else
+ g.setColor(MetalLookAndFeel.getControlDarkShadow());
g.drawLine(x + 1, y, x + 13, y);
g.drawLine(x + 14, y + 1, x + 14, y + 7);
g.drawLine(x + 14, y + 8, x + 7, y + 15);
@@ -537,8 +543,11 @@
g.drawLine(x, y + 7, x, y + 1);
// fill the icon
- g.setColor(focus ? new Color(153, 153, 204) : new Color(204, 204, 204));
// medium
- g.fillRect(x + 2, y + 2, 12, 7);
+ if (focus)
+ g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
+ else
+ g.setColor(MetalLookAndFeel.getControl());
+ g.fillRect(x + 1, y + 2, 13, 7);
g.drawLine(x + 2, y + 9, x + 12, y + 9);
g.drawLine(x + 3, y + 10, x + 11, y + 10);
g.drawLine(x + 4, y + 11, x + 10, y + 11);
@@ -546,33 +555,42 @@
g.drawLine(x + 6, y + 13, x + 8, y + 13);
g.drawLine(x + 7, y + 14, x + 7, y + 14);
- // draw highlights
- g.setColor(focus ? new Color(204, 204, 255) : new Color(255, 255, 255));
// light
- g.drawLine(x + 1, y + 1, x + 13, y + 1);
- g.drawLine(x + 1, y + 2, x + 1, y + 8);
- g.drawLine(x + 2, y + 2, x + 2, y + 2);
- g.drawLine(x + 6, y + 2, x + 6, y + 2);
- g.drawLine(x + 10, y + 2, x + 10, y + 2);
-
- g.drawLine(x + 4, y + 4, x + 4, y + 4);
- g.drawLine(x + 8, y + 4, x + 8, y + 4);
-
- g.drawLine(x + 2, y + 6, x + 2, y + 6);
- g.drawLine(x + 6, y + 6, x + 6, y + 6);
- g.drawLine(x + 10, y + 6, x + 10, y + 6);
-
- // draw dots
- g.setColor(focus ? new Color(102, 102, 153) : Color.black);
// dark
- g.drawLine(x + 3, y + 3, x + 3, y + 3);
- g.drawLine(x + 7, y + 3, x + 7, y + 3);
- g.drawLine(x + 11, y + 3, x + 11, y + 3);
-
- g.drawLine(x + 5, y + 5, x + 5, y + 5);
- g.drawLine(x + 9, y + 5, x + 9, y + 5);
-
- g.drawLine(x + 3, y + 7, x + 3, y + 7);
- g.drawLine(x + 7, y + 7, x + 7, y + 7);
- g.drawLine(x + 11, y + 7, x + 11, y + 7);
+ // if the slider is enabled, draw dots and highlights
+ if (c.isEnabled())
+ {
+ if (focus)
+ g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
+ else
+ g.setColor(MetalLookAndFeel.getBlack());
+ g.drawLine(x + 3, y + 3, x + 3, y + 3);
+ g.drawLine(x + 7, y + 3, x + 7, y + 3);
+ g.drawLine(x + 11, y + 3, x + 11, y + 3);
+
+ g.drawLine(x + 5, y + 5, x + 5, y + 5);
+ g.drawLine(x + 9, y + 5, x + 9, y + 5);
+
+ g.drawLine(x + 3, y + 7, x + 3, y + 7);
+ g.drawLine(x + 7, y + 7, x + 7, y + 7);
+ g.drawLine(x + 11, y + 7, x + 11, y + 7);
+
+ // draw highlights
+ if (focus)
+ g.setColor(MetalLookAndFeel.getPrimaryControl());
+ else
+ g.setColor(MetalLookAndFeel.getWhite());
+ g.drawLine(x + 1, y + 1, x + 13, y + 1);
+ g.drawLine(x + 1, y + 2, x + 1, y + 8);
+ g.drawLine(x + 2, y + 2, x + 2, y + 2);
+ g.drawLine(x + 6, y + 2, x + 6, y + 2);
+ g.drawLine(x + 10, y + 2, x + 10, y + 2);
+
+ g.drawLine(x + 4, y + 4, x + 4, y + 4);
+ g.drawLine(x + 8, y + 4, x + 8, y + 4);
+
+ g.drawLine(x + 2, y + 6, x + 2, y + 6);
+ g.drawLine(x + 6, y + 6, x + 6, y + 6);
+ g.drawLine(x + 10, y + 6, x + 10, y + 6);
+ }
}
}
@@ -1063,13 +1081,19 @@
*/
public void paintIcon(Component c, Graphics g, int x, int y)
{
+ boolean enabled = false;
boolean focus = false;
- if (c != null)
- focus = c.hasFocus();
- // TODO: pick up the colors from the look and feel
+ if (c != null)
+ {
+ enabled = c.isEnabled();
+ focus = c.hasFocus();
+ }
// draw the outline
- g.setColor(Color.black);
+ if (enabled)
+ g.setColor(MetalLookAndFeel.getBlack());
+ else
+ g.setColor(MetalLookAndFeel.getControlDarkShadow());
g.drawLine(x + 1, y, x + 7, y);
g.drawLine(x + 8, y, x + 15, y + 7);
g.drawLine(x + 14, y + 8, x + 8, y + 14);
@@ -1077,8 +1101,11 @@
g.drawLine(x, y + 13, x, y + 1);
// fill the icon
- g.setColor(focus ? new Color(153, 153, 204) : new Color(204, 204, 204));
// medium
- g.fillRect(x + 2, y + 2, 7, 12);
+ if (focus)
+ g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
+ else
+ g.setColor(MetalLookAndFeel.getControl());
+ g.fillRect(x + 2, y + 1, 7, 13);
g.drawLine(x + 9, y + 2, x + 9, y + 12);
g.drawLine(x + 10, y + 3, x + 10, y + 11);
g.drawLine(x + 11, y + 4, x + 11, y + 10);
@@ -1086,36 +1113,46 @@
g.drawLine(x + 13, y + 6, x + 13, y + 8);
g.drawLine(x + 14, y + 7, x + 14, y + 7);
- // draw highlights
- g.setColor(focus ? new Color(204, 204, 255) : new Color(255, 255, 255));
// light
- g.drawLine(x + 1, y + 1, x + 8, y + 1);
- g.drawLine(x + 1, y + 2, x + 1, y + 13);
- g.drawLine(x + 2, y + 2, x + 2, y + 2);
- g.drawLine(x + 2, y + 6, x + 2, y + 6);
- g.drawLine(x + 2, y + 10, x + 2, y + 10);
-
- g.drawLine(x + 4, y + 4, x + 4, y + 4);
- g.drawLine(x + 4, y + 8, x + 4, y + 8);
-
- g.drawLine(x + 6, y + 2, x + 6, y + 2);
- g.drawLine(x + 6, y + 6, x + 6, y + 6);
- g.drawLine(x + 6, y + 10, x + 6, y + 10);
-
- // draw dots
- g.setColor(focus ? new Color(102, 102, 153) : Color.black);
// dark
- g.drawLine(x + 3, y + 3, x + 3, y + 3);
- g.drawLine(x + 3, y + 7, x + 3, y + 7);
- g.drawLine(x + 3, y + 11, x + 3, y + 11);
-
- g.drawLine(x + 5, y + 5, x + 5, y + 5);
- g.drawLine(x + 5, y + 9, x + 5, y + 9);
-
- g.drawLine(x + 7, y + 3, x + 7, y + 3);
- g.drawLine(x + 7, y + 7, x + 7, y + 7);
- g.drawLine(x + 7, y + 11, x + 7, y + 11);
+ // if the slider is enabled, draw dots and highlights
+ if (enabled)
+ {
+ if (focus)
+ g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
+ else
+ g.setColor(MetalLookAndFeel.getBlack());
+ g.drawLine(x + 3, y + 3, x + 3, y + 3);
+ g.drawLine(x + 3, y + 7, x + 3, y + 7);
+ g.drawLine(x + 3, y + 11, x + 3, y + 11);
+
+ g.drawLine(x + 5, y + 5, x + 5, y + 5);
+ g.drawLine(x + 5, y + 9, x + 5, y + 9);
+
+ g.drawLine(x + 7, y + 3, x + 7, y + 3);
+ g.drawLine(x + 7, y + 7, x + 7, y + 7);
+ g.drawLine(x + 7, y + 11, x + 7, y + 11);
+
+ // draw highlights
+ if (focus)
+ g.setColor(MetalLookAndFeel.getPrimaryControl());
+ else
+ g.setColor(MetalLookAndFeel.getWhite());
+ g.drawLine(x + 1, y + 1, x + 8, y + 1);
+ g.drawLine(x + 1, y + 2, x + 1, y + 13);
+ g.drawLine(x + 2, y + 2, x + 2, y + 2);
+ g.drawLine(x + 2, y + 6, x + 2, y + 6);
+ g.drawLine(x + 2, y + 10, x + 2, y + 10);
+
+ g.drawLine(x + 4, y + 4, x + 4, y + 4);
+ g.drawLine(x + 4, y + 8, x + 4, y + 8);
+
+ g.drawLine(x + 6, y + 2, x + 6, y + 2);
+ g.drawLine(x + 6, y + 6, x + 6, y + 6);
+ g.drawLine(x + 6, y + 10, x + 6, y + 10);
+
+ }
}
}
-
+
/**
* A tree control icon. This icon can be in one of two states: expanded and
* collapsed.
Index: javax/swing/plaf/metal/MetalSliderUI.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalSliderUI.java,v
retrieving revision 1.4
diff -u -r1.4 MetalSliderUI.java
--- javax/swing/plaf/metal/MetalSliderUI.java 21 Jul 2005 14:13:36 -0000
1.4
+++ javax/swing/plaf/metal/MetalSliderUI.java 7 Sep 2005 09:57:48 -0000
@@ -42,8 +42,8 @@
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
+import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
-import java.util.HashMap;
import javax.swing.Icon;
import javax.swing.JComponent;
@@ -59,13 +59,56 @@
public class MetalSliderUI
extends BasicSliderUI
{
- // TODO: find a use for this
+ /**
+ * A property change handler that updates the rendered component in response
+ * to specific property change events. This custom handler is used to
+ * intercept the "JSlider.isFilled" property, which is only recognised by
+ * the address@hidden MetalLookAndFeel}.
+ */
+ protected class MetalPropertyListener
+ extends BasicSliderUI.PropertyChangeHandler
+ {
+ /**
+ * Creates a new listener.
+ */
+ protected MetalPropertyListener()
+ {
+ }
+
+ /**
+ * Handles property change events. Events with the name "JSlider.isFilled"
+ * are handled here, and other events are passed to the superclass.
+ *
+ * @param e the property change event.
+ */
+ public void propertyChange(PropertyChangeEvent e)
+ {
+ if (e.getPropertyName().equals(SLIDER_FILL))
+ {
+ Boolean b = (Boolean) e.getNewValue();
+ if (b == null)
+ filledSlider = false;
+ else
+ filledSlider = b.booleanValue();
+ }
+ else
+ super.propertyChange(e);
+ }
+ }
+
+ /** The thumb color (unused, because an icon is used to draw the thumb). */
protected static Color thumbColor;
- // TODO: find a use for this
+ /**
+ * The highlight color used for drawing the track rect when the slider is
+ * enabled.
+ */
protected static Color highlightColor;
- // TODO: find a use for this
+ /**
+ * The shadow color used for drawing the track rect when the slider is
+ * enabled.
+ */
protected static Color darkShadowColor;
/** The track width. */
@@ -85,17 +128,14 @@
/** The gap between the track and the tick marks. */
protected final int TICK_BUFFER = 4;
+ /** A key to look up the filledSlider setting in the address@hidden
UIManager}. */
+ protected final String SLIDER_FILL = "JSlider.isFilled";
+
/**
* 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 address@hidden
UIManager}. */
- protected final String SLIDER_FILL = "JSlider.isFilled";
-
- /** The UI instances for MetalSliderUIs */
- private static HashMap instances;
/**
* Constructs a new instance.
@@ -104,33 +144,27 @@
{
super(null);
filledSlider = UIManager.getBoolean(SLIDER_FILL);
+ darkShadowColor = MetalLookAndFeel.getControlDarkShadow();
+ highlightColor = MetalLookAndFeel.getControlHighlight();
}
/**
* Returns an instance of MetalSliderUI.
*
- * @param component the component for which we return an UI instance
+ * @param component the component (ignored).
*
* @return an instance of MetalSliderUI
*/
public static ComponentUI createUI(JComponent component)
{
- if (instances == null)
- instances = new HashMap();
-
- Object o = instances.get(component);
- MetalSliderUI instance;
- if (o == null)
- {
- instance = new MetalSliderUI();
- instances.put(component, instance);
- }
- else
- instance = (MetalSliderUI) o;
-
- return instance;
+ return new MetalSliderUI();
}
+ /**
+ * Installs the default for this UI delegate in the supplied component.
+ *
+ * @param c the component.
+ */
public void installUI(JComponent c)
{
super.installUI(c);
@@ -140,6 +174,18 @@
}
/**
+ * Creates a property change listener for the slider.
+ *
+ * @param slider the slider.
+ *
+ * @return A new instance of address@hidden MetalPropertyListener}.
+ */
+ protected PropertyChangeListener createPropertyChangeListener(JSlider slider)
+ {
+ return new MetalPropertyListener();
+ }
+
+ /**
* Paints the thumb icon for the slider.
*
* @param g the graphics device.
@@ -153,46 +199,79 @@
}
/**
- * 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)
{
+ Color shadowColor = MetalLookAndFeel.getControlShadow();
if (slider.getOrientation() == JSlider.HORIZONTAL)
- {
- if (filledSlider)
{
- // TODO: fill the track
+ int trackX = trackRect.x;
+ int trackY = trackRect.y + (trackRect.height - getTrackWidth()) / 2;
+ int trackW = trackRect.width - 1;
+ int trackH = getTrackWidth();
+
+ // draw border
+ if (slider.isEnabled())
+ BasicGraphicsUtils.drawEtchedRect(g, trackX, trackY, trackW, trackH,
+ darkShadowColor, shadowColor, darkShadowColor, highlightColor);
+ else
+ {
+ g.setColor(MetalLookAndFeel.getControlShadow());
+ g.drawRect(trackX, trackY, trackW - 2, trackH - 2);
+ }
+
+ // fill track (if required)
+ if (filledSlider)
+ {
+ int xPos = xPositionForValue(slider.getValue());
+ int x = (slider.getInverted() ? xPos : trackRect.x);
+ int w = (slider.getInverted() ? trackX + trackW - xPos
+ : xPos - trackRect.x);
+ g.setColor(MetalLookAndFeel.getControlShadow());
+ g.fillRect(x + 1, trackY + 1, w - 3, getTrackWidth() - 3);
+ if (slider.isEnabled())
+ {
+ g.setColor(MetalLookAndFeel.getControl());
+ g.drawLine(x + 1, trackY + 1, x + w - 3, trackY + 1);
+ g.drawLine(x + 1, trackY + 1, x + 1,
+ trackY + getTrackWidth() - 3);
+ }
+ }
}
- 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
+ int trackX = trackRect.x + (trackRect.width - getTrackWidth()) / 2;
+ int trackY = trackRect.y;
+ int trackW = getTrackWidth();
+ int trackH = trackRect.height - 1;
+ if (slider.isEnabled())
+ BasicGraphicsUtils.drawEtchedRect(g, trackX, trackY, trackW, trackH,
+ darkShadowColor, shadowColor, darkShadowColor, highlightColor);
+ else
+ {
+ g.setColor(MetalLookAndFeel.getControlShadow());
+ g.drawRect(trackX, trackY, trackW - 2, trackH - 2);
+ }
+
+ if (filledSlider)
+ {
+ int yPos = yPositionForValue(slider.getValue());
+ int y = (slider.getInverted() ? trackY : yPos);
+ int h = (slider.getInverted() ? yPos - trackY
+ : trackY + trackH - yPos);
+ g.setColor(MetalLookAndFeel.getControlShadow());
+ g.fillRect(trackX + 1, y + 1, getTrackWidth() - 3, h - 3);
+ if (slider.isEnabled())
+ {
+ g.setColor(MetalLookAndFeel.getControl());
+ g.drawLine(trackX + 1, y + 1, trackX + trackW - 3, y + 1);
+ g.drawLine(trackX + 1, y + 1, trackX + 1, y + h - 3);
+ }
+ }
}
- BasicGraphicsUtils.drawEtchedRect(g, trackRect.x + (trackRect.width
- - getTrackWidth()) / 2, trackRect.y, getTrackWidth(),
- trackRect.height - 1, Color.darkGray, Color.gray, Color.darkGray,
- Color.white);
- }
}
/**
@@ -262,12 +341,13 @@
*/
protected int getThumbOverhang()
{
- // TODO: figure out what this is used for
+ // FIXME: for what might this method be used?
return 0;
}
protected void scrollDueToClickInTrack(int dir)
{
+ // FIXME: for what might this method be overridden?
super.scrollDueToClickInTrack(dir);
}
@@ -283,8 +363,10 @@
{
// 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));
+ if (slider.isEnabled())
+ g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
+ else
+ g.setColor(MetalLookAndFeel.getControlDisabled());
g.drawLine(x, TICK_BUFFER, x, TICK_BUFFER + tickLength / 2);
}
@@ -300,8 +382,10 @@
{
// 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));
+ if (slider.isEnabled())
+ g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
+ else
+ g.setColor(MetalLookAndFeel.getControlDisabled());
g.drawLine(x, TICK_BUFFER, x, TICK_BUFFER + tickLength);
}
@@ -317,8 +401,10 @@
{
// 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));
+ if (slider.isEnabled())
+ g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
+ else
+ g.setColor(MetalLookAndFeel.getControlDisabled());
g.drawLine(TICK_BUFFER - 1, y, TICK_BUFFER - 1 + tickLength / 2, y);
}
@@ -334,8 +420,10 @@
{
// 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));
+ if (slider.isEnabled())
+ g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
+ else
+ g.setColor(MetalLookAndFeel.getControlDisabled());
g.drawLine(TICK_BUFFER - 1, y, TICK_BUFFER - 1 + tickLength, y);
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [cp-patches] FYI: MetalSliderUI and MetalIconFactory changes,
David Gilbert <=