aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/population/GUINode.java
blob: 1a3242671a94d5c24862d4eb9f6cbb8231b0f070 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
	}
}