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

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.CheckBox;
import jcgp.backend.resources.parameters.BooleanParameter;
import jcgp.backend.resources.parameters.ParameterStatus;
import jcgp.gui.settings.SettingsPane;

public class GUIBooleanParameter extends GUIParameter {
	
	private boolean originalValue;
	
	GUIBooleanParameter(final BooleanParameter parameter, final SettingsPane sp) {
		super();
		
		this.parameter = parameter;
		
		originalValue = parameter.get();
		
		valueControl = new CheckBox(parameter.getName());
		((CheckBox) valueControl).setSelected(parameter.get());
		
		valueControl.setDisable(parameter.isMonitor());
		
		if (parameter.isMonitor()) {
			makeLightBinding();
		} else {
			((CheckBox) valueControl).selectedProperty().addListener(new ChangeListener<Boolean>() {
				@Override
				public void changed(
						ObservableValue<? extends Boolean> observable,
						Boolean oldValue, Boolean newValue) {
					parameter.set(newValue);
					setValidityStyle();
					sp.revalidateParameters();
				}
			});
		}
		
		getChildren().add(valueControl);
	}

	@Override
	public void refreshValue() {
		if (!((CheckBox) valueControl).selectedProperty().isBound()) {
			((CheckBox) valueControl).setSelected(((BooleanParameter) parameter).get());
		}
	}
	
	@Override
	public boolean requiresReset() {
		return (parameter.isCritical() && ((BooleanParameter) parameter).get() != originalValue)
				|| parameter.getStatus() == ParameterStatus.WARNING_RESET;
	}
	
	@Override
	public void applyValue() {
		originalValue = ((BooleanParameter) parameter).get();
	}
	
	/**
	 * @param parameter
	 */
	private void setValidityStyle() {
		if (parameter.getStatus() == ParameterStatus.INVALID) {
			valueControl.setStyle(BASE_CHECKBOX_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_CHECKBOX_STYLE + WARNING_PARAMETER_STYLE);
			valueControl.setTooltip(tooltip);
			tooltip.setText(parameter.getStatus().getDetails());
		} else {
			valueControl.setStyle(BASE_CHECKBOX_STYLE + VALID_PARAMETER_STYLE);
			valueControl.setTooltip(null);
		}
	}

	@Override
	public void validate() {
		((BooleanParameter) parameter).validate(((BooleanParameter) parameter).get());
		setValidityStyle();
	}
}