-
Notifications
You must be signed in to change notification settings - Fork 23
/
DoubleConfigKey.java
166 lines (152 loc) · 5.23 KB
/
DoubleConfigKey.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package uk.ac.starlink.ttools.plot2.config;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
/**
* Config key for double precision values.
* NaN and null are not distinguished.
*
* <p>Static methods are provided to produce config keys with a variety of
* GUI options for specifying values.
*
* @author Mark Taylor
* @since 22 Feb 2013
*/
public abstract class DoubleConfigKey extends ConfigKey<Double> {
/**
* Constructor.
*
* @param meta metadata
* @param dflt default value
*/
protected DoubleConfigKey( ConfigMeta meta, double dflt ) {
super( meta, Double.class, new Double( dflt ) );
if ( meta.getStringUsage() == null ) {
meta.setStringUsage( "<number>" );
}
}
public String valueToString( Double value ) {
return ( value == null || Double.isNaN( value.doubleValue() ) )
? ""
: value.toString();
}
public Double stringToValue( String txt ) throws ConfigException {
if ( txt == null || txt.trim().length() == 0 ) {
return new Double( Double.NaN );
}
else {
try {
return Double.valueOf( txt.trim() );
}
catch ( NumberFormatException e ) {
throw new ConfigException( this,
"\"" + txt + "\" not numeric", e );
}
}
}
/**
* Constructs a key with a text field specifier and default NaN.
*
* @param meta metadata
* @return key
*/
public static DoubleConfigKey createTextKey( ConfigMeta meta ) {
return createTextKey( meta, Double.NaN );
}
/**
* Constructs a key with a text field specifier and an explicit default.
*
* @param meta metadata
* @param dflt default value
* @return key
*/
public static DoubleConfigKey createTextKey( ConfigMeta meta,
double dflt ) {
return new DoubleConfigKey( meta, dflt ) {
public Specifier<Double> createSpecifier() {
return new TextFieldSpecifier( this, new Double( Double.NaN ) );
}
};
}
/**
* Returns a key with a specifier that only provides a
* toggle between two values.
*
* @param meta metadata
* @param fval value for toggle false (default)
* @param tval value for toggle true
* @return key
*/
public static DoubleConfigKey createToggleKey( ConfigMeta meta,
final double fval,
final double tval ) {
return new DoubleConfigKey( meta, fval ) {
public Specifier<Double> createSpecifier() {
return new ToggleSpecifier( fval, tval );
}
};
}
/**
* Returns a key with a linear or logarithmic slider for a specifier.
* Note the lower and upper bounds configure only the slider range,
* they do not enforce a range when the value is set from a string value.
*
* @param meta metadata
* @param dflt default value
* @param lo slider lower bound
* @param hi slider upper bound
* @param log true for logarithmic slider scale, false for linear
* @return key
*/
public static DoubleConfigKey createSliderKey( ConfigMeta meta,
final double dflt,
final double lo,
final double hi,
final boolean log ) {
return new DoubleConfigKey( meta, dflt ) {
public Specifier<Double> createSpecifier() {
return new SliderSpecifier( lo, hi, log, dflt );
}
};
}
/**
* Check box specifier for double values.
* One of two floating point values can be chosen from the GUI.
*/
private static class ToggleSpecifier extends SpecifierPanel<Double> {
private final Double fVal_;
private final Double tVal_;
private final JCheckBox checkBox_;
/**
* Constructor.
*
* @param fval value if unchecked
* @param tval value if checked
*/
ToggleSpecifier( double fval, double tval ) {
super( false );
fVal_ = new Double( fval );
tVal_ = new Double( tval );
checkBox_ = new JCheckBox();
}
protected JComponent createComponent() {
checkBox_.addActionListener( getActionForwarder() );
return checkBox_;
}
public Double getSpecifiedValue() {
return checkBox_.isSelected() ? tVal_ : fVal_;
}
public void setSpecifiedValue( Double value ) {
if ( value == null ) {
value = new Double( Double.NaN );
}
if ( value.equals( fVal_ ) ) {
checkBox_.setSelected( false );
}
else if ( value.equals( tVal_ ) ) {
checkBox_.setSelected( true );
}
fireAction();
}
}
}