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

import java.util.concurrent.atomic.AtomicBoolean;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.control.Control;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import jcgp.backend.resources.parameters.BooleanParameter;
import jcgp.backend.resources.parameters.DoubleParameter;
import jcgp.backend.resources.parameters.IntegerParameter;
import jcgp.backend.resources.parameters.Parameter;
import jcgp.backend.resources.parameters.ParameterStatus;
import jcgp.gui.GUI;
import jcgp.gui.settings.SettingsPane;

public abstract class GUIParameter extends HBox {
	
	public static final String BASE_TEXT_STYLE = "-fx-border-color: #C9C9C9; -fx-border-radius: 2; -fx-padding: 0; ";
	public static final String BASE_CHECKBOX_STYLE = "-fx-padding: 0; ";
	
	public static final String INVALID_PARAMETER_STYLE = "-fx-background-color: " + GUI.BAD_SELECTION_COLOUR;
	public static final String WARNING_PARAMETER_STYLE = "-fx-background-color: " + GUI.NEUTRAL_SELECTION_COLOUR;
	public static final String VALID_PARAMETER_STYLE = "-fx-background-color: " + GUI.NEUTRAL_COLOUR;	
		
	protected Parameter parameter;
	protected Text name;
	protected Control valueControl;
	
	protected Tooltip tooltip;
	
	protected AtomicBoolean updateCheck = new AtomicBoolean(true);
	
	protected GUIParameter() {
		setAlignment(Pos.CENTER_LEFT);
		setSpacing(5);
		
		tooltip = new Tooltip();
		tooltip.setStyle("-fx-background-color: white; -fx-border-color: black; .page-corner {-fx-background-color: transparent;}");
		tooltip.setSkin(null);
	}
	
	protected final void makeLightBinding() {
		parameter.valueProperty().addListener(new ChangeListener<Object>() {
			@Override
			public void changed(
					ObservableValue<? extends Object> observable,
					Object oldValue, Object newValue) {
				if (updateCheck.getAndSet(false)) {
					Platform.runLater(new Runnable() {
						@Override
						public void run() {
							updateCheck.set(true);
							refreshValue();
						}
					});
				}
			}
		});
	}
	
	public abstract void refreshValue();
	
	public abstract boolean requiresReset();
	
	public abstract void applyValue();
	
	public abstract void validate();
	
	public static GUIParameter create(Parameter parameter, SettingsPane sp) {
		if (parameter instanceof IntegerParameter) {
			return new GUIIntegerParameter((IntegerParameter) parameter, sp);
		} else if (parameter instanceof DoubleParameter) {
			return new GUIDoubleParameter((DoubleParameter) parameter, sp);
		} else if (parameter instanceof BooleanParameter) {
			return new GUIBooleanParameter((BooleanParameter) parameter, sp);
		} else {
			throw new ClassCastException("No GUIParameter subclass exists for argument of type " + parameter.getClass());
		}
	}
	
	public boolean isValid() {
		return parameter.getStatus() == ParameterStatus.VALID
				|| parameter.getStatus() == ParameterStatus.WARNING
				|| parameter.getStatus() == ParameterStatus.WARNING_RESET;
	}
}