From efe490fec1c7a94f004b496c7c97c82083fe44ec Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 21 Apr 2014 00:09:55 +0100 Subject: Tooltips are looking strange, checking on a different machine --- .../settings/parameters/GUIDoubleParameter.java | 41 +++++++++++----------- .../settings/parameters/GUIIntegerParameter.java | 32 ++++++++--------- src/jcgp/gui/settings/parameters/GUIParameter.java | 12 +++---- 3 files changed, 39 insertions(+), 46 deletions(-) (limited to 'src/jcgp/gui/settings/parameters') diff --git a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java index 29648ca..9a6ec4a 100644 --- a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java @@ -1,12 +1,12 @@ package jcgp.gui.settings.parameters; +import java.text.DecimalFormat; + import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; -import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.control.Control; import javafx.scene.control.TextField; -import javafx.scene.input.KeyEvent; import jcgp.backend.resources.parameters.Parameter; import jcgp.backend.resources.parameters.ParameterStatus; import jcgp.gui.settings.SettingsPane; @@ -23,6 +23,7 @@ import jcgp.gui.settings.SettingsPane; public class GUIDoubleParameter extends GUIParameter { private TextField textField; + private DecimalFormat decimalFormat; /** * This default-visibility constructor is intended for use @@ -31,6 +32,8 @@ public class GUIDoubleParameter extends GUIParameter { */ GUIDoubleParameter(Parameter parameter, SettingsPane sp) { super(parameter, sp); + decimalFormat = new DecimalFormat(); + decimalFormat.setMaximumFractionDigits(10); } @Override @@ -38,23 +41,12 @@ public class GUIDoubleParameter extends GUIParameter { textField = new TextField(String.valueOf(parameter.get())); textField.setStyle(VALID_PARAMETER_STYLE); textField.setAlignment(Pos.CENTER_RIGHT); - + textField.prefWidthProperty().bind(widthProperty().divide(2)); return textField; } @Override protected void setControlListeners() { - /* filter keypresses and ignore anything that is not a number - * and any decimal point beyond the first ones */ - textField.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.getText().contains(".")))) { - t.consume(); - } - } - }); /* pass the TextField value back to the parameter whenever it gets * modified, provided it is not empty and the experiment isn't running */ textField.textProperty().addListener(new ChangeListener() { @@ -62,11 +54,17 @@ public class GUIDoubleParameter extends GUIParameter { public void changed( ObservableValue observable, String oldValue, String newValue) { - if (!newValue.isEmpty() && !settingsPane.isExperimentRunning()) { - parameter.set(Double.parseDouble(newValue)); - settingsPane.revalidateParameters(); + if (!settingsPane.isExperimentRunning()) { + if (newValue.matches("([0-9]*[.]*[0-9]*)")) { + if (!newValue.isEmpty()) { + double value = Double.parseDouble(newValue); + parameter.set(value); + settingsPane.revalidateParameters(); + } + } else { + refreshValue(); + } } - } }); /* if the TextField loses focus and is empty, set it to the current @@ -76,8 +74,8 @@ public class GUIDoubleParameter extends GUIParameter { public void changed( ObservableValue observable, Boolean oldValue, Boolean newValue) { - if (!newValue && textField.getText().isEmpty()) { - textField.setText(String.valueOf(parameter.get())); + if (!newValue) { + refreshValue(); } } }); @@ -102,6 +100,7 @@ public class GUIDoubleParameter extends GUIParameter { @Override public void refreshValue() { - textField.setText(String.valueOf((parameter).get())); + System.out.println("setting text to: " + parameter.get().doubleValue()); + textField.setText(decimalFormat.format(parameter.get().doubleValue())); } } diff --git a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java index da2c11f..ee266f3 100644 --- a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java @@ -2,11 +2,9 @@ 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.Control; import javafx.scene.control.TextField; -import javafx.scene.input.KeyEvent; import jcgp.backend.resources.parameters.Parameter; import jcgp.backend.resources.parameters.ParameterStatus; import jcgp.gui.settings.SettingsPane; @@ -38,22 +36,13 @@ public class GUIIntegerParameter extends GUIParameter { textField = new TextField(String.valueOf(parameter.get())); textField.setStyle(VALID_PARAMETER_STYLE); textField.setAlignment(Pos.CENTER_RIGHT); + textField.prefWidthProperty().bind(widthProperty().divide(2)); return textField; } @Override protected void setControlListeners() { - /* filter keypresses and ignore anything that is not a number */ - textField.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')) { - t.consume(); - } - } - }); /* pass the TextField value back to the parameter whenever it gets * modified, provided it is not empty and the experiment isn't running */ textField.textProperty().addListener(new ChangeListener() { @@ -61,9 +50,16 @@ public class GUIIntegerParameter extends GUIParameter { public void changed( ObservableValue observable, String oldValue, String newValue) { - if (!newValue.isEmpty() && !settingsPane.isExperimentRunning()) { - parameter.set(Double.parseDouble(newValue)); - settingsPane.revalidateParameters(); + if (!settingsPane.isExperimentRunning()) { + if (newValue.matches("[0-9]*")) { + if (!newValue.isEmpty()) { + int value = Integer.parseInt(newValue); + parameter.set(value); + settingsPane.revalidateParameters(); + } + } else { + refreshValue(); + } } } }); @@ -74,8 +70,8 @@ public class GUIIntegerParameter extends GUIParameter { public void changed( ObservableValue observable, Boolean oldValue, Boolean newValue) { - if (!newValue && textField.getText().isEmpty()) { - textField.setText(String.valueOf(parameter.get())); + if (!newValue) { + refreshValue(); } } }); @@ -101,6 +97,6 @@ public class GUIIntegerParameter extends GUIParameter { @Override public void refreshValue() { - textField.setText(String.valueOf((parameter).get())); + textField.setText(String.valueOf(parameter.get())); } } diff --git a/src/jcgp/gui/settings/parameters/GUIParameter.java b/src/jcgp/gui/settings/parameters/GUIParameter.java index 159e709..7dd7231 100644 --- a/src/jcgp/gui/settings/parameters/GUIParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIParameter.java @@ -7,10 +7,9 @@ import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Pos; import javafx.scene.control.Control; +import javafx.scene.control.Label; import javafx.scene.control.Tooltip; import javafx.scene.layout.HBox; -import javafx.scene.layout.Priority; -import javafx.scene.text.Text; import jcgp.backend.resources.parameters.BooleanParameter; import jcgp.backend.resources.parameters.DoubleParameter; import jcgp.backend.resources.parameters.IntegerParameter; @@ -46,7 +45,7 @@ public abstract class GUIParameter extends HBox { 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; - private Text name; + private Label name; private Control valueControl; protected SettingsPane settingsPane; @@ -80,9 +79,9 @@ public abstract class GUIParameter extends HBox { setAlignment(Pos.CENTER_LEFT); setSpacing(5); - name = new Text(parameter.getName()); + name = new Label(parameter.getName()); // set text width to half of the total width of the GUIParameter - name.wrappingWidthProperty().bind(widthProperty().divide(2)); + name.prefWidthProperty().bind(widthProperty().divide(2)); // the tooltip is the hover-over label containing status information, when appropriate tooltip = new Tooltip(); @@ -90,7 +89,6 @@ public abstract class GUIParameter extends HBox { tooltip.setSkin(null); valueControl = makeControl(); - setHgrow(valueControl, Priority.ALWAYS); // if the parameter is a monitor, it should be permanently disabled valueControl.setDisable(parameter.isMonitor()); @@ -177,7 +175,7 @@ public abstract class GUIParameter extends HBox { */ public void validate() { parameter.validate(parameter.get()); - refreshValue(); + //refreshValue(); setValidityStyle(); } -- cgit v1.2.3