diff options
author | Eduardo Pedroni <e.pedroni91@gmail.com> | 2015-03-09 17:03:48 -0300 |
---|---|---|
committer | Eduardo Pedroni <e.pedroni91@gmail.com> | 2015-03-09 17:03:48 -0300 |
commit | d69fa8746728367646494fd8c2c18944f306c6a2 (patch) | |
tree | f063f9efc6c93a5520991f509f3a481543b73a91 /src/jcgp/gui/population/GUINode.java | |
parent | 9062115b7d15cb05552632dc5486a5cd15a45289 (diff) |
Added existing source code
Diffstat (limited to 'src/jcgp/gui/population/GUINode.java')
-rw-r--r-- | src/jcgp/gui/population/GUINode.java | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/jcgp/gui/population/GUINode.java b/src/jcgp/gui/population/GUINode.java new file mode 100644 index 0000000..1a32426 --- /dev/null +++ b/src/jcgp/gui/population/GUINode.java @@ -0,0 +1,134 @@ +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; + } + + @Override + 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); + } + } + + @Override + public GUIConnection[] getConnections() { + GUIConnection[] connections = new GUIConnection[GUI.resources.arity()]; + for (int c = 0; c < connections.length; c++) { + connections[c] = (GUIConnection) ((Gene) node.getConnection(c)).getGUIObject(); + } + return connections; + } +} |