From c7969623b44f375e30fa3f15dcd7581609276a0f Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Fri, 4 Apr 2014 17:14:44 +0100 Subject: Parameter validation refactored and fully functional. --- src/jcgp/gui/GUI.java | 126 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 49 deletions(-) (limited to 'src/jcgp/gui/GUI.java') diff --git a/src/jcgp/gui/GUI.java b/src/jcgp/gui/GUI.java index 0902b21..f7aa25d 100644 --- a/src/jcgp/gui/GUI.java +++ b/src/jcgp/gui/GUI.java @@ -53,12 +53,38 @@ public class GUI extends Application { private GUIConsole console = new GUIConsole(); private SettingsPane settings; - private Service cgpService; - private boolean evolving = false; private Object printLock = new Object(); + private Service cgpService = new Service () { + @Override + protected Task createTask() { + Task t = new Task() { + @Override + protected Void call() throws Exception { + while (!isCancelled() && !cgp.isFinished()) { + synchronized (printLock) { + Platform.runLater(consoleFlush); + cgp.nextGeneration(); + printLock.wait(); + } + } + if (cgp.isFinished()) { + Platform.runLater(new Runnable() { + @Override + public void run() { + runningMode(false); + } + }); + } + return null; + } + }; + return t; + } + }; + private Runnable consoleFlush = new Runnable() { @Override public void run() { @@ -80,27 +106,6 @@ public class GUI extends Application { 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. * @@ -167,45 +172,68 @@ public class GUI extends Application { chromosomeTabs.setDisable(value); } - public void disable(boolean value) { - chromosomeTabs.setDisable(value); - settings.disableSettings(value); - } - - public void playPause() { - if (!evolving) { - disable(true); - unlockOutputs(); - evolving = true; - cgpService.restart(); - } else { - disable(false); - updateNodeGrids(); - evolving = false; - cgpService.cancel(); - relockOutputs(); + public void runPause() { + if (!cgp.isFinished() && settings.areParametersValid()) { + if (!evolving) { + runningMode(true); + cgpService.restart(); + } else { + cgpService.cancel(); + runningMode(false); + } } } public void step() { - if (!evolving) { - Thread t = new Thread(new Task() { + if (!evolving && !cgp.isFinished() && settings.areParametersValid()) { + if (settings.isResetRequired()) { + reset(); + } + unlockOutputs(); + Task task = new Task() { @Override protected Void call() throws Exception { - unlockOutputs(); cgp.nextGeneration(); - console.flush(); - updateNodeGrids(); - relockOutputs(); + Platform.runLater(consoleFlush); return null; } - }); + }; + Thread t = new Thread(task); t.start(); + try { + t.join(); + } catch (InterruptedException e) { + // nothing + } finally { + updateNodeGrids(); + relockOutputs(); + } } } public void reset() { - cgp.reset(); - makeChromosomeTabPane(); + if (!evolving && settings.areParametersValid()) { + settings.applyParameters(); + cgp.reset(); + makeChromosomeTabPane(); + settings.revalidateParameters(); + } + } + + private void runningMode(boolean value) { + chromosomeTabs.setDisable(value); + settings.disableSettings(value); + if (value) { + unlockOutputs(); + settings.getRunButton().setText("Pause"); + if (settings.isResetRequired()) { + reset(); + } + } else { + updateNodeGrids(); + relockOutputs(); + settings.getRunButton().setText("Run"); + } + evolving = value; } } -- cgit v1.2.3