From a00b13c12e6fd9f91ee156d5db96e02853d2e410 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 24 Nov 2014 11:30:21 +0000 Subject: Fixed formatting, removed commented out code, added comments to handlers and locking stuff. --- src/jcgp/gui/handlers/InputHandlers.java | 18 +++++++++- src/jcgp/gui/handlers/NodeHandlers.java | 25 ++++++++++++-- src/jcgp/gui/handlers/OutputHandlers.java | 56 +++++++++++++++++++------------ 3 files changed, 75 insertions(+), 24 deletions(-) (limited to 'src/jcgp/gui/handlers') diff --git a/src/jcgp/gui/handlers/InputHandlers.java b/src/jcgp/gui/handlers/InputHandlers.java index 6be4e7e..1d18ef5 100644 --- a/src/jcgp/gui/handlers/InputHandlers.java +++ b/src/jcgp/gui/handlers/InputHandlers.java @@ -6,13 +6,21 @@ import jcgp.gui.population.GUIGene.GUIGeneState; import jcgp.gui.population.GUIInput; /** - * + * Holds the handlers that define the behaviour of {@code GUIInput}. + *

+ * The handlers are instantiated here statically and added to {@code GUIInput} + * instances using {@code InputHandlers.addHandlers(...)}. This guarantees that + * all inputs behave the same way without instantiating a new set of handlers for + * each input instance. * * @author Eduardo Pedroni * */ public final class InputHandlers { + /** + * Inputs don't do much; set state to hover when mouse enters. + */ private static EventHandler mouseEnteredHandler = new EventHandler() { @Override public void handle(MouseEvent event) { @@ -20,6 +28,9 @@ public final class InputHandlers { } }; + /** + * Inputs don't do much; set state to neutral when mouse exits. + */ private static EventHandler mouseExitedHandler = new EventHandler() { @Override public void handle(MouseEvent event) { @@ -27,6 +38,11 @@ public final class InputHandlers { } }; + /** + * Adds all handlers to the specified input. + * + * @param input the {@code GUIInput} to which the handlers will be added. + */ public static void addHandlers(GUIInput input) { input.addEventHandler(MouseEvent.MOUSE_ENTERED, mouseEnteredHandler); input.addEventHandler(MouseEvent.MOUSE_EXITED, mouseExitedHandler); diff --git a/src/jcgp/gui/handlers/NodeHandlers.java b/src/jcgp/gui/handlers/NodeHandlers.java index 8c9c465..10a334a 100644 --- a/src/jcgp/gui/handlers/NodeHandlers.java +++ b/src/jcgp/gui/handlers/NodeHandlers.java @@ -8,21 +8,38 @@ import jcgp.gui.population.GUIGene; import jcgp.gui.population.GUIGene.GUIGeneState; import jcgp.gui.population.GUINode; +/** + * Holds the handlers that define the behaviour of {@code GUINode}. + *

+ * The handlers are instantiated here statically and added to {@code GUINode} + * instances using {@code NodeHandlers.addHandlers(...)}. This guarantees that + * all nodes behave the same way without instantiating a new set of handlers for + * each node instance. + * + * @author Eduardo Pedroni + * + */ public final class NodeHandlers { + /** + * Set the node to {@code GUIGeneState.HOVER} state, and set its immediate connections to {@code GUIGeneState.EXTENDED_HOVER}. + */ private static EventHandler mouseEnteredHandler = new EventHandler() { @Override public void handle(MouseEvent event) { // acquire the source, we can safely cast it to GUINode GUINode source = (GUINode) event.getSource(); + source.setState(GUIGeneState.HOVER); - for (int i = 0; i < GUI.resources.arity(); i++) { ((GUIGene) ((Gene) source.getNode().getConnection(i)).getGUIObject()).setState(GUIGeneState.EXTENDED_HOVER); } } }; + /** + * Set the node and its immediate connections to {@code GUIGeneState.NEUTRAL} state. + */ private static EventHandler mouseExitedHandler = new EventHandler() { @Override public void handle(MouseEvent event) { @@ -30,13 +47,17 @@ public final class NodeHandlers { GUINode source = (GUINode) event.getSource(); source.setState(GUIGeneState.NEUTRAL); - for (int i = 0; i < GUI.resources.arity(); i++) { ((GUIGene) ((Gene) source.getNode().getConnection(i)).getGUIObject()).setState(GUIGeneState.NEUTRAL); } } }; + /** + * Adds all handlers to the specified node. + * + * @param node the {@code GUINode} to which the handlers will be added. + */ public static void addHandlers(GUINode node) { node.addEventHandler(MouseEvent.MOUSE_ENTERED, mouseEnteredHandler); node.addEventHandler(MouseEvent.MOUSE_EXITED, mouseExitedHandler); diff --git a/src/jcgp/gui/handlers/OutputHandlers.java b/src/jcgp/gui/handlers/OutputHandlers.java index 7399e50..f72e430 100644 --- a/src/jcgp/gui/handlers/OutputHandlers.java +++ b/src/jcgp/gui/handlers/OutputHandlers.java @@ -7,55 +7,69 @@ import jcgp.gui.population.GUIConnection; import jcgp.gui.population.GUIGene.GUIGeneState; import jcgp.gui.population.GUIOutput; +/** + * Holds the handlers that define the behaviour of {@code GUIOutput}. + *

+ * The handlers are instantiated here statically and added to {@code GUIOutput} + * instances using {@code OutputHandlers.addHandlers(...)}. This guarantees that + * all outputs behave the same way without instantiating a new set of handlers for + * each output instance. + * + * @author Eduardo Pedroni + * + */ public final class OutputHandlers { + /** + * Set the output to {@code GUIGeneState.HOVER} state, and recursively set its active genes + * to {@code GUIGeneState.ACTIVE_HOVER}. + */ private static EventHandler mouseEnteredHandler = new EventHandler() { @Override public void handle(MouseEvent event) { // acquire the source, we can safely cast it to GUIOutput GUIOutput source = (GUIOutput) event.getSource(); - //if (!source.isLocked()) { - source.setState(GUIGeneState.HOVER); - ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setStateRecursively(GUIGeneState.ACTIVE_HOVER); - //} + source.setState(GUIGeneState.HOVER); + ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setStateRecursively(GUIGeneState.ACTIVE_HOVER); } }; + /** + * Set the output and all of its active genes to {@code GUIGeneState.NEUTRAL} state. + */ private static EventHandler mouseExitedHandler = new EventHandler() { @Override public void handle(MouseEvent event) { // acquire the source, we can safely cast it to GUIOutput GUIOutput source = (GUIOutput) event.getSource(); - - //if (!source.isLocked()) { - source.setState(GUIGeneState.NEUTRAL); - ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setStateRecursively(GUIGeneState.NEUTRAL); - //} - + + source.setState(GUIGeneState.NEUTRAL); + ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setStateRecursively(GUIGeneState.NEUTRAL); } }; + /** + * If the output is locked, unlock it and all of its associated genes recursively. If it is unlocked, + * lock it and its active genes. + */ private static EventHandler mouseClickHandler = new EventHandler() { @Override public void handle(MouseEvent event) { // acquire the source, we can safely cast it to GUIOutput GUIOutput source = (GUIOutput) event.getSource(); - if (source.isLocked()) { - source.setLock(false); - ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setLockRecursively(false); - } else { - source.setLock(true); - ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setLockRecursively(true); - } - -// source.setState(GUIGeneState.HOVER); -// ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setStateRecursively(GUIGeneState.ACTIVE_HOVER); - + boolean lock = !source.isLocked(); + source.setLock(lock); + ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setLockRecursively(lock); } }; + /** + * Adds all handlers to the specified output. + * + * @param output the {@code GUIOutput} to which the handlers will be added. + */ public static void addHandlers(GUIOutput output) { output.addEventHandler(MouseEvent.MOUSE_ENTERED, mouseEnteredHandler); output.addEventHandler(MouseEvent.MOUSE_EXITED, mouseExitedHandler); -- cgit v1.2.3