From 0dbf126fc524bc029d9f5803d849b7c8f43fe389 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 3 Apr 2014 15:29:24 +0100 Subject: Visual feedback for parameters implemented. --- src/jcgp/gui/GUI.java | 34 ++++----- src/jcgp/gui/settings/SettingsPane.java | 58 +++++++------- .../settings/parameters/GUIBooleanParameter.java | 63 ++++++++++----- .../settings/parameters/GUIDoubleParameter.java | 89 +++++++++++++++++----- .../settings/parameters/GUIIntegerParameter.java | 84 ++++++++++++++++---- src/jcgp/gui/settings/parameters/GUIParameter.java | 21 ++--- 6 files changed, 245 insertions(+), 104 deletions(-) (limited to 'src/jcgp/gui') diff --git a/src/jcgp/gui/GUI.java b/src/jcgp/gui/GUI.java index 7e7f385..0902b21 100644 --- a/src/jcgp/gui/GUI.java +++ b/src/jcgp/gui/GUI.java @@ -22,16 +22,16 @@ public class GUI extends Application { * (no hovering, clicking, dragging or any other form of interaction). */ public static final String NEUTRAL_COLOUR = "#FFFFFF"; public static final String HARD_HIGHLIGHT_COLOUR = "#5496FF"; - // 89AAD6 public static final String MEDIUM_HIGHLIGHT_COLOUR = "#75BAFF"; public static final String SOFT_HIGHLIGHT_COLOUR = "#C7DFFF"; - // BDFFC2 - public static final String GOOD_SELECTION_COLOUR = "#38C25B"; - // FBFFB8 - public static final String NEUTRAL_SELECTION_COLOUR = "#EDEB72"; - // FF9C9C - public static final String BAD_SELECTION_COLOUR = "#F53D3D"; + public static final String GOOD_SELECTION_COLOUR = "#38C25B"; + public static final String NEUTRAL_SELECTION_COLOUR = "#FFEF73"; + public static final String BAD_SELECTION_COLOUR = "#FF5C5C"; + + public static final String INVALID_PARAMETER_STYLE = "-fx-border-color: C9C9C9; -fx-border-radius: 2; -fx-padding: 0; -fx-background-color: " + BAD_SELECTION_COLOUR; + public static final String WARNING_PARAMETER_STYLE = "-fx-border-color: C9C9C9; -fx-border-radius: 2; -fx-padding: 0; -fx-background-color: " + NEUTRAL_SELECTION_COLOUR; + public static final String VALID_PARAMETER_STYLE = "-fx-border-color: C9C9C9; -fx-border-radius: 2; -fx-padding: 0; -fx-background-color: " + NEUTRAL_COLOUR; /* Sizes and distances */ public static final double RESIZE_MARGIN = 5.0; @@ -71,7 +71,7 @@ public class GUI extends Application { public static void main(String[] args) { cgp = new JCGP(); - resources = cgp.getResources(); + resources = cgp.getResources(); launch(); } @@ -166,17 +166,20 @@ public class GUI extends Application { public void disableChromosomePanes(boolean value) { chromosomeTabs.setDisable(value); } + + public void disable(boolean value) { + chromosomeTabs.setDisable(value); + settings.disableSettings(value); + } public void playPause() { if (!evolving) { - settings.disableSettings(true); - disableChromosomePanes(true); + disable(true); unlockOutputs(); evolving = true; cgpService.restart(); } else { - settings.disableSettings(false); - disableChromosomePanes(false); + disable(false); updateNodeGrids(); evolving = false; cgpService.cancel(); @@ -201,11 +204,8 @@ public class GUI extends Application { } } - public void resetCGP() { - cgp.remakePopulation(); + public void reset() { + cgp.reset(); makeChromosomeTabPane(); - - resources.set("currentGen", 1); - resources.set("currentRun", 1); } } diff --git a/src/jcgp/gui/settings/SettingsPane.java b/src/jcgp/gui/settings/SettingsPane.java index 61011aa..41b1bba 100644 --- a/src/jcgp/gui/settings/SettingsPane.java +++ b/src/jcgp/gui/settings/SettingsPane.java @@ -1,9 +1,6 @@ package jcgp.gui.settings; import java.util.ArrayList; -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; import javafx.event.ActionEvent; import javafx.event.EventHandler; @@ -37,11 +34,15 @@ public class SettingsPane extends AnchorPane { private VBox bpPane, eaPane, mutatorPane, ffPane, nfPane; private ArrayList parameters = new ArrayList(); + private GUI gui; + private boolean dragging = false; public SettingsPane(JCGP cgp, GUI gui) { super(); + this.gui = gui; + setResizeListeners(); mainContainer = new VBox(8); @@ -84,8 +85,7 @@ public class SettingsPane extends AnchorPane { bpPane.getChildren().add(header); - GUIParameter rows = GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("rows")); - parameters.add(rows); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("rows"))); parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("columns"))); parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("inputs"))); parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("outputs"))); @@ -98,14 +98,13 @@ public class SettingsPane extends AnchorPane { parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("currentGen"))); parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("currentRun"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("maxFitness"))); parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("seed"))); parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("report"))); bpPane.getChildren().addAll(parameters); mainContainer.getChildren().add(bpPane); - - } private void initialiseEAParameters(final JCGP cgp) { @@ -122,8 +121,9 @@ public class SettingsPane extends AnchorPane { final VBox eaParameters = new VBox(); eaParameters.setSpacing(2); + if (cgp.getEvolutionaryAlgorithm().getLocalParameters() != null) { - refreshParameters(cgp.getEvolutionaryAlgorithm().getLocalParameters().entrySet().iterator(), eaParameters); + refreshParameters(cgp.getEvolutionaryAlgorithm().getLocalParameters(), eaParameters); } eaCBox.setOnAction(new EventHandler() { @@ -131,7 +131,7 @@ public class SettingsPane extends AnchorPane { public void handle(ActionEvent event) { cgp.setEvolutionaryAlgorithm(eaCBox.getSelectionModel().getSelectedIndex()); if (eaCBox.getSelectionModel().getSelectedItem().getLocalParameters() != null) { - refreshParameters(eaCBox.getSelectionModel().getSelectedItem().getLocalParameters().entrySet().iterator(), eaParameters); + refreshParameters(eaCBox.getSelectionModel().getSelectedItem().getLocalParameters(), eaParameters); } } }); @@ -156,7 +156,7 @@ public class SettingsPane extends AnchorPane { final VBox mutatorParameters = new VBox(); mutatorParameters.setSpacing(2); if (cgp.getEvolutionaryAlgorithm().getLocalParameters() != null) { - refreshParameters(cgp.getMutator().getLocalParameters().entrySet().iterator(), mutatorParameters); + refreshParameters(cgp.getMutator().getLocalParameters(), mutatorParameters); } mutatorCBox.setOnAction(new EventHandler() { @@ -164,7 +164,7 @@ public class SettingsPane extends AnchorPane { public void handle(ActionEvent event) { cgp.setEvolutionaryAlgorithm(mutatorCBox.getSelectionModel().getSelectedIndex()); if (mutatorCBox.getSelectionModel().getSelectedItem().getLocalParameters() != null) { - refreshParameters(mutatorCBox.getSelectionModel().getSelectedItem().getLocalParameters().entrySet().iterator(), mutatorParameters); + refreshParameters(mutatorCBox.getSelectionModel().getSelectedItem().getLocalParameters(), mutatorParameters); } } }); @@ -188,7 +188,7 @@ public class SettingsPane extends AnchorPane { final VBox ffParameters = new VBox(); ffParameters.setSpacing(2); if (cgp.getFitnessFunction().getLocalParameters() != null) { - refreshParameters(cgp.getFitnessFunction().getLocalParameters().entrySet().iterator(), ffParameters); + refreshParameters(cgp.getFitnessFunction().getLocalParameters(), ffParameters); } ffCBox.setOnAction(new EventHandler() { @@ -196,7 +196,7 @@ public class SettingsPane extends AnchorPane { public void handle(ActionEvent event) { cgp.setEvolutionaryAlgorithm(ffCBox.getSelectionModel().getSelectedIndex()); if (ffCBox.getSelectionModel().getSelectedItem().getLocalParameters() != null) { - refreshParameters(ffCBox.getSelectionModel().getSelectedItem().getLocalParameters().entrySet().iterator(), ffParameters); + refreshParameters(ffCBox.getSelectionModel().getSelectedItem().getLocalParameters(), ffParameters); } } }); @@ -244,8 +244,8 @@ public class SettingsPane extends AnchorPane { @Override public void handle(ActionEvent event) { if (play.getText() == "Run") { - if (applyChanges()) { - gui.resetCGP(); + if (isResetRequired()) { + resetExperiment(); } play.setText("Pause"); } else { @@ -260,8 +260,8 @@ public class SettingsPane extends AnchorPane { step.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { - if (applyChanges()) { - gui.resetCGP(); + if (isResetRequired()) { + resetExperiment(); } gui.step(); } @@ -284,16 +284,15 @@ public class SettingsPane extends AnchorPane { * @param cgp * @param vb */ - private void refreshParameters(Iterator> it, VBox vb) { + private void refreshParameters(Parameter[] newParameters, VBox vb) { parameters.removeAll(vb.getChildren()); vb.getChildren().clear(); - while (it.hasNext()) { - Parameter parameter = ((Map.Entry) it.next()).getValue(); - GUIParameter gp = GUIParameter.create(parameter); + for (int i = 0; i < newParameters.length; i++) { + GUIParameter gp = GUIParameter.create(newParameters[i]); parameters.add(gp); vb.getChildren().add(gp); - } + } } private void refreshFunctions(final FunctionSet fs, VBox vb) { @@ -378,15 +377,22 @@ public class SettingsPane extends AnchorPane { * * @return true if the experiment needs to be reset, false otherwise. */ - private boolean applyChanges() { + private boolean isResetRequired() { boolean reset = false; - for (GUIParameter parameter : parameters) { - reset |= parameter.applyChange(); + reset |= parameter.requiresReset(); } - + System.out.println("reset: " + reset); return reset; } + private void resetExperiment() { + for (GUIParameter parameter : parameters) { + parameter.applyValue(); + } + gui.reset(); + + } + } diff --git a/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java b/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java index 8acd6dd..da564f7 100644 --- a/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java @@ -1,46 +1,71 @@ package jcgp.gui.settings.parameters; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.scene.control.CheckBox; import jcgp.backend.parameters.BooleanParameter; +import jcgp.backend.parameters.ParameterStatus; +import jcgp.gui.GUI; public class GUIBooleanParameter extends GUIParameter { - GUIBooleanParameter(BooleanParameter parameter) { + private boolean originalValue; + + GUIBooleanParameter(final BooleanParameter parameter) { super(); this.parameter = parameter; - value = new CheckBox(parameter.getName()); - ((CheckBox) value).setSelected(parameter.get()); + originalValue = parameter.get(); + + valueControl = new CheckBox(parameter.getName()); + ((CheckBox) valueControl).setSelected(parameter.get()); - value.setDisable(parameter.isMonitor()); + valueControl.setDisable(parameter.isMonitor()); if (parameter.isMonitor()) { makeLightBinding(); - System.out.println("light binding made for " + name); + } else { + ((CheckBox) valueControl).selectedProperty().addListener(new ChangeListener() { + @Override + public void changed( + ObservableValue observable, + Boolean oldValue, Boolean newValue) { + parameter.set(newValue); + if (parameter.getStatus() == ParameterStatus.INVALID) { + valueControl.setStyle(GUI.INVALID_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else if (parameter.getStatus() == ParameterStatus.WARNING) { + valueControl.setStyle(GUI.WARNING_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else { + valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); + valueControl.setTooltip(null); + } + } + }); } - getChildren().add(value); + getChildren().add(valueControl); } @Override public void refreshValue() { - if (!((CheckBox) value).selectedProperty().isBound()) { - ((CheckBox) value).setSelected(((BooleanParameter) parameter).get()); + if (!((CheckBox) valueControl).selectedProperty().isBound()) { + ((CheckBox) valueControl).setSelected(((BooleanParameter) parameter).get()); } - } - + @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; + public boolean requiresReset() { + return parameter.isCritical() && ((BooleanParameter) parameter).get() != originalValue; + } + + @Override + public void applyValue() { + originalValue = ((BooleanParameter) parameter).get(); } } diff --git a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java index 4b74c6e..a1b4526 100644 --- a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java @@ -1,51 +1,104 @@ 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.parameters.DoubleParameter; +import jcgp.backend.parameters.ParameterStatus; import jcgp.gui.GUI; public class GUIDoubleParameter extends GUIParameter { - GUIDoubleParameter(DoubleParameter parameter) { + private double originalValue; + + GUIDoubleParameter(final DoubleParameter parameter) { super(); this.parameter = parameter; + originalValue = parameter.get(); + name = new Text(parameter.getName()); - value = new TextField(String.valueOf(parameter.get())); + valueControl = new TextField(String.valueOf(parameter.get())); + valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); - ((TextField) value).setAlignment(Pos.CENTER_RIGHT); + ((TextField) valueControl).setAlignment(Pos.CENTER_RIGHT); - setHgrow(value, Priority.ALWAYS); + setHgrow(valueControl, Priority.ALWAYS); name.setWrappingWidth(GUI.WRAP_WIDTH); - ((TextField) value).setEditable(!parameter.isMonitor()); + ((TextField) valueControl).setEditable(!parameter.isMonitor()); + // bind if monitor, else set changelistener if (parameter.isMonitor()) { makeLightBinding(); + } else { + valueControl.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler() { + 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)); + if (parameter.getStatus() == ParameterStatus.INVALID) { + valueControl.setStyle(GUI.INVALID_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else if (parameter.getStatus() == ParameterStatus.WARNING) { + valueControl.setStyle(GUI.WARNING_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else { + valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); + valueControl.setTooltip(null); + } + } + + } + }); + 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, value); + + getChildren().addAll(name, valueControl); } @Override public void refreshValue() { - ((TextField) value).setText(String.valueOf(((DoubleParameter) parameter).get())); + ((TextField) valueControl).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; - } + public boolean requiresReset() { + return parameter.isCritical() && ((DoubleParameter) parameter).get() != originalValue; + } + + @Override + public void applyValue() { + originalValue = ((DoubleParameter) parameter).get(); + } + } diff --git a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java index 70b78a6..bf1a61e 100644 --- a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java @@ -1,49 +1,103 @@ 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.parameters.IntegerParameter; +import jcgp.backend.parameters.ParameterStatus; import jcgp.gui.GUI; public class GUIIntegerParameter extends GUIParameter { - GUIIntegerParameter(IntegerParameter parameter) { + private int originalValue; + + GUIIntegerParameter(final IntegerParameter parameter) { super(); this.parameter = parameter; + originalValue = parameter.get(); + name = new Text(parameter.getName()); - value = new TextField(String.valueOf(parameter.get())); + valueControl = new TextField(String.valueOf(parameter.get())); + valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); - ((TextField) value).setAlignment(Pos.CENTER_RIGHT); + ((TextField) valueControl).setAlignment(Pos.CENTER_RIGHT); - setHgrow(value, Priority.ALWAYS); + setHgrow(valueControl, Priority.ALWAYS); name.setWrappingWidth(GUI.WRAP_WIDTH); - ((TextField) value).setEditable(!parameter.isMonitor()); - + ((TextField) valueControl).setEditable(!parameter.isMonitor()); + + // bind if monitor, else set listeners if (parameter.isMonitor()) { makeLightBinding(); + } else { + valueControl.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler() { + public void handle( KeyEvent t ) { + char ch = t.getCharacter().toCharArray()[t.getCharacter().toCharArray().length - 1]; + if (!(ch >= '0' && ch <= '9')) { + t.consume(); + } + } + }); + ((TextField) valueControl).textProperty().addListener(new ChangeListener() { + @Override + public void changed( + ObservableValue observable, + String oldValue, String newValue) { + if (!newValue.isEmpty()) { + parameter.set(Integer.parseInt(newValue)); + if (parameter.getStatus() == ParameterStatus.INVALID) { + valueControl.setStyle(GUI.INVALID_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else if (parameter.getStatus() == ParameterStatus.WARNING) { + valueControl.setStyle(GUI.WARNING_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else { + valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); + valueControl.setTooltip(null); + } + } + + } + }); + 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, value); + getChildren().addAll(name, valueControl); } @Override public void refreshValue() { - ((TextField) value).setText(String.valueOf(((IntegerParameter) parameter).get())); + ((TextField) valueControl).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; + public boolean requiresReset() { + return parameter.isCritical() && ((IntegerParameter) parameter).get() != originalValue; + } + + @Override + public void applyValue() { + originalValue = ((IntegerParameter) parameter).get(); } + } diff --git a/src/jcgp/gui/settings/parameters/GUIParameter.java b/src/jcgp/gui/settings/parameters/GUIParameter.java index a783d71..9d6f07a 100644 --- a/src/jcgp/gui/settings/parameters/GUIParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIParameter.java @@ -7,6 +7,7 @@ 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.parameters.BooleanParameter; @@ -18,13 +19,19 @@ public abstract class GUIParameter extends HBox { protected Parameter parameter; protected Text name; - protected Control value; + 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() { @@ -48,6 +55,10 @@ public abstract class GUIParameter extends HBox { public abstract void refreshValue(); + public abstract boolean requiresReset(); + + public abstract void applyValue(); + public static GUIParameter create(Parameter parameter) { if (parameter instanceof IntegerParameter) { return new GUIIntegerParameter((IntegerParameter) parameter); @@ -59,12 +70,4 @@ public abstract class GUIParameter extends HBox { 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