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

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.IntegerParameter;
import jcgp.backend.parameters.ParameterStatus;
import jcgp.gui.constants.Constants;
import jcgp.gui.settings.SettingsPane;

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

	@Override
	protected Control makeControl() {
		// this uses a text field
		textField = new TextField(String.valueOf(parameter.get()));
		textField.setStyle(Constants.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 integer regex pattern */
		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]*")) {
						if (!newValue.isEmpty()) {
							int value = Integer.parseInt(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(Constants.BASE_TEXT_STYLE + Constants.INVALID_PARAMETER_STYLE);
			textField.setTooltip(tooltip);
			tooltip.setText(parameter.getStatus().getDetails());
		} else if (parameter.getStatus() == ParameterStatus.WARNING 
				|| parameter.getStatus() ==  ParameterStatus.WARNING_RESET) {
			textField.setStyle(Constants.BASE_TEXT_STYLE + Constants.WARNING_PARAMETER_STYLE);
			textField.setTooltip(tooltip);
			tooltip.setText(parameter.getStatus().getDetails());
		} else {
			textField.setStyle(Constants.BASE_TEXT_STYLE + Constants.VALID_PARAMETER_STYLE);
			textField.setTooltip(null);
		}
	}

	@Override
	public void refreshValue() {
		if (!textField.isFocused()) {
			textField.setText(parameter.get().toString());
		}
	}
}