/
MachineSetupPanel.java
387 lines (336 loc) · 14.3 KB
/
MachineSetupPanel.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (C) 2011 Jason von Nieda <jason@vonnieda.org>
*
* This file is part of OpenPnP.
*
* OpenPnP is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenPnP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with OpenPnP. If not, see
* <http://www.gnu.org/licenses/>.
*
* For more information about OpenPnP visit http://openpnp.org
*/
package org.openpnp.gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.openpnp.ConfigurationListener;
import org.openpnp.Translations;
import org.openpnp.gui.support.Wizard;
import org.openpnp.gui.support.WizardContainer;
import org.openpnp.model.Configuration;
import org.openpnp.spi.PropertySheetHolder;
import org.openpnp.spi.PropertySheetHolder.PropertySheet;
import org.openpnp.util.BeanUtils;
@SuppressWarnings("serial")
public class MachineSetupPanel extends JPanel implements WizardContainer {
private static final String PREF_DIVIDER_POSITION = "MachineSetupPanel.dividerPosition";
private static final int PREF_DIVIDER_POSITION_DEF = -1;
private JTextField searchTextField;
private Preferences prefs = Preferences.userNodeForPackage(MachineSetupPanel.class);
private JTree tree;
private DefaultTreeModel treeModel;
private JTabbedPane tabbedPane;
private JToolBar toolBar;
private final Action action = new SwingAction();
private JCheckBox cbExp;
/**
* These three variables are used to manage state so that if the user is flipping between nodes
* of the same type we select the same tab that was previously selected. The idea is that if
* you are looking at the Part Detection settings for a Nozzle Tip, and you switch to another
* Nozzle Tip you probably want to look at the Part Detection settings for it, and not
* whatever the first tab is.
*/
private boolean disableLastSelectedListener = false;
private Object lastSelectedNode = null;
private HashMap<Class, Integer> lastSelectedTabIndex = new HashMap<>();
public MachineSetupPanel() {
setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
add(panel, BorderLayout.NORTH);
panel.setLayout(new BorderLayout(0, 0));
toolBar = new JToolBar();
toolBar.setFloatable(false);
panel.add(toolBar, BorderLayout.CENTER);
JPanel panel_1 = new JPanel();
panel.add(panel_1, BorderLayout.EAST);
cbExp = new JCheckBox(Translations.getString("MachineSetupPanel.ExpandChkBox.text")); //$NON-NLS-1$
panel_1.add(cbExp);
cbExp.setAction(action);
JLabel lblSearch = new JLabel(Translations.getString("MachineSetupPanel.SearchLabel.text")); //$NON-NLS-1$
panel_1.add(lblSearch);
searchTextField = new JTextField();
searchTextField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
search();
}
@Override
public void insertUpdate(DocumentEvent e) {
search();
}
@Override
public void changedUpdate(DocumentEvent e) {
search();
}
});
panel_1.add(searchTextField);
searchTextField.setColumns(15);
final JSplitPane splitPane = new JSplitPane();
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane.setContinuousLayout(true);
splitPane
.setDividerLocation(prefs.getInt(PREF_DIVIDER_POSITION, PREF_DIVIDER_POSITION_DEF));
splitPane.addPropertyChangeListener("dividerLocation", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
prefs.putInt(PREF_DIVIDER_POSITION, splitPane.getDividerLocation());
}
});
add(splitPane, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane();
splitPane.setLeftComponent(scrollPane);
tree = new JTree();
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setCellRenderer(treeCellRenderer);
scrollPane.setViewportView(tree);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
splitPane.setRightComponent(tabbedPane);
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (lastSelectedNode != null) {
if (disableLastSelectedListener) {
return;
}
lastSelectedTabIndex.put(lastSelectedNode.getClass(), tabbedPane.getSelectedIndex());
}
}
});
tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
selectCurrentTreePath();
}
});
Configuration.get().addListener(new ConfigurationListener() {
@Override
public void configurationLoaded(Configuration configuration) throws Exception {}
@Override
public void configurationComplete(Configuration configuration) throws Exception {
tree.setModel(treeModel = new DefaultTreeModel(
new PropertySheetHolderTreeNode(Configuration.get().getMachine(), null)));
for (int i = 1; i < tree.getRowCount(); i++) {
if (cbExp.isSelected()) {
tree.expandRow(i);
}
else {
tree.collapseRow(i);
}
}
}
});
}
private void search() {}
@Override
public void wizardCompleted(Wizard wizard) {}
@Override
public void wizardCancelled(Wizard wizard) {}
public void selectCurrentTreePath() {
disableLastSelectedListener = true;
tabbedPane.removeAll();
toolBar.removeAll();
TreePath path = tree.getSelectionPath();
if (path != null) {
List<Object> pathsReverse = Arrays.asList(path.getPath());
Collections.reverse(pathsReverse);
for (Object o : pathsReverse) {
PropertySheetHolderTreeNode node = (PropertySheetHolderTreeNode) o;
Action[] actions = node.obj.getPropertySheetHolderActions();
if (actions != null) {
for (Action action : actions) {
toolBar.add(action);
}
}
}
PropertySheetHolderTreeNode node =
(PropertySheetHolderTreeNode) path.getLastPathComponent();
if (node != null) {
PropertySheet[] propertySheets = node.obj.getPropertySheets();
if (propertySheets != null) {
for (PropertySheet propertySheet : propertySheets) {
String title = propertySheet.getPropertySheetTitle();
JPanel panel = propertySheet.getPropertySheetPanel();
if (title == null) {
title = Translations.getString("MachineSetupPanel.RightComponent.tabs.configuration.title"); //$NON-NLS-1$
}
if (panel != null) {
tabbedPane.add(title, panel);
}
}
if (node.obj != null) {
if (lastSelectedTabIndex.get(node.obj.getClass()) != null) {
// Sometimes when clicking around quickly, the lastSelectedTabIndex update seems to be wrong,
// resulting in an IndexOutOfBoundsException. Therefore we check the tab count too. This covers variable per class Wizards too.
tabbedPane.setSelectedIndex(Math.min(tabbedPane.getTabCount()-1, lastSelectedTabIndex.get(node.obj.getClass())));
}
}
lastSelectedNode = node.obj;
}
else {
lastSelectedNode = null;
}
}
}
disableLastSelectedListener = false;
revalidate();
repaint();
}
public class PropertySheetHolderTreeNode implements TreeNode {
private final PropertySheetHolder obj;
private final TreeNode parent;
private final ArrayList<PropertySheetHolderTreeNode> children = new ArrayList<>();
public PropertySheetHolderTreeNode(PropertySheetHolder obj, TreeNode parent) {
this.obj = obj;
this.parent = parent;
loadChildren();
/**
* If the object we're creating a node for supports property change then we add
* a listener. When we get an indexed changed we refresh our children and
* when we get a non-indexed change we refresh ourself.
*
* TODO: Note: Since we don't know which child got refreshed, we refresh them
* all and this causes the JTree to collapse all the other children. This sucks
* but there isn't a clean way to know which child changed without including the
* property name somewhere.
*/
BeanUtils.addPropertyChangeListener(obj, (PropertyChangeListener) (e) -> {
SwingUtilities.invokeLater(() -> {
if (e instanceof IndexedPropertyChangeEvent) {
for (PropertySheetHolderTreeNode node : children) {
node.loadChildren();
treeModel.nodeStructureChanged(node);
}
}
else {
treeModel.nodeChanged(this);
}
});
});
}
private void loadChildren() {
this.children.clear();
PropertySheetHolder[] children = obj.getChildPropertySheetHolders();
if (children != null) {
for (PropertySheetHolder child : children) {
this.children.add(new PropertySheetHolderTreeNode(child, this));
}
}
}
public PropertySheetHolder getPropertySheetHolder() {
return obj;
}
@Override
public TreeNode getChildAt(int childIndex) {
return children.get(childIndex);
}
@Override
public int getChildCount() {
return children.size();
}
@Override
public TreeNode getParent() {
return parent;
}
@Override
public int getIndex(TreeNode node) {
return children.indexOf(node);
}
@Override
public boolean getAllowsChildren() {
return children.size() > 0;
}
@Override
public boolean isLeaf() {
return children.size() < 1;
}
@Override
public Enumeration children() {
return Collections.enumeration(children);
}
@Override
public String toString() {
return obj.getPropertySheetHolderTitle();
}
}
private TreeCellRenderer treeCellRenderer = new DefaultTreeCellRenderer() {
// http://stackoverflow.com/questions/20691946/set-icon-to-each-node-in-jtree
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row,
hasFocus);
if (value instanceof PropertySheetHolderTreeNode) {
PropertySheetHolderTreeNode node = (PropertySheetHolderTreeNode) value;
PropertySheetHolder psh = node.getPropertySheetHolder();
setIcon(psh.getPropertySheetHolderIcon());
}
return this;
}
};
private class SwingAction extends AbstractAction {
public SwingAction() {
putValue(NAME, Translations.getString("MachineSetupPanel.Action.Expand")); //$NON-NLS-1$
putValue(SHORT_DESCRIPTION, Translations.getString("MachineSetupPanel.Action.Expand.Description")); //$NON-NLS-1$
}
public void actionPerformed(ActionEvent e) {
for (int i = 1; i < tree.getRowCount(); i++) {
if (cbExp.isSelected()) {
tree.expandRow(i);
}
else {
tree.collapseRow(i);
}
}
}
}
}