6587742: filling half of a JSlider's track is no longer optional
authorrupashka
Wed, 10 Sep 2008 19:16:14 +0400
changeset 1317 0ba1e3d36c19
parent 1316 20fc6d4d57ae
child 1318 47179339f3eb
child 1828 26bee4a3a499
6587742: filling half of a JSlider's track is no longer optional Summary: now OceanTheme uses the JSlider.isFilled property like other themes Reviewed-by: alexp
jdk/src/share/classes/javax/swing/plaf/metal/MetalSliderUI.java
jdk/test/javax/swing/JSlider/6587742/bug6587742.html
jdk/test/javax/swing/JSlider/6587742/bug6587742.java
--- a/jdk/src/share/classes/javax/swing/plaf/metal/MetalSliderUI.java	Mon Sep 08 17:35:07 2008 +0900
+++ b/jdk/src/share/classes/javax/swing/plaf/metal/MetalSliderUI.java	Wed Sep 10 19:16:14 2008 +0400
@@ -27,23 +27,13 @@
 
 import javax.swing.plaf.basic.BasicSliderUI;
 
-import java.awt.Component;
-import java.awt.Container;
 import java.awt.Graphics;
 import java.awt.Dimension;
 import java.awt.Rectangle;
-import java.awt.Point;
-import java.awt.Insets;
 import java.awt.Color;
-import java.io.Serializable;
-import java.awt.IllegalComponentStateException;
-import java.awt.Polygon;
 import java.beans.*;
 
-import javax.swing.border.AbstractBorder;
-
 import javax.swing.*;
