JAL-3371 JAL-2983 don't modify 'extent' of slider - this is managed by look and feel
[jalview.git] / src / jalview / gui / Slider.java
1 package jalview.gui;
2
3 import javax.swing.JSlider;
4
5 /**
6  * A modified {@code javax.swing.JSlider} that
7  * <ul>
8  * <li>supports float valued numbers (by scaling up integer values)</li>
9  * <li>rescales 'true' value range to avoid negative values, as these are not
10  * rendered correctly by some look and feel libraries</li>
11  * </ul>
12  * 
13  * @author gmcarstairs
14  */
15 @SuppressWarnings("serial")
16 public class Slider extends JSlider
17 {
18   /*
19    * 'true' value corresponding to zero on the slider
20    */
21   private float trueMin;
22
23   /*
24    * 'true' value corresponding to slider maximum
25    */
26   private float trueMax;
27
28   /*
29    * scaleFactor applied to true value range to give a
30    * slider range of 0 - 100
31    */
32   private float sliderScaleFactor;
33
34   /**
35    * Constructor that rescales min - max to 0 - 100 for the slider
36    * 
37    * @param min
38    * @param max
39    * @param value
40    */
41   public Slider(float min, float max, float value)
42   {
43     super();
44     setSliderModel(min, max, value);
45   }
46
47   /**
48    * Sets the min-max range and current value of the slider, with rescaling from
49    * true values to slider range as required
50    * 
51    * @param min
52    * @param max
53    * @param value
54    */
55   public void setSliderModel(float min, float max, float value)
56   {
57     trueMin = min;
58     trueMax = max;
59     setMinimum(0);
60     sliderScaleFactor = 100f / (max - min);
61     int sliderMax = (int) ((max - min) * sliderScaleFactor);
62     setMaximum(sliderMax);
63     setSliderValue(value);
64   }
65
66   /**
67    * Answers the value of the slider position (descaled to 'true' value)
68    * 
69    * @return
70    */
71   public float getSliderValue()
72   {
73     /*
74      * convert slider max to 'true max' in case of rounding errors
75      */
76     int value = getValue();
77     return value == getMaximum() ? trueMax
78             : value / sliderScaleFactor + trueMin;
79   }
80
81   /**
82    * Sets the slider value (scaled from the true value to the slider range)
83    * 
84    * @param value
85    */
86   public void setSliderValue(float value)
87   {
88     setValue(Math.round((value - trueMin) * sliderScaleFactor));
89   }
90
91   /**
92    * Answers the value of the slider position as a percentage between minimum and
93    * maximum of its range
94    * 
95    * @return
96    */
97   public float getSliderPercentageValue()
98   {
99     return (getValue() - getMinimum()) * 100f
100             / (getMaximum() - getMinimum());
101   }
102
103   /**
104    * Sets the slider position for a given percentage value of its min-max range
105    * 
106    * @param pct
107    */
108   public void setSliderPercentageValue(float pct)
109   {
110     float pc = pct / 100f * getMaximum();
111     setValue((int) pc);
112   }
113 }