blob: c3c1f593d1bfeab3ff810c93024410a74076c72f (
plain)
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
|
package jcgp.gui.settings.parameters;
import java.text.DecimalFormat;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.control.Control;
import javafx.scene.control.TextField;
import jcgp.backend.parameters.Parameter;
import jcgp.backend.parameters.ParameterStatus;
import jcgp.gui.settings.SettingsPane;
/**
* This extension of @code{GUIParameter} uses a @code{TextField} to display
* the value of a @code{DoubleParameter}. It cannot be constructed
* directly - instead, use @code{GUIParameter.create()}.
* <br><br>
* See {@link GUIParameter} for more information.
*
* @author Eduardo Pedroni
*/
public class GUIDoubleParameter extends GUIParameter<Number> {
private TextField textField;
private DecimalFormat decimalFormat;
/**
* This protected constructor is intended for use
* by the factory method only.
*
*/
protected GUIDoubleParameter(Parameter<Number> parameter, SettingsPane sp) {
super(parameter, sp);
}
@Override
protected Control makeControl() {
// we use a text field, and a formatting class to enforce decimals
decimalFormat = new DecimalFormat();
decimalFormat.setMaximumFractionDigits(10);
textField = new TextField(decimalFormat.format(parameter.get().doubleValue()));
textField.setStyle(VALID_PARAMETER_STYLE);
textField.setAlignment(Pos.CENTER_RIGHT);
textField.prefWidthProperty().bind(widthProperty().divide(2));
return textField;
}
@Override
protected void setControlListeners() {
/* pass the TextField value back to the parameter whenever it gets
* modified, provided it is not empty, the experiment isn't running
* and it matches the double-precision regex filter */
textField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(
ObservableValue<? extends String> observable,
String oldValue, String newValue) {
if (!settingsPane.isExperimentRunning()) {
if (newValue.matches("^[-+]?[0-9]*\\.?[0-9]+$")) {
if (!newValue.isEmpty()) {
double value = Double.parseDouble(newValue);
parameter.set(value);
settingsPane.revalidateParameters();
}
} else {
refreshValue();
}
}
}
});
/* if the TextField loses focus and is empty, set it to the current
* value of the parameter */
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(
ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue) {
if (!newValue) {
refreshValue();
}
}
});
}
@Override
protected void setValidityStyle() {
// update the Control's style and tooltip based on the status of the parameter
if (parameter.getStatus() == ParameterStatus.INVALID) {
textField.setStyle(BASE_TEXT_STYLE + INVALID_PARAMETER_STYLE);
textField.setTooltip(tooltip);
tooltip.setText(parameter.getStatus().getDetails());
} else if (parameter.getStatus() == ParameterStatus.WARNING || parameter.getStatus() == ParameterStatus.WARNING_RESET) {
textField.setStyle(BASE_TEXT_STYLE + WARNING_PARAMETER_STYLE);
textField.setTooltip(tooltip);
tooltip.setText(parameter.getStatus().getDetails());
} else {
textField.setStyle(BASE_TEXT_STYLE + VALID_PARAMETER_STYLE);
textField.setTooltip(null);
}
}
@Override
public void refreshValue() {
if (!textField.isFocused()) {
textField.setText(decimalFormat.format(parameter.get().doubleValue()));
}
}
}
|