aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java
blob: 9c84c6a470e172d9125622f27fd198025c68e869 (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
110
111
112
113
114
115
116
117
118
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.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Priority;
import javafx.scene.text.Text;
import jcgp.backend.resources.parameters.IntegerParameter;
import jcgp.backend.resources.parameters.ParameterStatus;
import jcgp.gui.settings.SettingsPane;

public class GUIIntegerParameter extends GUIParameter {

	private int originalValue;
	
	GUIIntegerParameter(final IntegerParameter parameter, final SettingsPane sp) {	
		super();
		
		this.parameter = parameter;
		
		originalValue = parameter.get();
		
		name = new Text(parameter.getName());
		valueControl = new TextField(String.valueOf(parameter.get()));
		valueControl.setStyle(VALID_PARAMETER_STYLE);
		
		((TextField) valueControl).setAlignment(Pos.CENTER_RIGHT);
		
		setHgrow(valueControl, Priority.ALWAYS);
		setHgrow(name, Priority.ALWAYS);
		
		name.wrappingWidthProperty().bind(widthProperty().divide(2));
		
		((TextField) valueControl).setEditable(!parameter.isMonitor());
		
		// bind if monitor, else set listeners
		if (parameter.isMonitor()) {
			makeLightBinding();
		} else {
			valueControl.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();
					}
				}
			});
			((TextField) valueControl).textProperty().addListener(new ChangeListener<String>() {
				@Override
				public void changed(
						ObservableValue<? extends String> observable,
						String oldValue, String newValue) {
					if (!newValue.isEmpty()) {
						parameter.set(Integer.parseInt(newValue));
						setValidityStyle();
						sp.revalidateParameters();
					}

				}
			});
			valueControl.focusedProperty().addListener(new ChangeListener<Boolean>() {
				@Override
				public void changed(
						ObservableValue<? extends Boolean> observable,
						Boolean oldValue, Boolean newValue) {
					if (!newValue && ((TextField) valueControl).getText().isEmpty()) {
						((TextField) valueControl).setText(String.valueOf(parameter.get()));
					}
				}
			});
		}
				
		getChildren().addAll(name, valueControl);
	}

	@Override
	public void refreshValue() {
		((TextField) valueControl).setText(String.valueOf(((IntegerParameter) parameter).get()));
	}

	@Override
	public boolean requiresReset() {
		// true if critical and the value has changed, or it requires a reset anyway
		return (parameter.isCritical() && ((IntegerParameter) parameter).get() != originalValue)
				|| parameter.getStatus() == ParameterStatus.WARNING_RESET;
	}
	
	@Override
	public void applyValue() {
		originalValue = ((IntegerParameter) parameter).get();
	}

	@Override
	public void validate() {
		((IntegerParameter) parameter).validate(((IntegerParameter) parameter).get());
		setValidityStyle();
	}
	
	private void setValidityStyle() {
		if (parameter.getStatus() == ParameterStatus.INVALID) {
			valueControl.setStyle(BASE_TEXT_STYLE + INVALID_PARAMETER_STYLE);
			valueControl.setTooltip(tooltip);
			tooltip.setText(parameter.getStatus().getDetails());
		} else if (parameter.getStatus() == ParameterStatus.WARNING || parameter.getStatus() ==  ParameterStatus.WARNING_RESET) {
			valueControl.setStyle(BASE_TEXT_STYLE + WARNING_PARAMETER_STYLE);
			valueControl.setTooltip(tooltip);
			tooltip.setText(parameter.getStatus().getDetails());
		} else {
			valueControl.setStyle(BASE_TEXT_STYLE + VALID_PARAMETER_STYLE);
			valueControl.setTooltip(null);
		}
	}

}