1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.java.swing.plaf.windows;
  27 
  28 import java.awt.*;
  29 import java.awt.event.MouseEvent;
  30 
  31 import javax.swing.plaf.*;
  32 import javax.swing.plaf.basic.*;
  33 import javax.swing.*;
  34 
  35 import static com.sun.java.swing.plaf.windows.TMSchema.*;
  36 import static com.sun.java.swing.plaf.windows.XPStyle.Skin;
  37 
  38 
  39 /**
  40  * Windows rendition of the component.
  41  * <p>
  42  * <strong>Warning:</strong>
  43  * Serialized objects of this class will not be compatible with
  44  * future Swing releases.  The current serialization support is appropriate
  45  * for short term storage or RMI between applications running the same
  46  * version of Swing.  A future release of Swing will provide support for
  47  * long term persistence.
  48  */
  49 public class WindowsSliderUI extends BasicSliderUI
  50 {
  51     private boolean rollover = false;
  52     private boolean pressed = false;
  53 
  54     public WindowsSliderUI(JSlider b){
  55         super(b);
  56     }
  57 
  58     public static ComponentUI createUI(JComponent b) {
  59         return new WindowsSliderUI((JSlider)b);
  60     }
  61 
  62 
  63     /**
  64      * Overrides to return a private track listener subclass which handles
  65      * the HOT, PRESSED, and FOCUSED states.
  66      * @since 1.6
  67      */
  68     protected TrackListener createTrackListener(JSlider slider) {
  69         return new WindowsTrackListener();
  70     }
  71 
  72     private class WindowsTrackListener extends TrackListener {
  73 
  74         public void mouseMoved(MouseEvent e) {
  75             updateRollover(thumbRect.contains(e.getX(), e.getY()));
  76             super.mouseMoved(e);
  77         }
  78 
  79         public void mouseEntered(MouseEvent e) {
  80             updateRollover(thumbRect.contains(e.getX(), e.getY()));
  81             super.mouseEntered(e);
  82         }
  83 
  84         public void mouseExited(MouseEvent e) {
  85             updateRollover(false);
  86             super.mouseExited(e);
  87         }
  88 
  89         public void mousePressed(MouseEvent e) {
  90             updatePressed(thumbRect.contains(e.getX(), e.getY()));
  91             super.mousePressed(e);
  92         }
  93 
  94         public void mouseReleased(MouseEvent e) {
  95             updatePressed(false);
  96             super.mouseReleased(e);
  97         }
  98 
  99         public void updatePressed(boolean newPressed) {
 100             // You can't press a disabled slider
 101             if (!slider.isEnabled()) {
 102                 return;
 103             }
 104             if (pressed != newPressed) {
 105                 pressed = newPressed;
 106                 slider.repaint(thumbRect);
 107             }
 108         }
 109 
 110         public void updateRollover(boolean newRollover) {
 111             // You can't have a rollover on a disabled slider
 112             if (!slider.isEnabled()) {
 113                 return;
 114             }
 115             if (rollover != newRollover) {
 116                 rollover = newRollover;
 117                 slider.repaint(thumbRect);
 118             }
 119         }
 120 
 121     }
 122 
 123 
 124     public void paintTrack(Graphics g)  {
 125         XPStyle xp = XPStyle.getXP();
 126         if (xp != null) {
 127             boolean vertical = (slider.getOrientation() == JSlider.VERTICAL);
 128             Part part = vertical ? Part.TKP_TRACKVERT : Part.TKP_TRACK;
 129             Skin skin = xp.getSkin(slider, part);
 130 
 131             if (vertical) {
 132                 int x = (trackRect.width - skin.getWidth()) / 2;
 133                 skin.paintSkin(g, trackRect.x + x, trackRect.y,
 134                                skin.getWidth(), trackRect.height, null);
 135             } else {
 136                 int y = (trackRect.height - skin.getHeight()) / 2;
 137                 skin.paintSkin(g, trackRect.x, trackRect.y + y,
 138                                trackRect.width, skin.getHeight(), null);
 139             }
 140         } else {
 141             super.paintTrack(g);
 142         }
 143     }
 144 
 145 
 146     protected void paintMinorTickForHorizSlider( Graphics g, Rectangle tickBounds, int x ) {
 147         XPStyle xp = XPStyle.getXP();
 148         if (xp != null) {
 149             g.setColor(xp.getColor(slider, Part.TKP_TICS, null, Prop.COLOR, Color.black));
 150         }
 151         super.paintMinorTickForHorizSlider(g, tickBounds, x);
 152     }
 153 
 154     protected void paintMajorTickForHorizSlider( Graphics g, Rectangle tickBounds, int x ) {
 155         XPStyle xp = XPStyle.getXP();
 156         if (xp != null) {
 157             g.setColor(xp.getColor(slider, Part.TKP_TICS, null, Prop.COLOR, Color.black));
 158         }
 159         super.paintMajorTickForHorizSlider(g, tickBounds, x);
 160     }
 161 
 162     protected void paintMinorTickForVertSlider( Graphics g, Rectangle tickBounds, int y ) {
 163         XPStyle xp = XPStyle.getXP();
 164         if (xp != null) {
 165             g.setColor(xp.getColor(slider, Part.TKP_TICSVERT, null, Prop.COLOR, Color.black));
 166         }
 167         super.paintMinorTickForVertSlider(g, tickBounds, y);
 168     }
 169 
 170     protected void paintMajorTickForVertSlider( Graphics g, Rectangle tickBounds, int y ) {
 171         XPStyle xp = XPStyle.getXP();
 172         if (xp != null) {
 173             g.setColor(xp.getColor(slider, Part.TKP_TICSVERT, null, Prop.COLOR, Color.black));
 174         }
 175         super.paintMajorTickForVertSlider(g, tickBounds, y);
 176     }
 177 
 178 
 179     public void paintThumb(Graphics g)  {
 180         XPStyle xp = XPStyle.getXP();
 181         if (xp != null) {
 182             Part part = getXPThumbPart();
 183             State state = State.NORMAL;
 184 
 185             if (slider.hasFocus()) {
 186                 state = State.FOCUSED;
 187             }
 188             if (rollover) {
 189                 state = State.HOT;
 190             }
 191             if (pressed) {
 192                 state = State.PRESSED;
 193             }
 194             if(!slider.isEnabled()) {
 195                 state = State.DISABLED;
 196             }
 197 
 198             xp.getSkin(slider, part).paintSkin(g, thumbRect.x, thumbRect.y, state);
 199         } else {
 200             super.paintThumb(g);
 201         }
 202     }
 203 
 204     protected Dimension getThumbSize() {
 205         XPStyle xp = XPStyle.getXP();
 206         if (xp != null) {
 207             Dimension size = new Dimension();
 208             Skin s = xp.getSkin(slider, getXPThumbPart());
 209             size.width = s.getWidth();
 210             size.height = s.getHeight();
 211             return size;
 212         } else {
 213             return super.getThumbSize();
 214         }
 215     }
 216 
 217     private Part getXPThumbPart() {
 218         Part part;
 219         boolean vertical = (slider.getOrientation() == JSlider.VERTICAL);
 220         boolean leftToRight = slider.getComponentOrientation().isLeftToRight();
 221         Boolean paintThumbArrowShape =
 222                 (Boolean)slider.getClientProperty("Slider.paintThumbArrowShape");
 223         if ((!slider.getPaintTicks() && paintThumbArrowShape == null) ||
 224             paintThumbArrowShape == Boolean.FALSE) {
 225                 part = vertical ? Part.TKP_THUMBVERT
 226                                 : Part.TKP_THUMB;
 227         } else {
 228                 part = vertical ? (leftToRight ? Part.TKP_THUMBRIGHT : Part.TKP_THUMBLEFT)
 229                                 : Part.TKP_THUMBBOTTOM;
 230         }
 231         return part;
 232     }
 233 }