From 02fd2bc7059da416937beb1abe67e5ca60379030 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Tue, 1 Apr 2014 23:00:53 +0100 Subject: Settings pane now actually controls the parameters, not much left to do. --- .../settings/parameters/GUIBooleanParameter.java | 34 +++++++------ .../settings/parameters/GUIDoubleParameter.java | 39 ++++++++------- .../settings/parameters/GUIIntegerParameter.java | 36 +++++++------- src/jcgp/gui/settings/parameters/GUIParameter.java | 56 ++++++++++++++++++---- 4 files changed, 107 insertions(+), 58 deletions(-) (limited to 'src/jcgp/gui/settings/parameters') diff --git a/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java b/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java index a9f6753..8acd6dd 100644 --- a/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java @@ -1,11 +1,11 @@ package jcgp.gui.settings.parameters; import javafx.scene.control.CheckBox; -import jcgp.parameters.BooleanParameter; +import jcgp.backend.parameters.BooleanParameter; public class GUIBooleanParameter extends GUIParameter { - public GUIBooleanParameter(BooleanParameter parameter) { + GUIBooleanParameter(BooleanParameter parameter) { super(); this.parameter = parameter; @@ -13,22 +13,16 @@ public class GUIBooleanParameter extends GUIParameter { value = new CheckBox(parameter.getName()); ((CheckBox) value).setSelected(parameter.get()); - value.setDisable(!parameter.isEditable()); + value.setDisable(parameter.isMonitor()); - getChildren().add(value); + if (parameter.isMonitor()) { + makeLightBinding(); + System.out.println("light binding made for " + name); + } + getChildren().add(value); } -// @Override -// public void doBindings() { -// ((CheckBox) value).selectedProperty().bind(((BooleanParameter) parameter).valueProperty()); -// } -// -// @Override -// public void undoBindings() { -// ((CheckBox) value).selectedProperty().unbind(); -// } - @Override public void refreshValue() { if (!((CheckBox) value).selectedProperty().isBound()) { @@ -36,5 +30,17 @@ public class GUIBooleanParameter extends GUIParameter { } } + + @Override + public boolean applyChange() { + if (((CheckBox) value).isSelected() != ((BooleanParameter) parameter).get() && (!parameter.isMonitor())) { + // apply value + ((BooleanParameter) parameter).set(((CheckBox) value).isSelected()); + if (parameter.isCritical()) { + return true; + } + } + return false; + } } diff --git a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java index 25d5b9f..4b74c6e 100644 --- a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java @@ -4,12 +4,12 @@ import javafx.geometry.Pos; import javafx.scene.control.TextField; import javafx.scene.layout.Priority; import javafx.scene.text.Text; -import jcgp.GUI; -import jcgp.parameters.DoubleParameter; +import jcgp.backend.parameters.DoubleParameter; +import jcgp.gui.GUI; public class GUIDoubleParameter extends GUIParameter { - public GUIDoubleParameter(DoubleParameter parameter) { + GUIDoubleParameter(DoubleParameter parameter) { super(); this.parameter = parameter; @@ -23,26 +23,29 @@ public class GUIDoubleParameter extends GUIParameter { name.setWrappingWidth(GUI.WRAP_WIDTH); - ((TextField) value).setEditable(parameter.isEditable()); + ((TextField) value).setEditable(!parameter.isMonitor()); + + if (parameter.isMonitor()) { + makeLightBinding(); + } getChildren().addAll(name, value); } -// @Override -// public void doBindings() { -// ((TextField) value).textProperty().bind(((DoubleParameter) parameter).valueProperty().asString()); -// } -// -// @Override -// public void undoBindings() { -// ((TextField) value).textProperty().unbind(); -// } - @Override public void refreshValue() { - if (!((TextField) value).textProperty().isBound()) { - ((TextField) value).setText(String.valueOf(((DoubleParameter) parameter).get())); - } + ((TextField) value).setText(String.valueOf(((DoubleParameter) parameter).get())); } - + + @Override + public boolean applyChange() { + if (Double.valueOf(((TextField) value).getText()) != ((DoubleParameter) parameter).get() && (!parameter.isMonitor())) { + // apply value + ((DoubleParameter) parameter).set(Double.valueOf(((TextField) value).getText())); + if (parameter.isCritical()) { + return true; + } + } + return false; + } } diff --git a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java index 6bcfd55..70b78a6 100644 --- a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java @@ -4,12 +4,12 @@ import javafx.geometry.Pos; import javafx.scene.control.TextField; import javafx.scene.layout.Priority; import javafx.scene.text.Text; -import jcgp.GUI; -import jcgp.parameters.IntegerParameter; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.gui.GUI; public class GUIIntegerParameter extends GUIParameter { - public GUIIntegerParameter(IntegerParameter parameter) { + GUIIntegerParameter(IntegerParameter parameter) { super(); this.parameter = parameter; @@ -23,27 +23,27 @@ public class GUIIntegerParameter extends GUIParameter { name.setWrappingWidth(GUI.WRAP_WIDTH); - ((TextField) value).setEditable(parameter.isEditable()); + ((TextField) value).setEditable(!parameter.isMonitor()); + + if (parameter.isMonitor()) { + makeLightBinding(); + } getChildren().addAll(name, value); } -// @Override -// public void doBindings() { -// ((TextField) value).textProperty().bind(((IntegerParameter) parameter).valueProperty().asString()); -// } -// -// @Override -// public void undoBindings() { -// ((TextField) value).textProperty().unbind(); -// } - - public TextField getTextField() { - return (TextField) value; - } - @Override public void refreshValue() { ((TextField) value).setText(String.valueOf(((IntegerParameter) parameter).get())); } + + @Override + public boolean applyChange() { + if (Integer.valueOf(((TextField) value).getText()) != ((IntegerParameter) parameter).get() && (!parameter.isMonitor())) { + // apply value + ((IntegerParameter) parameter).set(Integer.valueOf(((TextField) value).getText())); + return parameter.isCritical(); + } + return false; + } } diff --git a/src/jcgp/gui/settings/parameters/GUIParameter.java b/src/jcgp/gui/settings/parameters/GUIParameter.java index a2f2f9c..a783d71 100644 --- a/src/jcgp/gui/settings/parameters/GUIParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIParameter.java @@ -1,10 +1,18 @@ 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.layout.HBox; import javafx.scene.text.Text; -import jcgp.parameters.Parameter; +import jcgp.backend.parameters.BooleanParameter; +import jcgp.backend.parameters.DoubleParameter; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.backend.parameters.Parameter; public abstract class GUIParameter extends HBox { @@ -12,19 +20,51 @@ public abstract class GUIParameter extends HBox { protected Text name; protected Control value; - public GUIParameter() { + protected AtomicBoolean updateCheck = new AtomicBoolean(true); + + protected GUIParameter() { setAlignment(Pos.CENTER_LEFT); setSpacing(5); } - public Parameter getParameter() { - return parameter; + 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 doBindings(); -// -// public abstract void undoBindings(); - public abstract void refreshValue(); + public static GUIParameter create(Parameter parameter) { + if (parameter instanceof IntegerParameter) { + return new GUIIntegerParameter((IntegerParameter) parameter); + } else if (parameter instanceof DoubleParameter) { + return new GUIDoubleParameter((DoubleParameter) parameter); + } else if (parameter instanceof BooleanParameter) { + return new GUIBooleanParameter((BooleanParameter) parameter); + } else { + throw new ClassCastException("No GUIParameter subclass exists for argument of type " + parameter.getClass()); + } + } + + /** + * Writes the GUI parameter value back to the resources parameter, + * if any changes were made. + * + * @return true if the experiment needs to be reset, false otherwise. + */ + public abstract boolean applyChange(); } -- cgit v1.2.3