classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] Proposed patch: BasicSliderUI/MetalSliderUI


From: David Gilbert
Subject: [cp-patches] Proposed patch: BasicSliderUI/MetalSliderUI
Date: Thu, 21 Jul 2005 13:31:01 +0000
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050426)

I've done some work on the BasicSliderUI class and an implementation of
MetalSliderUI (see attached screenshot).  This is now working reasonably
well, although it is not 100% complete.  Due to time
constraints/holidays, I may have to leave this for a few weeks - my
question is whether the attached patch should be committed to CVS.  I
think it is an improvement to the existing code, so it should go in,
even though it still requires work.  Any comments?

A couple of hints about the changes:

(1)  I deleted thumbHeight, thumbWidth, and tickHeight because they were
all initialised from values in the UIDefaults that don't exist in Sun's
implementation.  Instead, constants are now used - other lafs can change
these by overriding methods like getThumbSize().

(2)  I implemented the MetalSliderUI on top of Sun's BasicSliderUI, and
from testing it seems that the incoming Graphics in the tick painting
methods already has a translation applied to it.  I added the same
translation to Classpath's BasicSliderUI to make my MetalSliderUI code
work the same on both platforms.

2005-07-20  David Gilbert  <address@hidden>

        * 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.

Regards,

Dave Gilbert

PNG image

Index: examples/gnu/classpath/examples/swing/Demo.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/examples/gnu/classpath/examples/swing/Demo.java,v
retrieving revision 1.15
diff -u -r1.15 Demo.java
--- examples/gnu/classpath/examples/swing/Demo.java     12 Jul 2005 20:57:23 
-0000      1.15
+++ examples/gnu/classpath/examples/swing/Demo.java     21 Jul 2005 11:47:26 
-0000
@@ -658,6 +658,8 @@
     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);
Index: javax/swing/plaf/basic/BasicSliderUI.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicSliderUI.java,v
retrieving revision 1.16
diff -u -r1.16 BasicSliderUI.java
--- javax/swing/plaf/basic/BasicSliderUI.java   18 Jul 2005 15:00:00 -0000      
1.16
+++ javax/swing/plaf/basic/BasicSliderUI.java   21 Jul 2005 11:47:39 -0000
@@ -470,15 +470,6 @@
     }
   }
 
-  /** 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 @@
     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 @@
     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 @@
     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 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 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 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 address@hidden JSlider} for 
this
-   * look and feel. If it returns null, then it is up to the Layout Manager
-   * to give the address@hidden JComponent} a size.
+   * look and feel.
    *
    * @param c The address@hidden JComponent} to find a maximum size for.
    *
@@ -1015,10 +1026,40 @@
    */
   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 @@
   {
     insetCache = slider.getInsets();
     focusRect = SwingUtilities.calculateInnerArea(slider, focusRect);
-
     if (focusRect.width < 0)
       focusRect.width = 0;
     if (focusRect.height < 0)
@@ -1058,30 +1098,13 @@
    */
   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 @@
   {
     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 @@
     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 @@
   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 @@
    */
   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 @@
     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 @@
    */
   protected int getTickLength()
   {
-    return tickHeight;
+    return 8;
   }
 
   /**
@@ -1536,9 +1570,6 @@
     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 @@
       {
        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 @@
   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 @@
   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 @@
   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 @@
   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 @@
   {
     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 @@
       xPos += trackRect.x;
     else
       {
-       xPos = trackRect.width - xPos;
+       xPos = len - xPos;
        xPos += trackRect.x;
       }
     return xPos;
@@ -2100,14 +2134,13 @@
   {
     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 @@
 
     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 @@
 
     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);
 
Index: javax/swing/plaf/metal/MetalLookAndFeel.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalLookAndFeel.java,v
retrieving revision 1.29
diff -u -r1.29 MetalLookAndFeel.java
--- javax/swing/plaf/metal/MetalLookAndFeel.java        13 Jul 2005 21:15:42 
-0000      1.29
+++ javax/swing/plaf/metal/MetalLookAndFeel.java        21 Jul 2005 11:47:40 
-0000
@@ -47,6 +47,7 @@
 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;
 
 /**
@@ -802,6 +803,14 @@
       "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(),
Index: javax/swing/plaf/metal/MetalSliderUI.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalSliderUI.java,v
retrieving revision 1.3
diff -u -r1.3 MetalSliderUI.java
--- javax/swing/plaf/metal/MetalSliderUI.java   2 Jul 2005 20:32:51 -0000       
1.3
+++ javax/swing/plaf/metal/MetalSliderUI.java   21 Jul 2005 11:47:40 -0000
@@ -38,25 +38,72 @@
 
 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 address@hidden 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 address@hidden 
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 @@
     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 address@hidden 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 address@hidden 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);
+  }
+  
 }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]