jdk/src/share/classes/javax/swing/plaf/metal/MetalSliderUI.java
changeset 1317 0ba1e3d36c19
parent 1304 f0e7af74a2bc
child 1639 a97859015238
equal deleted inserted replaced
1316:20fc6d4d57ae 1317:0ba1e3d36c19
    25 
    25 
    26 package javax.swing.plaf.metal;
    26 package javax.swing.plaf.metal;
    27 
    27 
    28 import javax.swing.plaf.basic.BasicSliderUI;
    28 import javax.swing.plaf.basic.BasicSliderUI;
    29 
    29 
    30 import java.awt.Component;
       
    31 import java.awt.Container;
       
    32 import java.awt.Graphics;
    30 import java.awt.Graphics;
    33 import java.awt.Dimension;
    31 import java.awt.Dimension;
    34 import java.awt.Rectangle;
    32 import java.awt.Rectangle;
    35 import java.awt.Point;
       
    36 import java.awt.Insets;
       
    37 import java.awt.Color;
    33 import java.awt.Color;
    38 import java.io.Serializable;
       
    39 import java.awt.IllegalComponentStateException;
       
    40 import java.awt.Polygon;
       
    41 import java.beans.*;
    34 import java.beans.*;
    42 
    35 
    43 import javax.swing.border.AbstractBorder;
       
    44 
       
    45 import javax.swing.*;
    36 import javax.swing.*;
    46 import javax.swing.event.*;
       
    47 import javax.swing.plaf.*;
    37 import javax.swing.plaf.*;
    48 
    38 
    49 /**
    39 /**
    50  * A Java L&F implementation of SliderUI.
    40  * A Java L&F implementation of SliderUI.
    51  * <p>
    41  * <p>
   129         highlightColor = UIManager.getColor("Slider.highlight");
   119         highlightColor = UIManager.getColor("Slider.highlight");
   130         darkShadowColor = UIManager.getColor("Slider.darkShadow");
   120         darkShadowColor = UIManager.getColor("Slider.darkShadow");
   131 
   121 
   132         scrollListener.setScrollByBlock( false );
   122         scrollListener.setScrollByBlock( false );
   133 
   123 
   134         Object sliderFillProp = c.getClientProperty( SLIDER_FILL );
   124         prepareFilledSliderField();
   135         if ( sliderFillProp != null ) {
       
   136             filledSlider = ((Boolean)sliderFillProp).booleanValue();
       
   137         }
       
   138     }
   125     }
   139 
   126 
   140     protected PropertyChangeListener createPropertyChangeListener( JSlider slider ) {
   127     protected PropertyChangeListener createPropertyChangeListener( JSlider slider ) {
   141         return new MetalPropertyListener();
   128         return new MetalPropertyListener();
   142     }
   129     }
   143 
   130 
   144     protected class MetalPropertyListener extends BasicSliderUI.PropertyChangeHandler {
   131     protected class MetalPropertyListener extends BasicSliderUI.PropertyChangeHandler {
   145         public void propertyChange( PropertyChangeEvent e ) {  // listen for slider fill
   132         public void propertyChange( PropertyChangeEvent e ) {  // listen for slider fill
   146             super.propertyChange( e );
   133             super.propertyChange( e );
   147 
   134 
   148             String name = e.getPropertyName();
   135             if (e.getPropertyName().equals(SLIDER_FILL)) {
   149             if ( name.equals( SLIDER_FILL ) ) {
   136                 prepareFilledSliderField();
   150                 if ( e.getNewValue() != null ) {
   137             }
   151                     filledSlider = ((Boolean)e.getNewValue()).booleanValue();
   138         }
   152                 }
   139     }
   153                 else {
   140 
   154                     filledSlider = false;
   141     private void prepareFilledSliderField() {
   155                 }
   142         // Use true for Ocean theme
   156             }
   143         filledSlider = MetalLookAndFeel.usingOcean();
       
   144 
       
   145         Object sliderFillProp = slider.getClientProperty(SLIDER_FILL);
       
   146 
       
   147         if (sliderFillProp != null) {
       
   148             filledSlider = ((Boolean) sliderFillProp).booleanValue();
   157         }
   149         }
   158     }
   150     }
   159 
   151 
   160     public void paintThumb(Graphics g)  {
   152     public void paintThumb(Graphics g)  {
   161         Rectangle knobBounds = thumbRect;
   153         Rectangle knobBounds = thumbRect;
   168         else {
   160         else {
   169             getVertThumbIcon().paintIcon( slider, g, 0, 0 );
   161             getVertThumbIcon().paintIcon( slider, g, 0, 0 );
   170         }
   162         }
   171 
   163 
   172         g.translate( -knobBounds.x, -knobBounds.y );
   164         g.translate( -knobBounds.x, -knobBounds.y );
   173     }
       
   174 
       
   175     /**
       
   176      * If <code>chooseFirst</code>is true, <code>c1</code> is returned,
       
   177      * otherwise <code>c2</code>.
       
   178      */
       
   179     private Color chooseColor(boolean chooseFirst, Color c1, Color c2) {
       
   180         if (chooseFirst) {
       
   181             return c2;
       
   182         }
       
   183         return c1;
       
   184     }
   165     }
   185 
   166 
   186     /**
   167     /**
   187      * Returns a rectangle enclosing the track that will be painted.
   168      * Returns a rectangle enclosing the track that will be painted.
   188      */
   169      */
   189     private Rectangle getPaintTrackRect() {
   170     private Rectangle getPaintTrackRect() {
   190         int trackLeft = 0, trackRight = 0, trackTop = 0, trackBottom = 0;
   171         int trackLeft = 0, trackRight, trackTop = 0, trackBottom;
   191         if (slider.getOrientation() == JSlider.HORIZONTAL) {
   172         if (slider.getOrientation() == JSlider.HORIZONTAL) {
   192             trackBottom = (trackRect.height - 1) - getThumbOverhang();
   173             trackBottom = (trackRect.height - 1) - getThumbOverhang();
   193             trackTop = trackBottom - (getTrackWidth() - 1);
   174             trackTop = trackBottom - (getTrackWidth() - 1);
   194             trackRight = trackRect.width - 1;
   175             trackRight = trackRect.width - 1;
   195         }
   176         }
   221 
   202 
   222         g.translate( trackRect.x, trackRect.y );
   203         g.translate( trackRect.x, trackRect.y );
   223 
   204 
   224         int trackLeft = 0;
   205         int trackLeft = 0;
   225         int trackTop = 0;
   206         int trackTop = 0;
   226         int trackRight = 0;
   207         int trackRight;
   227         int trackBottom = 0;
   208         int trackBottom;
   228 
   209 
   229         // Draw the track
   210         // Draw the track
   230         if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
   211         if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
   231             trackBottom = (trackRect.height - 1) - getThumbOverhang();
   212             trackBottom = (trackRect.height - 1) - getThumbOverhang();
   232             trackTop = trackBottom - (getTrackWidth() - 1);
   213             trackTop = trackBottom - (getTrackWidth() - 1);
   264                         (trackRight - trackLeft) - 1, (trackBottom - trackTop) - 1 );
   245                         (trackRight - trackLeft) - 1, (trackBottom - trackTop) - 1 );
   265         }
   246         }
   266 
   247 
   267         // Draw the fill
   248         // Draw the fill
   268         if ( filledSlider ) {
   249         if ( filledSlider ) {
   269             int middleOfThumb = 0;
   250             int middleOfThumb;
   270             int fillTop = 0;
   251             int fillTop;
   271             int fillLeft = 0;
   252             int fillLeft;
   272             int fillBottom = 0;
   253             int fillBottom;
   273             int fillRight = 0;
   254             int fillRight;
   274 
   255 
   275             if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
   256             if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
   276                 middleOfThumb = thumbRect.x + (thumbRect.width / 2);
   257                 middleOfThumb = thumbRect.x + (thumbRect.width / 2);
   277                 middleOfThumb -= trackRect.x; // To compensate for the g.translate()
   258                 middleOfThumb -= trackRect.x; // To compensate for the g.translate()
   278                 fillTop = !slider.isEnabled() ? trackTop : trackTop + 1;
   259                 fillTop = !slider.isEnabled() ? trackTop : trackTop + 1;
   333 
   314 
   334         // Width and height of the painting rectangle.
   315         // Width and height of the painting rectangle.
   335         int w = paintRect.width;
   316         int w = paintRect.width;
   336         int h = paintRect.height;
   317         int h = paintRect.height;
   337 
   318 
   338         if (!slider.isEnabled()) {
   319         if (slider.getOrientation() == JSlider.HORIZONTAL) {
   339             g.setColor(MetalLookAndFeel.getControlShadow());
   320             int middleOfThumb = thumbRect.x + thumbRect.width / 2 - paintRect.x;
   340             g.drawRect(0, 0, w - 1, h - 1);
   321 
   341         }
   322             if (slider.isEnabled()) {
   342         else if (slider.getOrientation() == JSlider.HORIZONTAL) {
   323                 int fillMinX;
   343             int middleOfThumb = thumbRect.x + (thumbRect.width / 2) -
   324                 int fillMaxX;
   344                                 paintRect.x;
   325 
   345             int fillMinX;
   326                 if (middleOfThumb > 0) {
   346             int fillMaxX;
   327                     g.setColor(drawInverted ? MetalLookAndFeel.getControlDarkShadow() :
   347 
   328                             MetalLookAndFeel.getPrimaryControlDarkShadow());
   348             if (middleOfThumb > 0) {
   329 
   349                 g.setColor(chooseColor(drawInverted,
   330                     g.drawRect(0, 0, middleOfThumb - 1, h - 1);
   350                            MetalLookAndFeel.getPrimaryControlDarkShadow(),
   331                 }
   351                            MetalLookAndFeel.getControlDarkShadow()));
   332 
   352                g.drawRect(0, 0, middleOfThumb - 1, h - 1);
   333                 if (middleOfThumb < w) {
   353             }
   334                     g.setColor(drawInverted ? MetalLookAndFeel.getPrimaryControlDarkShadow() :
   354             if (middleOfThumb < w) {
   335                             MetalLookAndFeel.getControlDarkShadow());
   355                 g.setColor(chooseColor(drawInverted,
   336 
   356                            MetalLookAndFeel.getControlDarkShadow(),
   337                     g.drawRect(middleOfThumb, 0, w - middleOfThumb - 1, h - 1);
   357                            MetalLookAndFeel.getPrimaryControlDarkShadow()));
   338                 }
   358                 g.drawRect(middleOfThumb, 0, w - middleOfThumb - 1, h - 1);
   339 
   359             }
   340                 if (filledSlider) {
   360             g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
   341                     g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
   361             if (drawInverted) {
   342                     if (drawInverted) {
   362                 fillMinX = middleOfThumb;
   343                         fillMinX = middleOfThumb;
   363                 fillMaxX = w - 2;
   344                         fillMaxX = w - 2;
   364                 g.drawLine(1, 1, middleOfThumb, 1);
   345                         g.drawLine(1, 1, middleOfThumb, 1);
   365             }
   346                     } else {
   366             else {
   347                         fillMinX = 1;
   367                 fillMinX = 1;
   348                         fillMaxX = middleOfThumb;
   368                 fillMaxX = middleOfThumb;
   349                         g.drawLine(middleOfThumb, 1, w - 1, 1);
   369                 g.drawLine(middleOfThumb, 1, w - 1, 1);
   350                     }
   370             }
   351                     if (h == 6) {
   371             if (h == 6) {
   352                         g.setColor(MetalLookAndFeel.getWhite());
   372                 g.setColor(MetalLookAndFeel.getWhite());
   353                         g.drawLine(fillMinX, 1, fillMaxX, 1);
   373                 g.drawLine(fillMinX, 1, fillMaxX, 1);
   354                         g.setColor(sliderAltTrackColor);
   374                 g.setColor(sliderAltTrackColor);
   355                         g.drawLine(fillMinX, 2, fillMaxX, 2);
   375                 g.drawLine(fillMinX, 2, fillMaxX, 2);
   356                         g.setColor(MetalLookAndFeel.getControlShadow());
       
   357                         g.drawLine(fillMinX, 3, fillMaxX, 3);
       
   358                         g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
       
   359                         g.drawLine(fillMinX, 4, fillMaxX, 4);
       
   360                     }
       
   361                 }
       
   362             } else {
   376                 g.setColor(MetalLookAndFeel.getControlShadow());
   363                 g.setColor(MetalLookAndFeel.getControlShadow());
   377                 g.drawLine(fillMinX, 3, fillMaxX, 3);
   364 
   378                 g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
   365                 if (middleOfThumb > 0) {
   379                 g.drawLine(fillMinX, 4, fillMaxX, 4);
   366                     if (!drawInverted && filledSlider) {
   380             }
   367                         g.fillRect(0, 0, middleOfThumb - 1, h - 1);
   381         }
   368                     } else {
   382         else {
   369                         g.drawRect(0, 0, middleOfThumb - 1, h - 1);
   383             int middleOfThumb = thumbRect.y + (thumbRect.height / 2) -
   370                     }
   384                                 paintRect.y;
   371                 }
   385             int fillMinY;
   372 
   386             int fillMaxY;
   373                 if (middleOfThumb < w) {
   387 
   374                     if (drawInverted && filledSlider) {
   388             if (middleOfThumb > 0) {
   375                         g.fillRect(middleOfThumb, 0, w - middleOfThumb - 1, h - 1);
   389                 g.setColor(chooseColor(drawInverted,
   376                     } else {
   390                            MetalLookAndFeel.getControlDarkShadow(),
   377                         g.drawRect(middleOfThumb, 0, w - middleOfThumb - 1, h - 1);
   391                            MetalLookAndFeel.getPrimaryControlDarkShadow()));
   378                     }
   392                 g.drawRect(0, 0, w - 1, middleOfThumb - 1);
   379                 }
   393             }
   380             }
   394             if (middleOfThumb < h) {
   381         } else {
   395                 g.setColor(chooseColor(drawInverted,
   382             int middleOfThumb = thumbRect.y + (thumbRect.height / 2) - paintRect.y;
   396                            MetalLookAndFeel.getPrimaryControlDarkShadow(),
   383 
   397                            MetalLookAndFeel.getControlDarkShadow()));
   384             if (slider.isEnabled()) {
   398                 g.drawRect(0, middleOfThumb, w - 1, h - middleOfThumb - 1);
   385                 int fillMinY;
   399             }
   386                 int fillMaxY;
   400             g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
   387 
   401             if (drawInverted()) {
   388                 if (middleOfThumb > 0) {
   402                 fillMinY = 1;
   389                     g.setColor(drawInverted ? MetalLookAndFeel.getPrimaryControlDarkShadow() :
   403                 fillMaxY = middleOfThumb;
   390                             MetalLookAndFeel.getControlDarkShadow());
   404                 if (leftToRight) {
   391 
   405                     g.drawLine(1, middleOfThumb, 1, h - 1);
   392                     g.drawRect(0, 0, w - 1, middleOfThumb - 1);
   406                 }
   393                 }
   407                 else {
   394 
   408                     g.drawLine(w - 2, middleOfThumb, w - 2, h - 1);
   395                 if (middleOfThumb < h) {
   409                 }
   396                     g.setColor(drawInverted ? MetalLookAndFeel.getControlDarkShadow() :
   410             }
   397                             MetalLookAndFeel.getPrimaryControlDarkShadow());
   411             else {
   398 
   412                 fillMinY = middleOfThumb;
   399                     g.drawRect(0, middleOfThumb, w - 1, h - middleOfThumb - 1);
   413                 fillMaxY = h - 2;
   400                 }
   414                 if (leftToRight) {
   401 
   415                     g.drawLine(1, 1, 1, middleOfThumb);
   402                 if (filledSlider) {
   416                 }
   403                     g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
   417                 else {
   404                     if (drawInverted()) {
   418                     g.drawLine(w - 2, 1, w - 2, middleOfThumb);
   405                         fillMinY = 1;
   419                 }
   406                         fillMaxY = middleOfThumb;
   420             }
   407                         if (leftToRight) {
   421             if (w == 6) {
   408                             g.drawLine(1, middleOfThumb, 1, h - 1);
   422                 g.setColor(chooseColor(!leftToRight,
   409                         } else {
   423                            MetalLookAndFeel.getWhite(),
   410                             g.drawLine(w - 2, middleOfThumb, w - 2, h - 1);
   424                            MetalLookAndFeel.getPrimaryControlShadow()));
   411                         }
   425                 g.drawLine(1, fillMinY, 1, fillMaxY);
   412                     } else {
   426                 g.setColor(chooseColor(!leftToRight, sliderAltTrackColor,
   413                         fillMinY = middleOfThumb;
   427                            MetalLookAndFeel.getControlShadow()));
   414                         fillMaxY = h - 2;
   428                 g.drawLine(2, fillMinY, 2, fillMaxY);
   415                         if (leftToRight) {
   429                 g.setColor(chooseColor(!leftToRight,
   416                             g.drawLine(1, 1, 1, middleOfThumb);
   430                            MetalLookAndFeel.getControlShadow(),
   417                         } else {
   431                            sliderAltTrackColor));
   418                             g.drawLine(w - 2, 1, w - 2, middleOfThumb);
   432                 g.drawLine(3, fillMinY, 3, fillMaxY);
   419                         }
   433                 g.setColor(chooseColor(!leftToRight,
   420                     }
   434                            MetalLookAndFeel.getPrimaryControlShadow(),
   421                     if (w == 6) {
   435                            MetalLookAndFeel.getWhite()));
   422                         g.setColor(leftToRight ? MetalLookAndFeel.getWhite() : MetalLookAndFeel.getPrimaryControlShadow());
   436                 g.drawLine(4, fillMinY, 4, fillMaxY);
   423                         g.drawLine(1, fillMinY, 1, fillMaxY);
       
   424                         g.setColor(leftToRight ? sliderAltTrackColor : MetalLookAndFeel.getControlShadow());
       
   425                         g.drawLine(2, fillMinY, 2, fillMaxY);
       
   426                         g.setColor(leftToRight ? MetalLookAndFeel.getControlShadow() : sliderAltTrackColor);
       
   427                         g.drawLine(3, fillMinY, 3, fillMaxY);
       
   428                         g.setColor(leftToRight ? MetalLookAndFeel.getPrimaryControlShadow() : MetalLookAndFeel.getWhite());
       
   429                         g.drawLine(4, fillMinY, 4, fillMaxY);
       
   430                     }
       
   431                 }
       
   432             } else {
       
   433                 g.setColor(MetalLookAndFeel.getControlShadow());
       
   434 
       
   435                 if (middleOfThumb > 0) {
       
   436                     if (drawInverted && filledSlider) {
       
   437                         g.fillRect(0, 0, w - 1, middleOfThumb - 1);
       
   438                     } else {
       
   439                         g.drawRect(0, 0, w - 1, middleOfThumb - 1);
       
   440                     }
       
   441                 }
       
   442 
       
   443                 if (middleOfThumb < h) {
       
   444                     if (!drawInverted && filledSlider) {
       
   445                         g.fillRect(0, middleOfThumb, w - 1, h - middleOfThumb - 1);
       
   446                     } else {
       
   447                         g.drawRect(0, middleOfThumb, w - 1, h - middleOfThumb - 1);
       
   448                     }
       
   449                 }
   437             }
   450             }
   438         }
   451         }
   439 
   452 
   440         g.translate(-paintRect.x, -paintRect.y);
   453         g.translate(-paintRect.x, -paintRect.y);
   441     }
   454     }