c8f673d5f66e30b2ca035a121280321a7f302cfd
[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     int extent = 1;
63     setExtent(extent);
64     setMaximum(sliderMax + extent);
65     setSliderValue(value);
66   }
67
68   /**
69    * Answers the value of the slider position (descaled to 'true' value)
70    * 
71    * @return
72    */
73   public float getSliderValue()
74   {
75     /*
76      * convert slider max to 'true max' in case of rounding errors
77      */
78     int value = getValue();
79     return value == getMaximum() ? trueMax
80             : value / sliderScaleFactor + trueMin;
81   }
82
83   /**
84    * Sets the slider value (scaled from the true value to the slider range)
85    * 
86    * @param value
87    */
88   public void setSliderValue(float value)
89   {
90     setValue(Math.round((value - trueMin) * sliderScaleFactor));
91   }
92
93   /**
94    * Answers the value of the slider position as a percentage between minimum and
95    * maximum of its range
96    * 
97    * @return
98    */
99   public float getSliderPercentageValue()
100   {
101     return (getValue() - getMinimum()) * 100f
102             / (getMaximum() - getMinimum());
103   }
104
105   /**
106    * Sets the slider position for a given percentage value of its min-max range
107    * 
108    * @param pct
109    */
110   public void setSliderPercentageValue(float pct)
111   {
112     float pc = pct / 100f * getMaximum();
113     setValue((int) pc);
114   }
115 }