aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/handlers/OutputHandlers.java
blob: b89d746528d9d368f00a88b49c013c02187ce34d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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);
	}

}