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.DoubleParameter; import jcgp.backend.resources.parameters.ParameterStatus; import jcgp.gui.settings.SettingsPane; public class GUIDoubleParameter extends GUIParameter { private double originalValue; GUIDoubleParameter(final DoubleParameter 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 changelistener if (parameter.isMonitor()) { makeLightBinding(); } else { valueControl.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler() { @Override public void handle( KeyEvent t ) { char ch = t.getCharacter().toCharArray()[t.getCharacter().toCharArray().length - 1]; if (!((ch >= '0' && ch <= '9') || (ch == '.' && !((TextField) valueControl).getText().contains(".")))) { t.consume(); } } }); ((TextField) valueControl).textProperty().addListener(new ChangeListener() { @Override public void changed( ObservableValue observable, String oldValue, String newValue) { if (!newValue.isEmpty()) { parameter.set(Double.parseDouble(newValue)); setValidityStyle(); sp.revalidateParameters(); } } }); valueControl.focusedProperty().addListener(new ChangeListener() { @Override public void changed( ObservableValue 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(((DoubleParameter) parameter).get())); } @Override public boolean requiresReset() { return (parameter.isCritical() && ((DoubleParameter) parameter).get() != originalValue) || parameter.getStatus() == ParameterStatus.WARNING_RESET; } @Override public void applyValue() { originalValue = ((DoubleParameter) parameter).get(); } @Override public void validate() { ((DoubleParameter) parameter).validate(((DoubleParameter) 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); } } }