-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathMultiConnectionPanel.java
More file actions
153 lines (130 loc) · 5.44 KB
/
MultiConnectionPanel.java
File metadata and controls
153 lines (130 loc) · 5.44 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package nodebox.client;
import nodebox.Icons;
import nodebox.node.Connection;
import nodebox.node.Node;
import nodebox.node.NodeLibrary;
import nodebox.node.Port;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import static nodebox.base.Preconditions.checkState;
public class MultiConnectionPanel extends JPanel {
private Port input;
private ConnectionListModel connectionListModel;
private JList connectionList;
private NodeBoxDocument document;
public MultiConnectionPanel(NodeBoxDocument document, Port input) {
super(new BorderLayout(5, 0));
this.document = document;
setOpaque(false);
this.input = input;
connectionListModel = new ConnectionListModel();
connectionList = new JList(connectionListModel);
//outputParameterList.setPreferredSize(new Dimension(160, 200));
PortCellRenderer portCellRenderer = new PortCellRenderer();
connectionList.setCellRenderer(portCellRenderer);
connectionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
buttonPanel.setOpaque(false);
JButton upButton = new JButton(new Icons.ArrowIcon(Icons.ArrowIcon.NORTH));
upButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
moveUp();
}
});
JButton downButton = new JButton(new Icons.ArrowIcon(Icons.ArrowIcon.SOUTH));
downButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
moveDown();
}
});
JButton removeButton = new JButton(new Icons.MinusIcon());
removeButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
removeSelected();
}
});
buttonPanel.add(upButton);
buttonPanel.add(downButton);
buttonPanel.add(removeButton);
buttonPanel.setPreferredSize(new Dimension(35, 100));
add(new JScrollPane(connectionList), BorderLayout.CENTER);
add(buttonPanel, BorderLayout.EAST);
}
public NodeBoxDocument getDocument() {
return document;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 100);
}
public java.util.List<Connection> getConnections() {
return input.getConnections();
}
private void moveDown() {
moveDelta(1);
}
private void moveUp() {
moveDelta(-1);
}
private void moveDelta(int delta) {
Connection selectedConnection = (Connection) connectionList.getSelectedValue();
if (selectedConnection == null) return;
java.util.List<Connection> connections = getConnections();
int index = connections.indexOf(selectedConnection);
checkState(index >= 0, "Selected connection %s could not be found.", selectedConnection);
getDocument().reorderConnection(selectedConnection, delta, true);
update();
connectionList.setSelectedValue(selectedConnection, true);
}
private void removeSelected() {
Connection selectedConnection = (Connection) connectionList.getSelectedValue();
if (selectedConnection == null) return;
getDocument().disconnect(selectedConnection);
int lastIndex = connectionList.getSelectedIndex();
update();
connectionList.setSelectedIndex(Math.max(0, lastIndex - 1));
}
public void update() {
connectionListModel.update();
}
private class ConnectionListModel extends AbstractListModel {
public int getSize() {
return getConnections().size();
}
public Object getElementAt(int index) {
return getConnections().get(index);
}
public void update() {
fireContentsChanged(this, 0, getSize());
}
}
private class PortCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Connection conn = (Connection) value;
String displayValue = conn.getOutputNode().getName();
return super.getListCellRendererComponent(list, displayValue, index, isSelected, cellHasFocus);
}
}
public static void main(String[] args) {
NodeLibrary library = new NodeLibrary("test");
Node mergeShapes = Node.ROOT_NODE.newInstance(library, "mergeshapes", Polygon.class);
Node poly1 = Node.ROOT_NODE.newInstance(library, "poly1", Polygon.class);
Node poly2 = Node.ROOT_NODE.newInstance(library, "poly2", Polygon.class);
Node poly3 = Node.ROOT_NODE.newInstance(library, "poly3", Polygon.class);
Node poly4 = Node.ROOT_NODE.newInstance(library, "poly4", Polygon.class);
Port shapesPort = mergeShapes.addPort("shapes", Port.Cardinality.MULTIPLE);
shapesPort.connect(poly1);
shapesPort.connect(poly2);
shapesPort.connect(poly3);
shapesPort.connect(poly4);
JDialog d = new JDialog();
d.setModal(true);
d.getContentPane().setLayout(new BorderLayout());
MultiConnectionPanel panel = new MultiConnectionPanel(null, shapesPort);
d.getContentPane().add(panel, BorderLayout.CENTER);
d.setSize(400, 400);
d.setVisible(true);
}
}