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. --- src/jcgp/gui/ChromosomePane.java | 28 ++- src/jcgp/gui/GUI.java | 211 +++++++++++++++++++++ src/jcgp/gui/Messenger.java | 7 - src/jcgp/gui/console/Console.java | 11 ++ src/jcgp/gui/console/GUIConsole.java | 22 ++- src/jcgp/gui/population/GUIGene.java | 6 +- src/jcgp/gui/population/GUIInput.java | 10 +- src/jcgp/gui/population/GUINode.java | 10 +- src/jcgp/gui/population/GUIOutput.java | 27 ++- src/jcgp/gui/settings/SettingsPane.java | 116 ++++++----- .../settings/parameters/GUIBooleanParameter.java | 34 ++-- .../settings/parameters/GUIDoubleParameter.java | 39 ++-- .../settings/parameters/GUIIntegerParameter.java | 36 ++-- src/jcgp/gui/settings/parameters/GUIParameter.java | 56 +++++- 14 files changed, 457 insertions(+), 156 deletions(-) create mode 100644 src/jcgp/gui/GUI.java delete mode 100644 src/jcgp/gui/Messenger.java create mode 100644 src/jcgp/gui/console/Console.java (limited to 'src/jcgp/gui') diff --git a/src/jcgp/gui/ChromosomePane.java b/src/jcgp/gui/ChromosomePane.java index b4a628f..b1f4bf0 100644 --- a/src/jcgp/gui/ChromosomePane.java +++ b/src/jcgp/gui/ChromosomePane.java @@ -6,14 +6,14 @@ import javafx.scene.control.ScrollPane; import javafx.scene.layout.Pane; import javafx.scene.shape.Line; import jcgp.JCGP.Resources; +import jcgp.backend.population.Chromosome; +import jcgp.backend.population.Connection; +import jcgp.backend.population.Input; +import jcgp.backend.population.Node; import jcgp.gui.population.GUIGene; import jcgp.gui.population.GUIInput; import jcgp.gui.population.GUINode; import jcgp.gui.population.GUIOutput; -import jcgp.population.Chromosome; -import jcgp.population.Connection; -import jcgp.population.Input; -import jcgp.population.Node; public class ChromosomePane extends ScrollPane { @@ -26,14 +26,14 @@ public class ChromosomePane extends ScrollPane { private ArrayList connectionLines; + private ArrayList relock = new ArrayList(); + private boolean target = false; public ChromosomePane(Chromosome chromosome, Resources resources) { super(); connectionLines = new ArrayList(); - //setMouseTransparent(true); - content = new Pane(); content.setId("content pane for genes"); @@ -119,4 +119,20 @@ public class ChromosomePane extends ScrollPane { guiOutputs[i].updateLines(); } } + + public void unlockOutputs() { + relock.clear(); + for (int i = 0; i < guiOutputs.length; i++) { + if (guiOutputs[i].isLocked()) { + guiOutputs[i].unlock(); + relock.add(guiOutputs[i]); + } + } + } + + public void relockOutputs() { + for (int i = 0; i < relock.size(); i++) { + relock.get(i).lock(); + } + } } diff --git a/src/jcgp/gui/GUI.java b/src/jcgp/gui/GUI.java new file mode 100644 index 0000000..7e7f385 --- /dev/null +++ b/src/jcgp/gui/GUI.java @@ -0,0 +1,211 @@ +package jcgp.gui; + +import javafx.application.Application; +import javafx.application.Platform; +import javafx.concurrent.Service; +import javafx.concurrent.Task; +import javafx.scene.Scene; +import javafx.scene.control.Tab; +import javafx.scene.control.TabPane; +import javafx.scene.control.TabPane.TabClosingPolicy; +import javafx.scene.layout.BorderPane; +import javafx.stage.Stage; +import jcgp.JCGP; +import jcgp.JCGP.Resources; +import jcgp.gui.console.GUIConsole; +import jcgp.gui.settings.SettingsPane; + +public class GUI extends Application { + + /* Colours */ + /** NEUTRAL_COLOUR is the colour elements should be when no interaction is occurring + * (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"; + + + /* Sizes and distances */ + public static final double RESIZE_MARGIN = 5.0; + + public static final double SETTINGS_WIDTH = 200; + public static final double CONSOLE_HEIGHT = 100; + + public static final double WRAP_WIDTH = 90; + + private static JCGP cgp; + public static Resources resources; + + private BorderPane leftPane; + private BorderPane window; + + private ChromosomePane[] chromosomes; + private TabPane chromosomeTabs; + + private GUIConsole console = new GUIConsole(); + private SettingsPane settings; + + private Service cgpService; + + private boolean evolving = false; + + private Object printLock = new Object(); + + private Runnable consoleFlush = new Runnable() { + @Override + public void run() { + synchronized(printLock) { + console.flush(); + printLock.notifyAll(); + } + } + }; + + public static void main(String[] args) { + cgp = new JCGP(); + resources = cgp.getResources(); + + launch(); + } + + @Override + public void start(Stage primaryStage) throws Exception { + resources.setConsole(console); + + cgpService = new Service () { + @Override + protected Task createTask() { + + Task t = new Task() { + @Override + protected Void call() throws Exception { + while (!isCancelled()) { + Platform.runLater(consoleFlush); + synchronized (printLock) { + cgp.nextGeneration(); + printLock.wait(); + } + } + return null; + } + }; + return t; + } + }; + + /* + * Instantiate the various GUI elements here. + * + * + */ + + leftPane = new BorderPane(); + + chromosomeTabs = new TabPane(); + chromosomeTabs.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE); + makeChromosomeTabPane(); + + settings = new SettingsPane(cgp, this); + + leftPane.setCenter(chromosomeTabs); + leftPane.setBottom(console); + + window = new BorderPane(); + + window.setCenter(leftPane); + window.setRight(settings); + + primaryStage.setTitle("JCGP"); + + primaryStage.setScene(new Scene(window)); + primaryStage.show(); + } + + /** + * + */ + private void makeChromosomeTabPane() { + chromosomeTabs.getTabs().clear(); + + chromosomes = new ChromosomePane[cgp.getResources().getInt("popSize")]; + Tab tab; + for (int i = 0; i < chromosomes.length; i++) { + chromosomes[i] = new ChromosomePane(cgp.getPopulation().getChromosome(i), resources); + tab = new Tab("Chr " + i); + tab.setContent(chromosomes[i]); + chromosomeTabs.getTabs().add(tab); + } + } + + private void updateNodeGrids() { + for (int i = 0; i < chromosomes.length; i++) { + chromosomes[i].update(); + } + } + + private void unlockOutputs() { + for (int i = 0; i < chromosomes.length; i++) { + chromosomes[i].unlockOutputs(); + } + } + + private void relockOutputs() { + for (int i = 0; i < chromosomes.length; i++) { + chromosomes[i].relockOutputs(); + } + } + + public void disableChromosomePanes(boolean value) { + chromosomeTabs.setDisable(value); + } + + public void playPause() { + if (!evolving) { + settings.disableSettings(true); + disableChromosomePanes(true); + unlockOutputs(); + evolving = true; + cgpService.restart(); + } else { + settings.disableSettings(false); + disableChromosomePanes(false); + updateNodeGrids(); + evolving = false; + cgpService.cancel(); + relockOutputs(); + } + } + + public void step() { + if (!evolving) { + Thread t = new Thread(new Task() { + @Override + protected Void call() throws Exception { + unlockOutputs(); + cgp.nextGeneration(); + console.flush(); + updateNodeGrids(); + relockOutputs(); + return null; + } + }); + t.start(); + } + } + + public void resetCGP() { + cgp.remakePopulation(); + makeChromosomeTabPane(); + + resources.set("currentGen", 1); + resources.set("currentRun", 1); + } +} diff --git a/src/jcgp/gui/Messenger.java b/src/jcgp/gui/Messenger.java deleted file mode 100644 index becf47c..0000000 --- a/src/jcgp/gui/Messenger.java +++ /dev/null @@ -1,7 +0,0 @@ -package jcgp.gui; - -public class Messenger { - - - -} diff --git a/src/jcgp/gui/console/Console.java b/src/jcgp/gui/console/Console.java new file mode 100644 index 0000000..63c7f5b --- /dev/null +++ b/src/jcgp/gui/console/Console.java @@ -0,0 +1,11 @@ +package jcgp.gui.console; + +public interface Console { + + public void println(String s); + + public void print(String s); + + public void flush(); + +} diff --git a/src/jcgp/gui/console/GUIConsole.java b/src/jcgp/gui/console/GUIConsole.java index 0ef2be3..8fdb1a5 100644 --- a/src/jcgp/gui/console/GUIConsole.java +++ b/src/jcgp/gui/console/GUIConsole.java @@ -5,13 +5,15 @@ import javafx.scene.Cursor; import javafx.scene.control.TextArea; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; -import jcgp.GUI; +import jcgp.gui.GUI; -public class GUIConsole extends AnchorPane { +public class GUIConsole extends AnchorPane implements Console { private boolean dragging; private TextArea textArea = new TextArea("Welcome to JCGP!\n"); + private StringBuffer printBuffer = new StringBuffer(); + public GUIConsole() { super(); textArea.setEditable(false); @@ -87,8 +89,20 @@ public class GUIConsole extends AnchorPane { return event.getY() < (GUI.RESIZE_MARGIN); } - public TextArea getTextArea() { - return textArea; + @Override + public void println(String s) { + printBuffer.append(s + "\n"); + } + + @Override + public void print(String s) { + printBuffer.append(s); + } + + @Override + public void flush() { + textArea.appendText(printBuffer.toString()); + printBuffer = new StringBuffer(); } } diff --git a/src/jcgp/gui/population/GUIGene.java b/src/jcgp/gui/population/GUIGene.java index a37eb19..26be2a5 100644 --- a/src/jcgp/gui/population/GUIGene.java +++ b/src/jcgp/gui/population/GUIGene.java @@ -4,9 +4,9 @@ import javafx.beans.property.SimpleObjectProperty; import javafx.scene.Group; import javafx.scene.shape.Circle; import javafx.scene.text.Text; +import jcgp.backend.population.Connection; +import jcgp.backend.population.Gene; import jcgp.gui.ChromosomePane; -import jcgp.population.Connection; -import jcgp.population.Gene; enum GUIGeneState { NEUTRAL, @@ -59,7 +59,7 @@ public abstract class GUIGene extends Group { return locked; } - public abstract void setLocked(boolean value); + abstract void setLocked(boolean value); public abstract void addLocks(int value); diff --git a/src/jcgp/gui/population/GUIInput.java b/src/jcgp/gui/population/GUIInput.java index 30ce4ac..f47186c 100644 --- a/src/jcgp/gui/population/GUIInput.java +++ b/src/jcgp/gui/population/GUIInput.java @@ -11,11 +11,11 @@ import javafx.scene.shape.Circle; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.scene.text.TextAlignment; -import jcgp.GUI; +import jcgp.backend.population.Connection; +import jcgp.backend.population.Input; +import jcgp.backend.population.Output; import jcgp.gui.ChromosomePane; -import jcgp.population.Connection; -import jcgp.population.Input; -import jcgp.population.Output; +import jcgp.gui.GUI; public class GUIInput extends GUIGene { @@ -212,7 +212,7 @@ public class GUIInput extends GUIGene { } @Override - public void setLocked(boolean value) { + void setLocked(boolean value) { locked += value ? 1 : -1; stateProperty.set(locked > 0 ? GUIGeneState.HOVER : GUIGeneState.ACTIVE_HOVER); } diff --git a/src/jcgp/gui/population/GUINode.java b/src/jcgp/gui/population/GUINode.java index 6fc84da..450647f 100644 --- a/src/jcgp/gui/population/GUINode.java +++ b/src/jcgp/gui/population/GUINode.java @@ -13,11 +13,11 @@ import javafx.scene.shape.Line; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.scene.text.TextAlignment; -import jcgp.GUI; +import jcgp.backend.population.Connection; +import jcgp.backend.population.Input; +import jcgp.backend.population.Node; import jcgp.gui.ChromosomePane; -import jcgp.population.Connection; -import jcgp.population.Input; -import jcgp.population.Node; +import jcgp.gui.GUI; public class GUINode extends GUIGene { @@ -429,7 +429,7 @@ public class GUINode extends GUIGene { } @Override - public void setLocked(boolean value) { + void setLocked(boolean value) { locked += value ? 1 : -1; stateProperty.set(locked > 0 ? GUIGeneState.HOVER : GUIGeneState.ACTIVE_HOVER); diff --git a/src/jcgp/gui/population/GUIOutput.java b/src/jcgp/gui/population/GUIOutput.java index 16d95f0..9c5dfa5 100644 --- a/src/jcgp/gui/population/GUIOutput.java +++ b/src/jcgp/gui/population/GUIOutput.java @@ -13,12 +13,12 @@ import javafx.scene.shape.Line; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.scene.text.TextAlignment; -import jcgp.GUI; +import jcgp.backend.population.Connection; +import jcgp.backend.population.Input; +import jcgp.backend.population.Node; +import jcgp.backend.population.Output; import jcgp.gui.ChromosomePane; -import jcgp.population.Connection; -import jcgp.population.Input; -import jcgp.population.Node; -import jcgp.population.Output; +import jcgp.gui.GUI; public class GUIOutput extends GUIGene { @@ -281,7 +281,7 @@ public class GUIOutput extends GUIGene { } @Override - public void setLocked(boolean value) { + void setLocked(boolean value) { locked += value ? 1 : -1; setConnections(value ? GUIGeneState.HOVER : GUIGeneState.ACTIVE_HOVER); @@ -314,5 +314,20 @@ public class GUIOutput extends GUIGene { sourceLine.setEndX(gene.getLayoutX() + NODE_RADIUS); sourceLine.setEndY(gene.getLayoutY()); } + + public void unlock() { + if (isLocked()) { + setLocked(false); + stateProperty.set(GUIGeneState.NEUTRAL); + setConnections(GUIGeneState.NEUTRAL); + } + } + + public void lock() { + if (!isLocked()) { + stateProperty.set(GUIGeneState.HOVER); + setLocked(true); + } + } } diff --git a/src/jcgp/gui/settings/SettingsPane.java b/src/jcgp/gui/settings/SettingsPane.java index 9edcbdb..61011aa 100644 --- a/src/jcgp/gui/settings/SettingsPane.java +++ b/src/jcgp/gui/settings/SettingsPane.java @@ -5,7 +5,6 @@ import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; -import javafx.beans.property.SimpleIntegerProperty; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; @@ -22,14 +21,15 @@ import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.text.FontSmoothingType; import javafx.scene.text.Text; -import jcgp.GUI; import jcgp.JCGP; -import jcgp.function.FunctionSet; +import jcgp.backend.function.FunctionSet; +import jcgp.backend.modules.ea.EvolutionaryAlgorithm; +import jcgp.backend.modules.fitness.FitnessFunction; +import jcgp.backend.modules.mutator.Mutator; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.backend.parameters.Parameter; +import jcgp.gui.GUI; import jcgp.gui.settings.parameters.*; -import jcgp.modules.ea.EvolutionaryAlgorithm; -import jcgp.modules.fitness.FitnessFunction; -import jcgp.modules.mutator.Mutator; -import jcgp.parameters.*; public class SettingsPane extends AnchorPane { @@ -84,31 +84,28 @@ public class SettingsPane extends AnchorPane { bpPane.getChildren().add(header); - final GUIIntegerParameter generation = new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("currentGen")); - final GUIIntegerParameter run = new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("currentRun")); + GUIParameter rows = GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("rows")); + parameters.add(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"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("levelsBack"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("rows"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("columns"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("inputs"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("outputs"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("levelsBack"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("popSize"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("generations"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("runs"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("popSize"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("generations"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("runs"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("currentGen"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("currentRun"))); - parameters.add(generation); - parameters.add(run); - - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("seed"))); - parameters.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("report"))); - parameters.add(new GUIBooleanParameter((BooleanParameter) cgp.getResources().getParameter("verbose"))); + 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); - generation.getTextField().textProperty().bind(((SimpleIntegerProperty) cgp.getResources().getParameter("currentGen").valueProperty()).asString()); + } private void initialiseEAParameters(final JCGP cgp) { @@ -245,13 +242,16 @@ public class SettingsPane extends AnchorPane { play.setOnAction(new EventHandler() { @Override - public void handle(ActionEvent event) { - gui.playPause(); - if (gui.isEvolving()) { + public void handle(ActionEvent event) { + if (play.getText() == "Run") { + if (applyChanges()) { + gui.resetCGP(); + } play.setText("Pause"); } else { play.setText("Run"); } + gui.playPause(); } }); @@ -260,6 +260,9 @@ public class SettingsPane extends AnchorPane { step.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { + if (applyChanges()) { + gui.resetCGP(); + } gui.step(); } }); @@ -274,8 +277,7 @@ public class SettingsPane extends AnchorPane { controls.getChildren().add(flowButtons); - mainContainer.getChildren().add(controls); - + mainContainer.getChildren().add(controls); } /** @@ -287,19 +289,10 @@ public class SettingsPane extends AnchorPane { vb.getChildren().clear(); while (it.hasNext()) { Parameter parameter = ((Map.Entry) it.next()).getValue(); - if (parameter instanceof IntegerParameter) { - GUIIntegerParameter gip = new GUIIntegerParameter((IntegerParameter) parameter); - parameters.add(gip); - vb.getChildren().add(gip); - } else if (parameter instanceof DoubleParameter) { - GUIDoubleParameter gdp = new GUIDoubleParameter((DoubleParameter) parameter); - parameters.add(gdp); - vb.getChildren().add(gdp); - } else if (parameter instanceof BooleanParameter) { - GUIBooleanParameter gbp = new GUIBooleanParameter((BooleanParameter) parameter); - parameters.add(gbp); - vb.getChildren().add(gbp); - } + GUIParameter gp = GUIParameter.create(parameter); + + parameters.add(gp); + vb.getChildren().add(gp); } } @@ -371,30 +364,29 @@ public class SettingsPane extends AnchorPane { return event.getX() < (GUI.RESIZE_MARGIN); } - public void refreshValues() { - for (GUIParameter parameter : parameters) { - parameter.refreshValue(); - } - //parent.updateNodeGrids(); - } - -// public void bindParameters() { -// for (GUIParameter parameter : parameters) { -// parameter.doBindings(); -// } -// } -// -// public void unbindParameters() { -// for (GUIParameter parameter : parameters) { -// parameter.undoBindings(); -// } -// } - - public void toggleSettings(boolean value) { + public void disableSettings(boolean value) { bpPane.setDisable(value); eaPane.setDisable(value); mutatorPane.setDisable(value); ffPane.setDisable(value); nfPane.setDisable(value); } + + /** + * 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. + */ + private boolean applyChanges() { + boolean reset = false; + + for (GUIParameter parameter : parameters) { + reset |= parameter.applyChange(); + } + + return reset; + } + + } 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