-import javax.swing.event.*;
 import javax.swing.plaf.*;
 
 /**
@@ -131,10 +121,7 @@
 
         scrollListener.setScrollByBlock( false );
 
-        Object sliderFillProp = c.getClientProperty( SLIDER_FILL );
-        if ( sliderFillProp != null ) {
-            filledSlider = ((Boolean)sliderFillProp).booleanValue();
-        }
+        prepareFilledSliderField();
     }
 
     protected PropertyChangeListener createPropertyChangeListener( JSlider slider ) {
@@ -145,18 +132,23 @@
         public void propertyChange( PropertyChangeEvent e ) {  // listen for slider fill
             super.propertyChange( e );
 
-            String name = e.getPropertyName();
-            if ( name.equals( SLIDER_FILL ) ) {
-                if ( e.getNewValue() != null ) {
-                    filledSlider = ((Boolean)e.getNewValue()).booleanValue();
-                }
-                else {
-                    filledSlider = false;
-                }
+            if (e.getPropertyName().equals(SLIDER_FILL)) {
+                prepareFilledSliderField();
             }
         }
     }
 
+    private void prepareFilledSliderField() {
+        // Use true for Ocean theme
+        filledSlider = MetalLookAndFeel.usingOcean();
+
+        Object sliderFillProp = slider.getClientProperty(SLIDER_FILL);
+
+        if (sliderFillProp != null) {
+            filledSlider = ((Boolean) sliderFillProp).booleanValue();
+        }
+    }
+
     public void paintThumb(Graphics g)  {
         Rectangle knobBounds = thumbRect;
 
@@ -173,21 +165,10 @@
     }
 
     /**
-     * If <code>chooseFirst</code>is true, <code>c1</code> is returned,
-     * otherwise <code>c2</code>.
-     */
-    private Color chooseColor(boolean chooseFirst, Color c1, Color c2) {
-        if (chooseFirst) {
-            return c2;
-        }
-        return c1;
-    }
-
-    /**
      * Returns a rectangle enclosing the track that will be painted.
      */
     private Rectangle getPaintTrackRect() {
-        int trackLeft = 0, trackRight = 0, trackTop = 0, trackBottom = 0;
+        int trackLeft = 0, trackRight, trackTop = 0, trackBottom;
         if (slider.getOrientation() == JSlider.HORIZONTAL) {
             trackBottom = (trackRect.height - 1) - getThumbOverhang();
             trackTop = trackBottom - (getTrackWidth() - 1);
@@ -223,8 +204,8 @@
 
         int trackLeft = 0;
         int trackTop = 0;
-        int trackRight = 0;
-        int trackBottom = 0;
+        int trackRight;
+        int trackBottom;
 
         // Draw the track
         if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
@@ -266,11 +247,11 @@
 
         // Draw the fill
         if ( filledSlider ) {
-            int middleOfThumb = 0;
-            int fillTop = 0;
-            int fillLeft = 0;
-            int fillBottom = 0;
-            int fillRight = 0;
+            int middleOfThumb;
+            int fillTop;
+            int fillLeft;
+            int fillBottom;
+            int fillRight;
 
             if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
                 middleOfThumb = thumbRect.x + (thumbRect.width / 2);
@@ -335,105 +316,137 @@
         int w = paintRect.width;
         int h = paintRect.height;
 
-        if (!slider.isEnabled()) {
-            g.setColor(MetalLookAndFeel.getControlShadow());
-            g.drawRect(0, 0, w - 1, h - 1);
-        }
-        else if (slider.getOrientation() == JSlider.HORIZONTAL) {
-            int middleOfThumb = thumbRect.x + (thumbRect.width / 2) -
-                                paintRect.x;
-            int fillMinX;
-            int fillMaxX;
+        if (slider.getOrientation() == JSlider.HORIZONTAL) {
+            int middleOfThumb = thumbRect.x + thumbRect.width / 2 - paintRect.x;
+
+            if (slider.isEnabled()) {
+                int fillMinX;
+                int fillMaxX;
+
+                if (middleOfThumb > 0) {
+                    g.setColor(drawInverted ? MetalLookAndFeel.getControlDarkShadow() :
+                            MetalLookAndFeel.getPrimaryControlDarkShadow());
+
+                    g.drawRect(0, 0, middleOfThumb - 1, h - 1);
+                }
+
+                if (middleOfThumb < w) {
+                    g.setColor(drawInverted ? MetalLookAndFeel.getPrimaryControlDarkShadow() :
+                            MetalLookAndFeel.getControlDarkShadow());
+
+                    g.drawRect(middleOfThumb, 0, w - middleOfThumb - 1, h - 1);
+                }
 
-            if (middleOfThumb > 0) {
-                g.setColor(chooseColor(drawInverted,
-                           MetalLookAndFeel.getPrimaryControlDarkShadow(),
-                           MetalLookAndFeel.getControlDarkShadow()));
-               g.drawRect(0, 0, middleOfThumb - 1, h - 1);
-            }
-            if (middleOfThumb < w) {
-                g.setColor(chooseColor(drawInverted,
-                           MetalLookAndFeel.getControlDarkShadow(),
-                           MetalLookAndFeel.getPrimaryControlDarkShadow()));
-                g.drawRect(middleOfThumb, 0, w - middleOfThumb - 1, h - 1);
-            }
-            g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
-            if (drawInverted) {
-                fillMinX = middleOfThumb;
-                fillMaxX = w - 2;
-                g.drawLine(1, 1, middleOfThumb, 1);
-            }
-            else {
-                fillMinX = 1;
-                fillMaxX = middleOfThumb;
-                g.drawLine(middleOfThumb, 1, w - 1, 1);
-            }
-            if (h == 6) {
-                g.setColor(MetalLookAndFeel.getWhite());
-                g.drawLine(fillMinX, 1, fillMaxX, 1);
-                g.setColor(sliderAltTrackColor);
-                g.drawLine(fillMinX, 2, fillMaxX, 2);
+                if (filledSlider) {
+                    g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
+                    if (drawInverted) {
+                        fillMinX = middleOfThumb;
+                        fillMaxX = w - 2;
+                        g.drawLine(1, 1, middleOfThumb, 1);
+                    } else {
+                        fillMinX = 1;
+                        fillMaxX = middleOfThumb;
+                        g.drawLine(middleOfThumb, 1, w - 1, 1);
+                    }
+                    if (h == 6) {
+                        g.setColor(MetalLookAndFeel.getWhite());
+                        g.drawLine(fillMinX, 1, fillMaxX, 1);
+                        g.setColor(sliderAltTrackColor);
+                        g.drawLine(fillMinX, 2, fillMaxX, 2);
+                        g.setColor(MetalLookAndFeel.getControlShadow());
+                        g.drawLine(fillMinX, 3, fillMaxX, 3);
+                        g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
+                        g.drawLine(fillMinX, 4, fillMaxX, 4);
+                    }
+                }
+            } else {
                 g.setColor(MetalLookAndFeel.getControlShadow());
-                g.drawLine(fillMinX, 3, fillMaxX, 3);
-                g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
-                g.drawLine(fillMinX, 4, fillMaxX, 4);
-            }
-        }
-        else {
-            int middleOfThumb = thumbRect.y + (thumbRect.height / 2) -
-                                paintRect.y;
-            int fillMinY;
-            int fillMaxY;
 
-            if (middleOfThumb > 0) {
-                g.setColor(chooseColor(drawInverted,
-                           MetalLookAndFeel.getControlDarkShadow(),
-                           MetalLookAndFeel.getPrimaryControlDarkShadow()));
-                g.drawRect(0, 0, w - 1, middleOfThumb - 1);
-            }
-            if (middleOfThumb < h) {
-                g.setColor(chooseColor(drawInverted,
-                           MetalLookAndFeel.getPrimaryControlDarkShadow(),
-                           MetalLookAndFeel.getControlDarkShadow()));
-                g.drawRect(0, middleOfThumb, w - 1, h - middleOfThumb - 1);
-            }
-            g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
-            if (drawInverted()) {
-                fillMinY = 1;
-                fillMaxY = middleOfThumb;
-                if (leftToRight) {
-                    g.drawLine(1, middleOfThumb, 1, h - 1);
+                if (middleOfThumb > 0) {
+                    if (!drawInverted && filledSlider) {
+                        g.fillRect(0, 0, middleOfThumb - 1, h - 1);
+                    } else {
+                        g.drawRect(0, 0, middleOfThumb - 1, h - 1);
+                    }
                 }
-                else {
-                    g.drawLine(w - 2, middleOfThumb, w - 2, h - 1);
+
+                if (middleOfThumb < w) {
+                    if (drawInverted && filledSlider) {
+                        g.fillRect(middleOfThumb, 0, w - middleOfThumb - 1, h - 1);
+                    } else {
+                        g.drawRect(middleOfThumb, 0, w - middleOfThumb - 1, h - 1);
+                    }
                 }
             }
-            else {
-                fillMinY = middleOfThumb;
-                fillMaxY = h - 2;
-                if (leftToRight) {
-                    g.drawLine(1, 1, 1, middleOfThumb);
+        } else {
+            int middleOfThumb = thumbRect.y + (thumbRect.height / 2) - paintRect.y;
+
+            if (slider.isEnabled()) {
+                int fillMinY;
+                int fillMaxY;
+
+                if (middleOfThumb > 0) {
+                    g.setColor(drawInverted ? MetalLookAndFeel.getPrimaryControlDarkShadow() :
+                            MetalLookAndFeel.getControlDarkShadow());
+
+                    g.drawRect(0, 0, w - 1, middleOfThumb - 1);
                 }
-                else {
-                    g.drawLine(w - 2, 1, w - 2, middleOfThumb);
+
+                if (middleOfThumb < h) {
+                    g.setColor(drawInverted ? MetalLookAndFeel.getControlDarkShadow() :
+                            MetalLookAndFeel.getPrimaryControlDarkShadow());
+
+                    g.drawRect(0, middleOfThumb, w - 1, h - middleOfThumb - 1);
                 }
-            }
-            if (w == 6) {
-                g.setColor(chooseColor(!leftToRight,
-                           MetalLookAndFeel.getWhite(),
-                           MetalLookAndFeel.getPrimaryControlShadow()));
-                g.drawLine(1, fillMinY, 1, fillMaxY);
-                g.setColor(chooseColor(!leftToRight, sliderAltTrackColor,
-                           MetalLookAndFeel.getControlShadow()));
-                g.drawLine(2, fillMinY, 2, fillMaxY);
-                g.setColor(chooseColor(!leftToRight,
-                           MetalLookAndFeel.getControlShadow(),
-                           sliderAltTrackColor));
-                g.drawLine(3, fillMinY, 3, fillMaxY);
-                g.setColor(chooseColor(!leftToRight,
-                           MetalLookAndFeel.getPrimaryControlShadow(),
-                           MetalLookAndFeel.getWhite()));
-                g.drawLine(4, fillMinY, 4, fillMaxY);
+
+                if (filledSlider) {
+                    g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
+                    if (drawInverted()) {
+                        fillMinY = 1;
+                        fillMaxY = middleOfThumb;
+                        if (leftToRight) {
+                            g.drawLine(1, middleOfThumb, 1, h - 1);
+                        } else {
+                            g.drawLine(w - 2, middleOfThumb, w - 2, h - 1);
+                        }
+                    } else {
+                        fillMinY = middleOfThumb;
+                        fillMaxY = h - 2;
+                        if (leftToRight) {
+                            g.drawLine(1, 1, 1, middleOfThumb);
+                        } else {
+                            g.drawLine(w - 2, 1, w - 2, middleOfThumb);
+                        }
+                    }
+                    if (w == 6) {
+                        g.setColor(leftToRight ? MetalLookAndFeel.getWhite() : MetalLookAndFeel.getPrimaryControlShadow());
+                        g.drawLine(1, fillMinY, 1, fillMaxY);
+                        g.setColor(leftToRight ? sliderAltTrackColor : MetalLookAndFeel.getControlShadow());
+                        g.drawLine(2, fillMinY, 2, fillMaxY);
+                        g.setColor(leftToRight ? MetalLookAndFeel.getControlShadow() : sliderAltTrackColor);
+                        g.drawLine(3, fillMinY, 3, fillMaxY);
+                        g.setColor(leftToRight ? MetalLookAndFeel.getPrimaryControlShadow() : MetalLookAndFeel.getWhite());
+                        g.drawLine(4, fillMinY, 4, fillMaxY);
+                    }
+                }
+            } else {
+                g.setColor(MetalLookAndFeel.getControlShadow());
+
+                if (middleOfThumb > 0) {
+                    if (drawInverted && filledSlider) {
+                        g.fillRect(0, 0, w - 1, middleOfThumb - 1);
+                    } else {
+                        g.drawRect(0, 0, w - 1, middleOfThumb - 1);
+                    }
+                }
+
+                if (middleOfThumb < h) {
+                    if (!drawInverted && filledSlider) {
+                        g.fillRect(0, middleOfThumb, w - 1, h - middleOfThumb - 1);
+                    } else {
+                        g.drawRect(0, middleOfThumb, w - 1, h - middleOfThumb - 1);
+                    }
+                }
             }
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JSlider/6587742/bug6587742.html	Wed Sep 10 19:16:14 2008 +0400
@@ -0,0 +1,13 @@
+<html>
+<body>
+<applet  code="bug6587742.class" width=800 height=600></applet>
+Select every theme and check that all sliders looks good.
+Note that every slider has a tooltip text with information about
+slider configuration.
+There is a small difference in sliders with property "filled = null" (it's
+default behaviour when property JSlider.isFilled is not setted)
+for themes:
+1. OceanTheme - sliders look like filled
+2. DefaultMetalTheme - sliders look like NOT filled 
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JSlider/6587742/bug6587742.java	Wed Sep 10 19:16:14 2008 +0400
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ * @bug 6587742
+ * @summary filling half of a JSlider's track is no longer optional
+ * @author Pavel Porvatov
+ * @run applet/manual=done bug6587742.html
+ */
+
+import javax.swing.*;
+import javax.swing.plaf.metal.DefaultMetalTheme;
+import javax.swing.plaf.metal.MetalLookAndFeel;
+import javax.swing.plaf.metal.MetalTheme;
+import javax.swing.plaf.metal.OceanTheme;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+
+public class bug6587742 extends JApplet {
+    public void init() {
+        TestPanel panel = new TestPanel();
+
+        setContentPane(panel);
+    }
+
+    private class TestPanel extends JPanel {
+        private final JComboBox cbThemes = new JComboBox();
+
+        private TestPanel() {
+            // Fill cbThemes
+            cbThemes.addItem(new OceanTheme());
+            cbThemes.addItem(new DefaultMetalTheme());
+
+            cbThemes.addItemListener(new ItemListener() {
+                public void itemStateChanged(ItemEvent e) {
+                    MetalTheme theme = (MetalTheme) cbThemes.getSelectedItem();
+
+                    if (theme != null) {
+                        MetalLookAndFeel.setCurrentTheme(theme);
+
+                        // re-install the Metal Look and Feel
+                        try {
+                            UIManager.setLookAndFeel(new MetalLookAndFeel());
+                        } catch (UnsupportedLookAndFeelException e1) {
+                            JOptionPane.showMessageDialog(TestPanel.this, "Can't change theme: " + e1.getMessage(),
+                                    "Error", JOptionPane.ERROR_MESSAGE);
+
+                            return;
+                        }
+
+                        SwingUtilities.updateComponentTreeUI(bug6587742.this);
+                    }
+                }
+            });
+
+            JPanel pnVertical = new JPanel();
+
+            pnVertical.setLayout(new BoxLayout(pnVertical, BoxLayout.Y_AXIS));
+
+            for (int i = 0; i < 12; i++) {
+                int filled = i >> 2;
+
+                pnVertical.add(createSlider(false, filled > 1 ? null : Boolean.valueOf(filled == 1), (i & 2) == 0,
+                        (i & 1) != 0));
+            }
+
+            JPanel pnHorizontal = new JPanel();
+
+            pnHorizontal.setLayout(new BoxLayout(pnHorizontal, BoxLayout.X_AXIS));
+
+            for (int i = 0; i < 12; i++) {
+                int filled = i >> 2;
+
+                pnHorizontal.add(createSlider(true, filled > 1 ? null : Boolean.valueOf(filled == 1), (i & 2) == 0,
+                        (i & 1) != 0));
+            }
+
+            JTabbedPane tpSliders = new JTabbedPane();
+
+            tpSliders.add("Vertical sliders", pnVertical);
+            tpSliders.add("Horizontal sliders", pnHorizontal);
+
+            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
+
+            add(new JLabel("Select theme:"));
+            add(cbThemes);
+            add(tpSliders);
+        }
+    }
+
+    private static JSlider createSlider(boolean vertical, Boolean filled, boolean enabled, boolean inverted) {
+        JSlider result = new JSlider(vertical ? SwingConstants.VERTICAL : SwingConstants.HORIZONTAL, 0, 100, 50);
+
+        result.setMajorTickSpacing(20);
+        result.setMinorTickSpacing(5);
+        result.setPaintTicks(true);
+        result.setPaintLabels(true);
+        result.setEnabled(enabled);
+
+        if (filled != null) {
+            result.putClientProperty("JSlider.isFilled", filled);
+        }
+
+        result.setInverted(inverted);
+        result.setToolTipText("<html>vertical = " + vertical + "<br>enabled = " + enabled + "<br>filled = " + filled +
+                "<br>inverted = " + inverted + "</html>");
+
+        return result;
+    }
+}