184304cfbf0f311f80f0b88b20c7f83e04bcd049
[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    * the number of nominal positions the slider represents
20    * (higher number = more fine-grained positioning)
21    */
22   private static final int SCALE_TICKS = 1000;
23
24   /*
25    * 'true' value corresponding to zero on the slider
26    */
27   private float trueMin;
28
29   /*
30    * 'true' value corresponding to slider maximum
31    */
32   private float trueMax;
33
34   /*
35    * scaleFactor applied to true value range to give a
36    * slider range of 0 - 100
37    */
38   private float sliderScaleFactor;
39
40   /**
41    * Constructor that rescales min - max to 0 - 100 for the slider
42    * 
43    * @param min
44    * @param max
45    * @param value
46    */
47   public Slider(float min, float max, float value)
48   {
49     super();
50     setSliderModel(min, max, value);
51   }
52
53   /**
54    * Sets the min-max range and current value of the slider, with rescaling from
55    * true values to slider range as required
56    * 
57    * @param min
58    * @param max
59    * @param value
60    */
61   public void setSliderModel(float min, float max, float value)
62   {
63     trueMin = min;
64     trueMax = max;
65     setMinimum(0);
66     sliderScaleFactor = SCALE_TICKS / (max - min);
67     int sliderMax = (int) ((max - min) * sliderScaleFactor);
68     setMaximum(sliderMax);
69     setSliderValue(value);
70   }
71
72   /**
73    * Answers the value of the slider position (descaled to 'true' value)
74    * 
75    * @return
76    */
77   public float getSliderValue()
78   {
79     /*
80      * convert slider max to 'true max' in case of rounding errors
81      */
82     int value = getValue();
83     return value == getMaximum() ? trueMax
84             : value / sliderScaleFactor + trueMin;
85   }
86
87   /**
88    * Sets the slider value (scaled from the true value to the slider range)
89    * 
90    * @param value
91    */
92   public void setSliderValue(float value)
93   {
94     setValue(Math.round((value - trueMin) * sliderScaleFactor));
95   }
96
97   /**
98    * Answers the value of the slider position as a percentage between minimum
99    * and maximum of its range
100    * 
101    * @return
102    */
103   public float getSliderPercentageValue()
104   {
105     return (getValue() - getMinimum()) * 100f
106             / (getMaximum() - getMinimum());
107   }
108
109   /**
110    * Sets the slider position for a given percentage value of its min-max range
111    * 
112    * @param pct
113    */
114   public void setSliderPercentageValue(float pct)
115   {
116     float pc = pct / 100f * getMaximum();
117     setValue((int) pc);
118   }
119 }