1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jarduino.gui;
18
19 import java.awt.BorderLayout;
20 import java.awt.CardLayout;
21 import java.awt.Dimension;
22 import java.util.EnumMap;
23 import java.util.Map;
24 import java.util.Objects;
25 import javax.swing.BorderFactory;
26 import javax.swing.Box;
27 import javax.swing.BoxLayout;
28 import javax.swing.JButton;
29 import javax.swing.JComponent;
30 import javax.swing.JFrame;
31 import javax.swing.JLabel;
32 import javax.swing.JPanel;
33 import javax.swing.JScrollPane;
34 import javax.swing.JSeparator;
35 import javax.swing.border.Border;
36
37 import net.sourceforge.jarduino.message.ArduinoAttribute.ArduinoAttrObject;
38 import net.sourceforge.jarduino.message.ArduinoChar;
39 import net.sourceforge.jarduino.message.ArduinoMessage;
40 import net.sourceforge.jarduino.message.ArduinoNamedObject;
41 import net.sourceforge.jarduino.message.ArduinoNode;
42 import net.sourceforge.jarduino.message.ArduinoSignal;
43 import net.sourceforge.jarduino.message.ArduinoSystem;
44 import net.sourceforge.jarduino.message.ArduinoValues;
45
46
47
48
49 public class ArduinoPanelMeta {
50
51
52
53 private static final String VIEW_ATTR = "Attributes";
54
55
56
57
58 private static final String VIEW_VALUES = "Values";
59
60
61
62
63 private static final String VIEW_CONNECTS = "Connections";
64
65
66
67
68 private final JPanel thePanel;
69
70
71
72
73 private final ArduinoScrollButton<ArduinoNode> theNode;
74
75
76
77
78 private final ArduinoScrollButton<ArduinoMessage> theMessage;
79
80
81
82
83 private final ArduinoScrollButton<ArduinoSignal> theSignal;
84
85
86
87
88 private final JButton theViewButton;
89
90
91
92
93 private final Map<ArduinoConfigMode, JPanel> theButtons;
94
95
96
97
98 private final JPanel theSelect;
99
100
101
102
103 private final JPanel theFiller;
104
105
106
107
108 private final JPanel theView;
109
110
111
112
113 private final JLabel theComment;
114
115
116
117
118 private final ArduinoTableAttr theAttrs;
119
120
121
122
123 private final ArduinoTableValues theValues;
124
125
126
127
128 private final ArduinoTableNodeConnect theNodeConnect;
129
130
131
132
133 private final ArduinoTableMsgConnect theMsgConnect;
134
135
136
137
138 private final JPanel theTablePanel;
139
140
141
142
143 private ArduinoConfigMode theMode;
144
145
146
147
148 private ArduinoSystem theSystem;
149
150
151
152
153 private ArduinoNamedObject theSelected;
154
155
156
157
158 private boolean showValues;
159
160
161
162
163
164 ArduinoPanelMeta(final JFrame pFrame) {
165
166 theNode = new ArduinoScrollButton<>(pFrame);
167 theNode.setFormatter(ArduinoNode::getName);
168 theNode.onSelect(this::setSelected);
169 theMessage = new ArduinoScrollButton<>(pFrame);
170 theMessage.setFormatter(ArduinoMessage::getName);
171 theMessage.onSelect(this::setSelected);
172 theSignal = new ArduinoScrollButton<>(pFrame);
173 theSignal.setFormatter(ArduinoSignal::getName);
174 theSignal.onSelect(this::setSelected);
175
176
177 final ArduinoScrollButton<ArduinoConfigMode> myModes = new ArduinoScrollButton<>(pFrame);
178 for (ArduinoConfigMode myMode : ArduinoConfigMode.values()) {
179 myModes.add(myMode);
180 }
181 myModes.setSelectedItem(ArduinoConfigMode.SYSTEM);
182 myModes.onSelect(this::selectMode);
183
184
185 theButtons = new EnumMap<>(ArduinoConfigMode.class);
186
187
188 final JPanel myModePanel = new JPanel();
189 myModePanel.setLayout(new BoxLayout(myModePanel, BoxLayout.X_AXIS));
190 myModePanel.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
191 myModePanel.add(myModes.getComponent());
192 myModePanel.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
193 Border myBorder = BorderFactory.createTitledBorder("Mode");
194 myModePanel.setBorder(myBorder);
195
196
197 theSelect = new JPanel();
198 theSelect.setLayout(new BoxLayout(theSelect, BoxLayout.X_AXIS));
199 theSelect.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
200 theSelect.add(buildMsgPanel(ArduinoConfigMode.NODE, theNode));
201 theSelect.add(buildMsgPanel(ArduinoConfigMode.MESSAGE, theMessage));
202 theSelect.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
203 theSelect.add(buildMsgPanel(ArduinoConfigMode.SIGNAL, theSignal));
204 theSelect.add(Box.createHorizontalGlue());
205 myBorder = BorderFactory.createTitledBorder("Selection");
206 theSelect.setBorder(myBorder);
207
208
209 theViewButton = new JButton(VIEW_ATTR);
210 theViewButton.addActionListener(e -> {
211 showValues = !showValues;
212 theViewButton.setText(getViewButtonText());
213 updateView();
214 });
215
216
217 theView = new JPanel();
218 theView.setLayout(new BoxLayout(theView, BoxLayout.X_AXIS));
219 theView.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
220 theView.add(theViewButton);
221 theView.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
222 myBorder = BorderFactory.createTitledBorder("View");
223 theView.setBorder(myBorder);
224
225
226 theFiller = new JPanel();
227 theFiller.setLayout(new BoxLayout(theFiller, BoxLayout.X_AXIS));
228 theFiller.add(Box.createHorizontalGlue());
229
230
231 final JPanel mySelectPanel = new JPanel();
232 mySelectPanel.setLayout(new BoxLayout(mySelectPanel, BoxLayout.X_AXIS));
233 mySelectPanel.add(myModePanel);
234 mySelectPanel.add(theSelect);
235 mySelectPanel.add(theFiller);
236 mySelectPanel.add(theView);
237
238
239 theComment = new JLabel();
240 myBorder = BorderFactory.createTitledBorder("Comment");
241 theComment.setBorder(myBorder);
242 final JPanel myComment = new JPanel(new BorderLayout());
243 myComment.add(theComment);
244
245
246 final JPanel myHeader = new JPanel();
247 myHeader.setLayout(new BoxLayout(myHeader, BoxLayout.Y_AXIS));
248 myHeader.add(mySelectPanel);
249 myHeader.add(new JSeparator());
250 myHeader.add(myComment);
251
252
253 theAttrs = new ArduinoTableAttr();
254 final JScrollPane myAttrScroll = new JScrollPane(theAttrs.getComponent());
255 myBorder = BorderFactory.createTitledBorder(VIEW_ATTR);
256 myAttrScroll.setBorder(myBorder);
257
258
259 theValues = new ArduinoTableValues();
260 final JScrollPane myValueScroll = new JScrollPane(theValues.getComponent());
261 myBorder = BorderFactory.createTitledBorder(VIEW_VALUES);
262 myValueScroll.setBorder(myBorder);
263
264
265 theNodeConnect = new ArduinoTableNodeConnect();
266 myBorder = BorderFactory.createTitledBorder(VIEW_CONNECTS);
267 theNodeConnect.getComponent().setBorder(myBorder);
268
269
270 theMsgConnect = new ArduinoTableMsgConnect();
271 myBorder = BorderFactory.createTitledBorder(VIEW_CONNECTS);
272 theMsgConnect.getComponent().setBorder(myBorder);
273
274
275 theTablePanel = new JPanel(new CardLayout());
276 theTablePanel.add(myAttrScroll, VIEW_ATTR);
277 theTablePanel.add(myValueScroll, VIEW_VALUES);
278 theTablePanel.add(theNodeConnect.getComponent(), VIEW_CONNECTS + ArduinoConfigMode.NODE.toString());
279 theTablePanel.add(theMsgConnect.getComponent(), VIEW_CONNECTS + ArduinoConfigMode.MESSAGE.toString());
280
281
282 thePanel = new JPanel(new BorderLayout());
283 thePanel.add(myHeader, BorderLayout.PAGE_START);
284 thePanel.add(theTablePanel, BorderLayout.CENTER);
285
286
287 theMode = ArduinoConfigMode.SYSTEM;
288 }
289
290
291
292
293
294 JComponent getComponent() {
295 return thePanel;
296 }
297
298
299
300
301
302 private void selectMode(final ArduinoConfigMode pMode) {
303
304 if (theMode == pMode) {
305 return;
306 }
307
308
309 setVisibility(pMode);
310
311
312 theMode = pMode;
313 switch (theMode) {
314 case NODE:
315 setSelected(theNode.getSelectedItem());
316 break;
317 case MESSAGE:
318 setSelected(theMessage.getSelectedItem());
319 break;
320 case SIGNAL:
321
322 final ArduinoSignal mySignal = theSignal.getSelectedItem();
323 setSelected(mySignal == null ? theMessage.getSelectedItem() : mySignal);
324 break;
325 case SYSTEM:
326 default:
327 setSelected(theSystem);
328 break;
329 }
330
331
332 theViewButton.setText(getViewButtonText());
333 }
334
335
336
337
338
339 private void setSelected(final ArduinoNamedObject pObject) {
340
341 if (!theMode.checkObject(pObject)
342 || Objects.equals(theSelected, pObject)) {
343 return;
344 }
345
346
347 if (pObject instanceof ArduinoMessage) {
348
349 final ArduinoMessage/net/sourceforge/jarduino/message/ArduinoMessage.html#ArduinoMessage">ArduinoMessage myMessage = (ArduinoMessage) pObject;
350 if (theMode == ArduinoConfigMode.SIGNAL
351 && theSelected instanceof ArduinoSignal
352 && pObject.equals(((ArduinoSignal) theSelected).getOwner())) {
353 return;
354 }
355
356
357 theSignal.removeAll();
358 for (ArduinoSignal mySignal : myMessage.getAllSignals()) {
359
360 theSignal.add(mySignal);
361 }
362
363
364 if (theMode == ArduinoConfigMode.SIGNAL) {
365 return;
366 }
367 }
368
369
370 setComment(pObject);
371 theAttrs.configureTable(theSystem, theMode, (ArduinoAttrObject) pObject);
372 theValues.getComponent().setVisible(false);
373 theView.setVisible(false);
374 if (pObject instanceof ArduinoSignal) {
375 final ArduinoValues myValues = ((ArduinoSignal) pObject).getValues();
376 if (myValues != null) {
377 theValues.configureTable(myValues);
378 theValues.getComponent().setVisible(true);
379 theView.setVisible(true);
380 }
381 }
382 if (pObject instanceof ArduinoNode) {
383 theNodeConnect.configureTable((ArduinoNode) pObject);
384 theView.setVisible(true);
385 }
386 if (pObject instanceof ArduinoMessage) {
387 theMsgConnect.configureTable((ArduinoMessage) pObject);
388 theView.setVisible(true);
389 }
390 theSelected = pObject;
391 updateView();
392
393
394 thePanel.setPreferredSize(new Dimension(ArduinoPanelMain.WIDTH, ArduinoPanelMain.HEIGHT));
395 }
396
397
398
399
400
401 private void setComment(final ArduinoNamedObject pObject) {
402
403 String myComment = theSystem.getCommentForObject(pObject);
404
405
406 myComment = cleanseLabelText(myComment);
407
408
409 theComment.setText(myComment);
410 theComment.setVisible(myComment != null && !showValues);
411 }
412
413
414
415
416
417
418 static String cleanseLabelText(final String pText) {
419
420 String myText = pText;
421
422
423 if (myText != null
424 && myText.indexOf(ArduinoChar.LF) != -1) {
425
426 myText = "<html>"
427 + myText.replace("&", "&")
428 .replace(">", ">")
429 .replace("<", "<")
430 .replace(Character.toString(ArduinoChar.LF), "<br>");
431 }
432
433
434 return myText;
435 }
436
437
438
439
440
441
442
443 private JPanel buildMsgPanel(final ArduinoConfigMode pMode,
444 final ArduinoScrollButton<?> pButton) {
445
446 final JPanel myPanel = new JPanel();
447 myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
448 myPanel.add(new JLabel(pMode.toString() + ArduinoChar.COLON));
449 myPanel.add(Box.createHorizontalStrut(ArduinoPanelMain.STRUTSIZE));
450 myPanel.add(pButton.getComponent());
451
452
453 theButtons.put(pMode, myPanel);
454 return myPanel;
455 }
456
457
458
459
460
461 private void setVisibility(final ArduinoConfigMode pMode) {
462
463 final boolean bNodeVisible = pMode == ArduinoConfigMode.NODE;
464 final boolean bSignalVisible = pMode == ArduinoConfigMode.SIGNAL;
465 final boolean bMessageVisible = pMode == ArduinoConfigMode.MESSAGE || bSignalVisible;
466 final boolean bSelectVisible = pMode != ArduinoConfigMode.SYSTEM;
467
468
469 theButtons.get(ArduinoConfigMode.NODE).setVisible(bNodeVisible);
470 theButtons.get(ArduinoConfigMode.MESSAGE).setVisible(bMessageVisible);
471 theButtons.get(ArduinoConfigMode.SIGNAL).setVisible(bSignalVisible);
472
473
474 theSelect.setVisible(bSelectVisible);
475 theFiller.setVisible(!bSelectVisible);
476 }
477
478
479
480
481 private void updateView() {
482
483 String myCard = VIEW_ATTR;
484 if (showValues) {
485 if (theSelected instanceof ArduinoNode) {
486 myCard = VIEW_CONNECTS + ArduinoConfigMode.NODE.toString();
487 } else if (theSelected instanceof ArduinoMessage) {
488 myCard = VIEW_CONNECTS + ArduinoConfigMode.MESSAGE.toString();
489 } else if (theSelected instanceof ArduinoSignal
490 && ((ArduinoSignal) theSelected).getValues() != null) {
491 myCard = VIEW_VALUES;
492 }
493 }
494
495
496 final CardLayout myLayout = (CardLayout) theTablePanel.getLayout();
497 myLayout.show(theTablePanel, myCard);
498 }
499
500
501
502
503
504 private String getViewButtonText() {
505
506 if (showValues) {
507 switch (theMode) {
508 case NODE:
509 case MESSAGE:
510 return VIEW_CONNECTS;
511 case SIGNAL:
512 return VIEW_VALUES;
513 default:
514 break;
515 }
516 }
517 return VIEW_ATTR;
518 }
519
520
521
522
523
524 void setSystem(final ArduinoSystem pSystem) {
525
526 theSystem = pSystem;
527
528
529 theSelected = null;
530 theNode.removeAll();
531 theMessage.removeAll();
532 theSignal.removeAll();
533
534
535 for (ArduinoNode myNode : theSystem.getNodes()) {
536
537 if (!myNode.getName().equals(ArduinoNode.NULL_NODE)) {
538 theNode.add(myNode);
539 }
540
541
542 for (ArduinoMessage myMessage : myNode.getMessages()) {
543
544 theMessage.add(myMessage);
545 }
546 }
547
548
549 final ArduinoConfigMode myMode = theMode;
550 theMode = null;
551 selectMode(myMode);
552 }
553
554
555
556
557 enum ArduinoConfigMode {
558
559
560
561 SYSTEM("System"),
562
563
564
565
566 NODE("Node"),
567
568
569
570
571 MESSAGE("Message"),
572
573
574
575
576 SIGNAL("Signal");
577
578
579
580
581 private final String theName;
582
583
584
585
586
587 ArduinoConfigMode(final String pName) {
588 theName = pName;
589 }
590
591
592
593
594
595
596 boolean checkObject(final ArduinoNamedObject pObject) {
597 switch (this) {
598 case NODE:
599 return pObject instanceof ArduinoNode;
600 case MESSAGE:
601 return pObject instanceof ArduinoMessage;
602 case SIGNAL:
603 return pObject instanceof ArduinoMessage
604 || pObject instanceof ArduinoSignal;
605 case SYSTEM:
606 return pObject instanceof ArduinoSystem;
607 default:
608 return false;
609 }
610 }
611
612 @Override
613 public String toString() {
614 return theName;
615 }
616 }
617 }