aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/handlers/OutputHandlers.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/gui/handlers/OutputHandlers.java')
-rw-r--r--src/jcgp/gui/handlers/OutputHandlers.java84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/jcgp/gui/handlers/OutputHandlers.java b/src/jcgp/gui/handlers/OutputHandlers.java
new file mode 100644
index 0000000..b89d746
--- /dev/null
+++ b/src/jcgp/gui/handlers/OutputHandlers.java
@@ -0,0 +1,84 @@
+package jcgp.gui.handlers;
+
+import javafx.event.EventHandler;
+import javafx.scene.input.MouseEvent;
+import jcgp.backend.population.Gene;
+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}.
+ * <br><br>
+ * 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 {
+
+ /**
+ * Private constructor to prevent instantiation.
+ */
+ private OutputHandlers() {}
+
+ /**
+ * Set the output to {@code GUIGeneState.HOVER} state, and recursively set its active genes
+ * to {@code GUIGeneState.ACTIVE_HOVER}.
+ */
+ private static EventHandler<MouseEvent> mouseEnteredHandler = new EventHandler<MouseEvent>() {
+ @Override
+ public void handle(MouseEvent event) {
+ // acquire the source, we can safely cast it to GUIOutput
+ GUIOutput source = (GUIOutput) event.getSource();
+
+ 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<MouseEvent> mouseExitedHandler = new EventHandler<MouseEvent>() {
+ @Override
+ public void handle(MouseEvent event) {
+ // acquire the source, we can safely cast it to GUIOutput
+ GUIOutput source = (GUIOutput) event.getSource();
+
+ 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<MouseEvent> mouseClickHandler = new EventHandler<MouseEvent>() {
+ @Override
+ public void handle(MouseEvent event) {
+ // acquire the source, we can safely cast it to GUIOutput
+ GUIOutput source = (GUIOutput) event.getSource();
+
+ 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);
+ output.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseClickHandler);
+ }
+
+}