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}.
*
* 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();
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();
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();
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);
}
}