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() { @Override public void changed( ObservableValue 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; } }