aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java
blob: da2c11fde57cc9652246262662284ef4b23c2bd3 (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
package jcgp.gui.settings.parameters;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Control;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import jcgp.backend.resources.parameters.Parameter;
import jcgp.backend.resources.parameters.ParameterStatus;
import jcgp.gui.settings.SettingsPane;

/**
 * This extension of GUIParameter uses a TextField to display
 * the value of a IntegerParameter. It cannot be constructed
 * directly - instead, use GUIParameter.create().
 * <br><br>
 * See {@link GUIParameter} for more information.
 * 
 * @author Eduardo Pedroni
 */
public class GUIIntegerParameter extends GUIParameter<Number> {
	
	private TextField textField;
	
	/**
	 * This default-visibility constructor is intended for use
	 * by the factory method only.
	 * 
	 */
	GUIIntegerParameter(Parameter<Number> parameter, SettingsPane sp) {
		super(parameter, sp);
	}

	@Override
	protected Control makeControl() {
		textField = new TextField(String.valueOf(parameter.get()));
		textField.setStyle(VALID_PARAMETER_STYLE);
		textField.setAlignment(Pos.CENTER_RIGHT);
		
		return textField;
	}

	@Override
	protected void setControlListeners() {
		/* filter keypresses and ignore anything that is not a number */
		textField.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
			@Override
			public void handle( KeyEvent t ) {
				char ch = t.getCharacter().toCharArray()[t.getCharacter().toCharArray().length - 1];
				if (!(ch >= '0' && ch <= '9')) {
					t.consume();
				}
			}
		});
		/* pass the TextField value back to the parameter whenever it gets
		 * modified, provided it is not empty and the experiment isn't running */
		textField.textProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(
					ObservableValue<? extends String> observable,
					String oldValue, String newValue) {
				if (!newValue.isEmpty() && !settingsPane.isExperimentRunning()) {
					parameter.set(Double.parseDouble(newValue));
					settingsPane.revalidateParameters();
				}
			}
		});
		/* 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 && textField.getText().isEmpty()) {
					textField.setText(String.valueOf(parameter.get()));
				}
			}
		});
	}
	
	@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() {
		textField.setText(String.valueOf((parameter).get()));
	}
}