2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import javax.swing.JSlider;
26 * A modified {@code javax.swing.JSlider} that
28 * <li>supports float valued numbers (by scaling up integer values)</li>
29 * <li>rescales 'true' value range to avoid negative values, as these are not
30 * rendered correctly by some look and feel libraries</li>
35 @SuppressWarnings("serial")
36 public class Slider extends JSlider
39 * the number of nominal positions the slider represents
40 * (higher number = more fine-grained positioning)
42 private static final int SCALE_TICKS = 1000;
45 * 'true' value corresponding to zero on the slider
47 private float trueMin;
50 * 'true' value corresponding to slider maximum
52 private float trueMax;
55 * scaleFactor applied to true value range to give a
56 * slider range of 0 - 100
58 private float sliderScaleFactor;
61 * Constructor that rescales min - max to 0 - 100 for the slider
67 public Slider(float min, float max, float value)
70 setSliderModel(min, max, value);
74 * Sets the min-max range and current value of the slider, with rescaling from
75 * true values to slider range as required
81 public void setSliderModel(float min, float max, float value)
86 sliderScaleFactor = SCALE_TICKS / (max - min);
87 int sliderMax = (int) ((max - min) * sliderScaleFactor);
88 setMaximum(sliderMax);
89 setSliderValue(value);
93 * Answers the value of the slider position (descaled to 'true' value)
97 public float getSliderValue()
100 * convert slider max to 'true max' in case of rounding errors
102 int value = getValue();
103 return value == getMaximum() ? trueMax
104 : value / sliderScaleFactor + trueMin;
108 * Sets the slider value (scaled from the true value to the slider range)
112 public void setSliderValue(float value)
114 setValue(Math.round((value - trueMin) * sliderScaleFactor));
118 * Answers the value of the slider position as a percentage between minimum
119 * and maximum of its range
123 public float getSliderPercentageValue()
125 return (getValue() - getMinimum()) * 100f
126 / (getMaximum() - getMinimum());
130 * Sets the slider position for a given percentage value of its min-max range
134 public void setSliderPercentageValue(float pct)
136 float pc = pct / 100f * getMaximum();