Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.gui

File Slider.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
33% of files have more coverage

Code metrics

2
15
6
1
113
44
7
0.47
2.5
6
1.17

Classes

Class Line # Actions
Slider 16 15 7
0.565217456.5%
 

Contributing tests

This file is covered by 1 test. .

Source view

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  2 toggle public Slider(float min, float max, float value)
42    {
43  2 super();
44  2 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  2 toggle public void setSliderModel(float min, float max, float value)
56    {
57  2 trueMin = min;
58  2 trueMax = max;
59  2 setMinimum(0);
60  2 sliderScaleFactor = 100f / (max - min);
61  2 int sliderMax = (int) ((max - min) * sliderScaleFactor);
62  2 setMaximum(sliderMax);
63  2 setSliderValue(value);
64    }
65   
66    /**
67    * Answers the value of the slider position (descaled to 'true' value)
68    *
69    * @return
70    */
 
71  0 toggle public float getSliderValue()
72    {
73    /*
74    * convert slider max to 'true max' in case of rounding errors
75    */
76  0 int value = getValue();
77  0 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  2 toggle public void setSliderValue(float value)
87    {
88  2 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  0 toggle public float getSliderPercentageValue()
98    {
99  0 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  0 toggle public void setSliderPercentageValue(float pct)
109    {
110  0 float pc = pct / 100f * getMaximum();
111  0 setValue((int) pc);
112    }
113    }