-
Notifications
You must be signed in to change notification settings - Fork 325
/
BeanSwitch.java
1221 lines (1127 loc) · 47 KB
/
BeanSwitch.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package jmri.jmrit.display.switchboardEditor;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import javax.annotation.Nonnull;
import javax.imageio.ImageIO;
import javax.swing.*;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.Light;
import jmri.NamedBean;
import jmri.NamedBeanHandle;
import jmri.Sensor;
import jmri.Turnout;
import jmri.jmrit.beantable.AddNewDevicePanel;
import jmri.jmrit.display.Positionable;
import jmri.util.JmriJFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Class for a switchboard interface object.
* <p>
* Contains a JButton or JPanel to control existing turnouts, sensors and
* lights.
* Separated from SwitchboardEditor.java in 4.12.3
*
* @author Egbert Broerse Copyright (c) 2017, 2018, 2020
*/
public class BeanSwitch extends JPanel implements java.beans.PropertyChangeListener, ActionListener {
private final JButton beanButton;
private final int _shape;
private final String _switchSysName;
private String _uName = "unconnected";
private String _uLabel = ""; // for display, empty if userName == null
private final Boolean showUserName;
protected String switchLabel;
protected String switchButtonLabel;
protected String switchTooltip;
protected boolean _text;
protected boolean _icon = false;
protected boolean _control = false;
protected String _state;
protected String _color;
protected String stateClosed = Bundle.getMessage("StateClosedShort");
protected String stateThrown = Bundle.getMessage("StateThrownShort");
// the associated Bean object
private final NamedBean _bname;
private NamedBeanHandle<?> namedBean = null; // could be Turnout, Sensor or Light
protected jmri.NamedBeanHandleManager nbhm = jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class);
private IconSwitch beanIcon;
private IconSwitch beanKey;
private IconSwitch beanSymbol;
private final char beanTypeChar;
private final Color defaultActiveColor = Color.RED;
private final Color defaultInactiveColor = Color.GREEN;
//private final Color defaultUnknownColor = Color.WHITE; // often hard to see
private final SwitchboardEditor _editor;
/**
* Ctor.
*
* @param index DCC address.
* @param bean layout object to connect to.
* @param switchName descriptive name corresponding with system name to
* display in switch tooltip, i.e. LT1.
* @param shapeChoice Button, Icon (static) or Drawing (vector graphics).
* @param editor main switchboard editor.
*/
public BeanSwitch(int index, NamedBean bean, String switchName, int shapeChoice, SwitchboardEditor editor) {
log.debug("Name = [{}]", switchName);
_switchSysName = switchName;
sysNameTextBox.setText(switchName); // setting name here allows test of AddNew()
_editor = editor;
_bname = bean;
showUserName = (_editor.showUserName().equals("yes"));
if (bean != null) {
_uName = bean.getUserName();
log.debug("Switch userName from bean: {}", _uName);
if (_uName == null) {
_uName = Bundle.getMessage("NoUserName");
} else if (showUserName) {
_uLabel = _uName;
}
}
beanButton = new JButton();
beanButton.setText(getSwitchButtonLabel(_switchSysName + ": ?")); // initial text to display
switchTooltip = switchName + " (" + _uName + ")";
this.setLayout(new BorderLayout()); // makes JButtons expand to the whole grid cell
_shape = shapeChoice;
String beanManuPrefix = _editor.getSwitchManu(); // connection/manufacturer prefix i.e. M for MERG
beanTypeChar = _switchSysName.charAt(beanManuPrefix.length()); // bean type, i.e. L, usually at char(1)
// check for space char which might be caused by connection name > 2 chars and/or space in name
if (beanTypeChar != 'T' && beanTypeChar != 'S' && beanTypeChar != 'L') { // add if more bean types are supported
log.error("invalid char in Switchboard Button \"{}\". Check connection name.", _switchSysName);
JOptionPane.showMessageDialog(null, Bundle.getMessage("ErrorSwitchAddFailed"),
Bundle.getMessage("WarningTitle"),
JOptionPane.ERROR_MESSAGE);
return;
}
int r = _editor.getTileSize()/2; // max WxH of canvas inside cell, used as relative unit to draw
log.debug("BeanSwitch graphic tilesize/2 r = {}", r);
beanIcon = new IconSwitch(iconOnPath, iconOffPath, r);
beanKey = new IconSwitch(keyOnPath, keyOffPath, r);
beanSymbol = new IconSwitch(rootPath + beanTypeChar + "-on-s.png", rootPath + beanTypeChar + "-off-s.png", r);
// look for bean to connect to by name
log.debug("beanconnect = {}, beantype = {}", beanManuPrefix, beanTypeChar);
try {
if (bean != null) {
namedBean = nbhm.getNamedBeanHandle(switchName, bean);
}
} catch (IllegalArgumentException e) {
log.error("invalid bean name= \"{}\" in Switchboard Button", switchName);
}
// attach shape specific code to this beanSwitch
switch (_shape) {
case 1: // slider shape
beanIcon.addMouseListener(new MouseAdapter() { // handled by JPanel
@Override
public void mouseClicked(MouseEvent me) {
operate(me, switchName);
}
@Override
public void mouseReleased(MouseEvent me) { // for Windows
if (me.isPopupTrigger()) {
showPopUp(me); // display the popup
}
}
@Override
public void mousePressed(MouseEvent me) { // for macOS, Linux
if (me.isPopupTrigger()) {
showPopUp(me); // display the popup
}
}
});
_text = true; // not actually used
_icon = true;
beanIcon.setPreferredSize(new Dimension(2*r, 2*r));
beanIcon.setLabels(switchLabel, _uLabel);
beanIcon.positionLabel(0, 5*r/-8, Component.CENTER_ALIGNMENT, 14);
beanIcon.positionSubLabel(0, r/3, Component.CENTER_ALIGNMENT, 8); // smaller (system name)
if (_editor.showToolTip()) {
beanIcon.setToolTipText(switchTooltip);
}
beanIcon.setBackground(_editor.getDefaultBackgroundColor());
//remove the line around icon switches?
this.add(beanIcon);
break;
case 2: // Maerklin style keyboard
beanKey.addMouseListener(new MouseAdapter() { // handled by JPanel
@Override
public void mouseClicked(MouseEvent me) {
operate(me, switchName);
}
@Override
public void mouseReleased(MouseEvent me) { // for Windows
if (me.isPopupTrigger()) {
showPopUp(me); // display the popup
}
}
@Override
public void mousePressed(MouseEvent me) { // for macOS, Linux
if (me.isPopupTrigger()) {
showPopUp(me); // display the popup
}
}
});
_text = true; // not actually used for Switchboards
_icon = true;
beanKey.setPreferredSize(new Dimension(new Dimension(2*r, 2*r)));
beanKey.setLabels(switchLabel, _uLabel);
beanKey.positionLabel(0, r/16, Component.CENTER_ALIGNMENT, 14);
beanKey.positionSubLabel(0, r/4, Component.CENTER_ALIGNMENT, 8); // smaller (system name)
// provide x, y offset, depending on image size and free space
if (_editor.showToolTip()) {
beanKey.setToolTipText(switchTooltip);
}
beanKey.setBackground(_editor.getDefaultBackgroundColor());
//remove the line around icon switches?
this.add(beanKey);
break;
case 3: // turnout/sensor/light Icon (selecting image by letter in switch name/label)
beanSymbol.addMouseListener(new MouseAdapter() { // handled by JPanel
@Override
public void mouseClicked(MouseEvent me) {
operate(me, switchName);
}
@Override
public void mouseReleased(MouseEvent me) { // for Windows
if (me.isPopupTrigger()) {
showPopUp(me); // display the popup
}
}
@Override
public void mousePressed(MouseEvent me) { // for macOS, Linux
if (me.isPopupTrigger()) {
showPopUp(me); // display the popup
}
}
});
_text = true; // web server supports in-browser drawn switches
_icon = true; // panel.js assigns only text OR icon for a single class such as BeanSwitch
beanSymbol.setPreferredSize(new Dimension(2*r, 2*r));
beanSymbol.setLabels(switchLabel, _uLabel);
switch (beanTypeChar) {
case 'T' :
beanSymbol.positionLabel(0, -3*r/5, Component.CENTER_ALIGNMENT, 14);
beanSymbol.positionSubLabel(0, 6*r/8, Component.CENTER_ALIGNMENT, 8);
break;
case 'S' :
case 'L' :
default :
beanSymbol.positionLabel(0, r/-3, Component.CENTER_ALIGNMENT, 14);
beanSymbol.positionSubLabel(0, 6*r/8, Component.CENTER_ALIGNMENT, 8);
}
if (_editor.showToolTip()) {
beanSymbol.setToolTipText(switchTooltip);
}
beanSymbol.setBackground(_editor.getDefaultBackgroundColor());
// common (in)activecolor etc defined in SwitchboardEditor, retrieved by Servlet
// remove the line around icon switches?
this.setBorder(BorderFactory.createLineBorder(_editor.getDefaultBackgroundColor(), 3));
this.add(beanSymbol);
break;
case 0: // 0 = "Button" shape
default:
beanButton.addMouseListener(new MouseAdapter() { // handled by JPanel
@Override
public void mouseClicked(MouseEvent me) {
operate(me, switchName);
}
@Override
public void mouseReleased(MouseEvent me) { // for Windows
if (me.isPopupTrigger()) {
showPopUp(me); // display the popup
}
}
@Override
public void mousePressed(MouseEvent me) { // for macOS, Linux
if (me.isPopupTrigger()) {
showPopUp(me); // display the popup
}
}
});
_text = true;
_icon = false;
beanButton.setForeground(_editor.getDefaultTextColorAsColor());
beanButton.setOpaque(true); // to show color from the start
this.setBorder(BorderFactory.createLineBorder(_editor.getDefaultBackgroundColor(), 2));
if (_editor.showToolTip()) {
beanButton.setToolTipText(switchTooltip);
}
beanButton.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if (showUserName && beanButton.getHeight() < 50) {
beanButton.setVerticalAlignment(JLabel.TOP);
} else {
beanButton.setVerticalAlignment(JLabel.CENTER); //default
}
}
});
beanButton.setMargin(new Insets(4, 1, 2, 1));
this.add(beanButton);
break;
}
// connect to object or dim switch
if (bean == null) {
if (!_editor.hideUnconnected()) {
// to dim unconnected symbols TODO make graphics see through, now icons just become bleak
//float dim = 100f;
switch (_shape) {
case 0:
beanButton.setEnabled(false);
break;
case 1:
// beanIcon.setOpacity(dim);
// break;
case 2:
// beanKey.setOpacity(dim);
// break;
case 3:
default:
// beanSymbol.setOpacity(dim);
}
displayState(0); // show unconnected as unknown/greyed
}
} else {
_control = true;
switch (beanTypeChar) {
case 'T':
getTurnout().addPropertyChangeListener(this, _switchSysName, "Switchboard Editor Turnout Switch");
if (getTurnout().canInvert()) {
this.setInverted(getTurnout().getInverted()); // only add and set when supported by object/connection
}
break;
case 'S':
getSensor().addPropertyChangeListener(this, _switchSysName, "Switchboard Editor Sensor Switch");
if (getSensor().canInvert()) {
this.setInverted(getSensor().getInverted()); // only add and set when supported by object/connection
}
break;
default: // light
getLight().addPropertyChangeListener(this, _switchSysName, "Switchboard Editor Light Switch");
// Lights do not support Invert
}
}
// from finishClone
setTristate(getTristate());
setMomentary(getMomentary());
log.debug("Created switch {}", index);
}
public NamedBean getNamedBean() {
return _bname;
}
/**
* Store an object as NamedBeanHandle, using _label as the display
* name.
*
* @param bean the object (either a Turnout, Sensor or Light) to attach
* to this switch
*/
public void setNamedBean(@Nonnull NamedBean bean) {
try {
namedBean = nbhm.getNamedBeanHandle(_switchSysName, bean);
} catch (IllegalArgumentException e) {
log.error("invalid bean name= \"{}\" in Switchboard Button", _switchSysName);
}
_uName = bean.getUserName();
if (_uName == null) {
_uName = Bundle.getMessage("NoUserName");
} else {
if (showUserName) _uLabel = _uName;
}
_control = true;
}
public Turnout getTurnout() {
if (namedBean == null) {
return null;
}
return (Turnout) namedBean.getBean();
}
public Sensor getSensor() {
if (namedBean == null) {
return null;
}
return (Sensor) namedBean.getBean();
}
public Light getLight() {
if (namedBean == null) {
return null;
}
return (Light) namedBean.getBean();
}
/**
* Get the user selected switch shape (e.g. 3 for Slider)
*
* @return the index of the selected item in Shape comboBox
*/
public int getShape() {
return _shape;
}
/**
* Get text to display on this switch on Switchboard and in Web Server panel when attached
* object is Active.
*
* @return text to show on active state (differs per type of object)
*/
public String getActiveText() {
// fetch bean specific abbreviation
switch (beanTypeChar) {
case 'T':
_state = stateClosed; // +
break;
default: // Light, Sensor
_state = "+"; // 1 char abbreviation for StateOff not clear
}
return _switchSysName + ": " + _state;
}
/**
* Get text to display on this switch on Switchboard and in Web Server panel when attached
* object is Inactive.
*
* @return text to show on inactive state (differs per type of objects)
*/
public String getInactiveText() {
// fetch bean specific abbreviation
switch (beanTypeChar) {
case 'T':
_state = stateThrown; // +
break;
default: // Light, Sensor
_state = "-"; // 1 char abbreviation for StateOff not clear
}
return _switchSysName + ": " + _state;
}
/**
* Get text to display on this switch in Web Server panel when attached
* object is Unknown (initial state displayed).
*
* @return text to show on unknown state (used on all types of objects)
*/
public String getUnknownText() {
return _switchSysName + ": ?";
}
public String getInconsistentText() {
return _switchSysName + ": X";
}
/**
* Get text to display as switch tooltip in Web Server panel.
* Used in jmri.jmrit.display.switchboardEditor.configureXml.BeanSwitchXml#store(Object)
*
* @return switch tooltip text
*/
public String getToolTip() {
return switchTooltip;
}
// ******************* Display ***************************
@Override
public void actionPerformed(ActionEvent e) {
//updateBean();
}
/**
* Get the label of this switch.
*
* @return display name including current state
*/
public String getNameString() {
return _switchSysName;
}
public String getUserNameString() {
return _uLabel;
}
private String getSwitchButtonLabel(String label) {
if (!showUserName || _uLabel.equals("")) {
return label;
} else {
String subLabel = _uLabel.substring(0, (Math.min(_uLabel.length(), 35))); // reasonable max. to display 2 lines on tile
return "<html><center>" + label + "</center><br><center><i>" + subLabel + "</i></center></html>"; // 2 lines of text
}
}
/**
* Drive the current state of the display from the state of the
* connected bean.
*
* @param state integer representing the new state e.g. Turnout.CLOSED
*/
public void displayState(int state) {
String switchLabel;
Color switchColor;
log.debug("Change heard. STATE={}", state);
// display abbreviated name of state instead of state index, fine for unconnected switches too
switch (state) {
case 1:
switchLabel = getUnknownText();
switchColor = Color.GRAY;
break;
case 2:
switchLabel = getActiveText();
switchColor = defaultActiveColor;
break;
case 4:
switchLabel = getInactiveText();
switchColor = defaultInactiveColor;
break;
default:
switchLabel = getInconsistentText();
switchColor = Color.WHITE;
//log.warn("SwitchState INCONSISTENT"); // normal for unconnected switchboard
}
if (getNamedBean() == null) {
switchLabel = _switchSysName; // unconnected, doesn't show state using : and ?
log.debug("Switch label {} state {}, disconnected", switchLabel, state);
} else {
log.debug("Switch label {} state: {}, connected", switchLabel, state);
}
if (isText() && !isIcon()) { // to allow text buttons on web switchboard. always add graphic switches on web
log.debug("Label = {}", getSwitchButtonLabel(switchLabel));
beanButton.setText(getSwitchButtonLabel(switchLabel));
beanButton.setBackground(switchColor); // only the color is visible TODO get access to bg color of JButton?
beanButton.setOpaque(true);
}
if (isIcon() && beanIcon != null && beanKey != null && beanSymbol != null) {
beanIcon.showSwitchIcon(state);
beanIcon.setLabels(switchLabel, _uLabel);
beanKey.showSwitchIcon(state);
beanKey.setLabels(switchLabel, _uLabel);
beanSymbol.showSwitchIcon(state);
beanSymbol.setLabels(switchLabel, _uLabel);
}
}
/**
* Switch presentation is graphic image based.
*
* @see #displayState(int)
* @return true when switch shape other than 'Button' is selected
*/
public final boolean isIcon() {
return _icon;
}
/**
* Switch presentation is text based.
*
* @see #displayState(int)
* @return true when switch shape 'Button' is selected (and also for the
* other, graphic switch types until SwitchboardServlet directly
* supports their graphic icons)
*/
public final boolean isText() {
return _text;
}
/**
* Get current state of attached turnout.
*
* @return A state variable from a Turnout, e.g. Turnout.CLOSED
*/
int turnoutState() {
if (namedBean != null) {
return getTurnout().getKnownState();
} else {
return Turnout.UNKNOWN;
}
}
/**
* Update switch as state of turnout changes.
*
* @param e the PropertyChangeEvent heard
*/
@Override
public void propertyChange(java.beans.PropertyChangeEvent e) {
if (log.isDebugEnabled()) {
log.debug("property change: {} {} is now: {}", _switchSysName, e.getPropertyName(), e.getNewValue());
}
// when there's feedback, transition through inconsistent icon for better animation
if (getTristate()
&& (getTurnout().getFeedbackMode() != Turnout.DIRECT)
&& (e.getPropertyName().equals("CommandedState"))) {
if (getTurnout().getCommandedState() != getTurnout().getKnownState()) {
int now = Turnout.INCONSISTENT;
displayState(now);
}
// this takes care of the quick double click
if (getTurnout().getCommandedState() == getTurnout().getKnownState()) {
int now = ((Integer) e.getNewValue());
displayState(now);
}
}
if (e.getPropertyName().equals("KnownState")) {
int now = ((Integer) e.getNewValue());
displayState(now);
log.debug("Item state changed");
}
if (e.getPropertyName().equals("UserName")) {
// update tooltip
String newUserName;
if (_editor.showToolTip()) {
newUserName = ((String) e.getNewValue());
_uLabel = (newUserName == null ? "" : newUserName); // store for display on icon
if (newUserName == null || newUserName.equals("")) {
newUserName = Bundle.getMessage("NoUserName"); // longer for tooltip
}
beanButton.setToolTipText(_switchSysName + " (" + newUserName + ")");
beanIcon.setToolTipText(_switchSysName + " (" + newUserName + ")");
beanKey.setToolTipText(_switchSysName + " (" + newUserName + ")");
beanSymbol.setToolTipText(_switchSysName + " (" + newUserName + ")");
log.debug("User Name changed to {}", newUserName);
}
}
}
void cleanup() {
if (namedBean != null) {
getTurnout().removePropertyChangeListener(this);
}
namedBean = null;
}
private boolean tristate = false;
public void setTristate(boolean set) {
tristate = set;
}
public boolean getTristate() {
return tristate;
}
boolean momentary = false;
public boolean getMomentary() {
return momentary;
}
public void setMomentary(boolean m) {
momentary = m;
}
JPopupMenu switchPopup;
JMenuItem connectNewMenu = new JMenuItem(Bundle.getMessage("ConnectNewMenu", "..."));
/**
* Show pop-up on a switch with its unique attributes including the
* (un)connected bean. Derived from
* {@link jmri.jmrit.display.switchboardEditor.SwitchboardEditor#showPopUp(Positionable, MouseEvent)}
*
* @param e unused
* @return true when pop up displayed
*/
public boolean showPopUp(MouseEvent e) {
if (switchPopup != null) {
switchPopup.removeAll();
} else {
switchPopup = new JPopupMenu();
}
switchPopup.add(getNameString());
if (_editor.isEditable()) {
// add tristate option if turnout has feedback
if (namedBean != null) {
//addTristateEntry(switchPopup); // beanswitches don't do anything with this property
addEditUserName(switchPopup);
switch (beanTypeChar) {
case 'T':
if (getTurnout().canInvert()) { // check whether supported by this turnout
addInvert(switchPopup);
}
break;
case 'S':
if (getSensor().canInvert()) { // check whether supported by this sensor
addInvert(switchPopup);
}
break;
default:
// invert is not supported by Lights, so skip
}
} else {
// show option to attach a new bean
switchPopup.add(connectNewMenu);
connectNewMenu.addActionListener((java.awt.event.ActionEvent e1) -> connectNew(_switchSysName));
}
}
// display the popup
switchPopup.show(this, this.getWidth() / 3 + (int) ((_editor.getPaintScale() - 1.0) * this.getX()),
this.getHeight() / 3 + (int) ((_editor.getPaintScale() - 1.0) * this.getY()));
return true;
}
javax.swing.JMenuItem editItem = null;
void addEditUserName(JPopupMenu popup) {
editItem = new javax.swing.JMenuItem(Bundle.getMessage("EditNameTitle", "..."));
popup.add(editItem);
editItem.addActionListener((java.awt.event.ActionEvent e) -> renameBean());
}
javax.swing.JCheckBoxMenuItem invertItem = null;
void addInvert(JPopupMenu popup) {
invertItem = new javax.swing.JCheckBoxMenuItem(Bundle.getMessage("MenuInvertItem", _switchSysName));
invertItem.setSelected(getInverted());
popup.add(invertItem);
invertItem.addActionListener((java.awt.event.ActionEvent e) -> setBeanInverted(invertItem.isSelected()));
}
/**
* Edit user name on a switch.
*/
public void renameBean() {
NamedBean nb;
String oldName = _uName;
// show input dialog
String newUserName = (String) JOptionPane.showInputDialog(null,
Bundle.getMessage("EnterNewName", _switchSysName),
Bundle.getMessage("EditNameTitle", ""), JOptionPane.PLAIN_MESSAGE, null, null, oldName);
if (newUserName == null) { // user cancelled
log.debug("NewName dialog returned Null, cancelled");
return;
}
log.debug("New name: {}", newUserName);
if (newUserName.length() == 0) {
log.debug("new user name is empty");
JOptionPane.showMessageDialog(null, Bundle.getMessage("WarningEmptyUserName"),
Bundle.getMessage("WarningTitle"),
JOptionPane.ERROR_MESSAGE);
return;
}
if (newUserName.equals(oldName)) { // name was not changed by user
return;
} else { // check if name is already in use
switch (beanTypeChar) {
case 'T':
nb = jmri.InstanceManager.turnoutManagerInstance().getTurnout(newUserName);
break;
case 'S':
nb = jmri.InstanceManager.sensorManagerInstance().getSensor(newUserName);
break;
case 'L':
nb = jmri.InstanceManager.lightManagerInstance().getLight(newUserName);
break;
default:
log.error("Check userName: cannot parse bean name. userName = {}", newUserName);
return;
}
if (nb != null) {
log.error("User name is not unique {}", newUserName);
String msg = Bundle.getMessage("WarningUserName", newUserName);
JOptionPane.showMessageDialog(null, msg,
Bundle.getMessage("WarningTitle"),
JOptionPane.ERROR_MESSAGE);
return;
}
}
_bname.setUserName(newUserName);
if (oldName == null || oldName.equals("")) {
if (!nbhm.inUse(_switchSysName, _bname)) {
return; // no problem, so stop
}
String msg = Bundle.getMessage("UpdateToUserName", _editor.getSwitchTypeName(), newUserName, _switchSysName);
int optionPane = JOptionPane.showConfirmDialog(null,
msg, Bundle.getMessage("UpdateToUserNameTitle"),
JOptionPane.YES_NO_OPTION);
if (optionPane == JOptionPane.YES_OPTION) {
//This will update the bean reference from the systemName to the userName
try {
nbhm.updateBeanFromSystemToUser(_bname);
} catch (JmriException ex) {
// We should never get an exception here as we already check that the username is not valid
}
}
} else {
nbhm.renameBean(oldName, newUserName, _bname); // will pick up name change in label
}
_editor.updatePressed(); // but we must redraw whole board
}
private boolean inverted = false;
public void setInverted(boolean set) {
inverted = set;
}
public boolean getInverted() {
return inverted;
}
/**
* Invert attached object on the layout, if supported by its connection.
*
* @param set new inverted state, true for inverted, false for normal.
*/
public void setBeanInverted(boolean set) {
switch (beanTypeChar) {
case 'T':
if (getTurnout() != null && getTurnout().canInvert()) { // if supported
this.setInverted(set);
getTurnout().setInverted(set);
}
break;
case 'S':
if (getSensor() != null && getSensor().canInvert()) { // if supported
this.setInverted(set);
getSensor().setInverted(set);
}
break;
case 'L':
// Lights cannot be inverted, so never called
return;
default:
log.error("Invert item: cannot parse bean name. userName = {}", _switchSysName);
}
}
/**
* Process mouseClick on this switch, passing in name for debug.
*
* @param e the event heard
* @param name ID of this button (identical to name of suggested bean
* object)
*/
public void operate(MouseEvent e, String name) {
log.debug("Button {} clicked", name);
//if (!_editor.getFlag(Editor.OPTION_CONTROLS, isControlling())) {
// return;
//}
if (namedBean == null || e == null || e.isMetaDown()) {
return;
}
alternateOnClick();
}
/**
* Process mouseClick on this switch.
* Similar to {@link #operate(MouseEvent, String)}.
*
* @param e the event heard
*/
public void doMouseClicked(java.awt.event.MouseEvent e) {
log.debug("Switch clicked");
//if (!_editor.getFlag(Editor.OPTION_CONTROLS, isControlling())) {
// return;
//}
if (namedBean == null || e == null || e.isMetaDown()) {
return;
}
alternateOnClick();
}
/**
* Change the state of attached Turnout, Light or Sensor on the layout
* unless menu option Panel Items Control Layout is set to off.
*/
void alternateOnClick() {
if (_editor.allControlling()) {
switch (beanTypeChar) {
case 'T': // Turnout
log.debug("T clicked");
if (getTurnout().getKnownState() == jmri.Turnout.CLOSED) // if clear known state, set to opposite
{
getTurnout().setCommandedState(jmri.Turnout.THROWN);
} else if (getTurnout().getKnownState() == jmri.Turnout.THROWN) {
getTurnout().setCommandedState(jmri.Turnout.CLOSED);
} else if (getTurnout().getCommandedState() == jmri.Turnout.CLOSED) {
getTurnout().setCommandedState(jmri.Turnout.THROWN); // otherwise, set to opposite of current commanded state if known
} else {
getTurnout().setCommandedState(jmri.Turnout.CLOSED); // just force Closed
}
break;
case 'L': // Light
log.debug("L clicked");
if (getLight().getState() == jmri.Light.OFF) {
getLight().setState(jmri.Light.ON);
} else {
getLight().setState(jmri.Light.OFF);
}
break;
case 'S': // Sensor
log.debug("S clicked");
try {
if (getSensor().getKnownState() == jmri.Sensor.INACTIVE) {
getSensor().setKnownState(jmri.Sensor.ACTIVE);
} else {
getSensor().setKnownState(jmri.Sensor.INACTIVE);
}
} catch (jmri.JmriException reason) {
log.warn("Exception flipping sensor: {}", (Object) reason);
}
break;
default:
log.error("invalid char in Switchboard Button \"{}\". State not set.", _switchSysName);
}
}
}
/**
* Only for lights. Used for All Off/All On.
* Skips unconnected switch icons.
*
* @param state On = 1, Off = 0
*/
public void switchLight(int state) {
if (namedBean != null) {
getLight().setState(state);
}
}
public void setBackgroundColor(Color bgcolor) {
this.setBackground(bgcolor);
}
JmriJFrame addFrame = null;
JTextField sysNameTextBox = new JTextField(12);
JTextField userName = new JTextField(15);
/**
* Create new bean and connect it to this switch. Use type letter from
* switch label (S, T or L).
*
* @param systemName system name of bean.
*/
protected void connectNew(String systemName) {
log.debug("Request new bean");
userName.setText(""); // only available on unconnected switches, so no useful content yet
// provide etc.
if (addFrame == null) {
addFrame = new JmriJFrame(Bundle.getMessage("ConnectNewMenu", ""), false, true);
addFrame.addHelpMenu("package.jmri.jmrit.display.switchboardEditor.SwitchboardEditor", true);
addFrame.getContentPane().setLayout(new BoxLayout(addFrame.getContentPane(), BoxLayout.Y_AXIS));
ActionListener okListener = this::okAddPressed;
ActionListener cancelListener = this::cancelAddPressed;
AddNewDevicePanel switchConnect = new AddNewDevicePanel(sysNameTextBox, userName, "ButtonOK", okListener, cancelListener);
switchConnect.setSystemNameFieldIneditable(); // prevent user interference with switch label
switchConnect.setOK(); // activate OK button on Add new device pane
addFrame.add(switchConnect);
}
addFrame.pack();
addFrame.setVisible(true);
}
protected void cancelAddPressed(ActionEvent e) {
if (addFrame != null) {
addFrame.setVisible(false);
addFrame.dispose();
addFrame = null;
}
}
protected void okAddPressed(ActionEvent e) {
NamedBean nb;
String manuPrefix = _editor.getSwitchManu();
String user = userName.getText();
if (user.trim().equals("")) {
user = null;
}
// systemName can't be changed, fixed
if (addFrame != null) {
addFrame.setVisible(false);
addFrame.dispose();
addFrame = null;
}
switch (_switchSysName.charAt(manuPrefix.length())) {
case 'T':
Turnout t;
try {
// add turnout to JMRI (w/appropriate manager)
t = InstanceManager.turnoutManagerInstance().provideTurnout(_switchSysName);
t.setUserName(user);
} catch (IllegalArgumentException ex) {
// user input no good
handleCreateException(_switchSysName);
return; // without creating
}
nb = jmri.InstanceManager.turnoutManagerInstance().getTurnout(_switchSysName);
break;
case 'S':
Sensor s;
try {
// add Sensor to JMRI (w/appropriate manager)
s = InstanceManager.sensorManagerInstance().provideSensor(_switchSysName);
s.setUserName(user);
} catch (IllegalArgumentException ex) {
// user input no good
handleCreateException(_switchSysName);
return; // without creating
}
nb = jmri.InstanceManager.sensorManagerInstance().getSensor(_switchSysName);
break;
case 'L':
Light l;
try {
// add Light to JMRI (w/appropriate manager)
l = InstanceManager.lightManagerInstance().provideLight(_switchSysName);
l.setUserName(user);