package jcgp.gui.population; import javafx.scene.paint.Paint; import javafx.scene.shape.Circle; import javafx.scene.shape.Line; import jcgp.backend.population.Gene; import jcgp.backend.population.Node; import jcgp.gui.GUI; import jcgp.gui.constants.Constants; import jcgp.gui.constants.Position; import jcgp.gui.handlers.NodeHandlers; /** * The GUI counterpart of {@link jcgp.backend.population.Node}. This is a * subclass of {@code GUIGene} which represents a chromosome node. * * @author Eduardo Pedroni */ public class GUINode extends GUIGene implements GUIMutable, GUIConnection { private Node node; private Line[] lines; private Circle[] sockets; /** * Instantiate {@code GUINode} given a {@code Node} and the lines needed * to show its connections. * * @param node the associated backend node. * @param lines the lines used to display connections. */ public GUINode(Node node, Line[] lines) { super(); // store references, associate with node this.node = node; this.lines = lines; node.setGUIObject(this); // create the output socket Circle output = new Circle(Constants.NODE_RADIUS, 0, Constants.SOCKET_RADIUS, Constants.SOCKET_PAINT); output.setStroke(Paint.valueOf("black")); // create input sockets sockets = new Circle[GUI.resources.arity()]; for (int l = 0; l < sockets.length; l++) { sockets[l] = new Circle(Constants.SOCKET_RADIUS, Constants.SOCKET_PAINT); sockets[l].setStroke(Paint.valueOf("black")); sockets[l].setId(String.valueOf(l)); // relocate them Position.placeSocket(l, sockets[l]); Position.connect(lines[l], (GUIGene) ((Gene) node.getConnection(l)).getGUIObject()); } // add elements getChildren().addAll(sockets); getChildren().add(output); // relocate node, add handlers Position.place(this); NodeHandlers.addHandlers(this); } /** * @return the {@code Node} instance associated with this object. */ public Node getNode() { return node; } /** * Associates this instance with a new node. * * @param node the new node. */ void setNode(Node node) { this.node = node; } /** * Returns one of this object's connection lines. Lines are * indexed in the same order as sockets and the connections * they represent. * * @param index the line to return. * @return the indexed line object. */ public Line getLine(int index) { return lines[index]; } /** * @return the entire {@code Line} array. */ public Line[] getLines() { return lines; } /** * Returns one of this object's connection sockets. They are * indexed in the same order as lines and the connections * they represent. * * @param index the socket to return. * @return the indexed socket object. */ public Circle getSocket(int index) { return sockets[index]; } /** * @return the entire {@code Socket} array. */ public Circle[] getSockets() { return sockets; } @Override public void setStateRecursively(GUIGeneState state) { setState(state); for (int i = 0; i < GUI.resources.arity(); i++) { ((GUIConnection) ((Gene) node.getConnection(i)).getGUIObject()).setStateRecursively(state); } } @Override protected void setLinesVisible(boolean value) { for (int i = 0; i < lines.length; i++) { lines[i].setVisible(value); } } @Override public void setLockRecursively(boolean value) { setLock(value); for (int i = 0; i < GUI.resources.arity(); i++) { ((GUIConnection) ((Gene) node.getConnection(i)).getGUIObject()).setLockRecursively(value); } } }