/
MemoryIcon.java
236 lines (217 loc) · 7.93 KB
/
MemoryIcon.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
package jmri.jmrit.display.layoutEditor;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import jmri.jmrit.catalog.NamedIcon;
import jmri.jmrit.roster.RosterEntry;
import jmri.Reportable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An icon to display a status of a Memory.
*
* This is the same name as display.MemoryIcon, but a very
* separate class. That's not good. Unfortunately, it's too
* hard to disentangle that now because it's resident in the
* panel file that have been written out, so we just annotated
* the fact, but now we want to leave it on the list to fix.
*/
@SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS", justification="Cannot rename for user data compatiblity reasons.")
public class MemoryIcon extends jmri.jmrit.display.MemoryIcon {
private String defaultText = " ";
public MemoryIcon(String s, LayoutEditor panel) {
super(s, panel);
log.debug("MemoryIcon ctor= " + MemoryIcon.class.getName());
}
@Override
public void setText(String text) {
if (text == null || text.isEmpty()) {
super.setText(defaultText);
} else {
super.setText(text);
}
}
private transient LayoutBlock lBlock = null;
public LayoutBlock getLayoutBlock() {
return lBlock;
}
public void setLayoutBlock(LayoutBlock lb) {
lBlock = lb;
}
@Override
public void displayState() {
log.debug("displayState");
if (getMemory() == null) { // use default if not connected yet
setText(defaultText);
updateSize();
return;
}
if (re != null) {
jmri.InstanceManager.throttleManagerInstance().removeListener(re.getDccLocoAddress(), this);
re = null;
}
Object key = getMemory().getValue();
if (key != null) {
java.util.HashMap<String, NamedIcon> map = getMap();
if (map == null) {
// no map, attempt to show object directly
Object val = key;
if (val instanceof jmri.jmrit.roster.RosterEntry) {
jmri.jmrit.roster.RosterEntry roster = (jmri.jmrit.roster.RosterEntry) val;
val = updateIconFromRosterVal(roster);
flipRosterIcon = false;
if (val == null) {
return;
}
}
if (val instanceof String) {
if (((String)val).isEmpty()) {
setText(defaultText);
} else {
setText((String) val);
}
setIcon(null);
_text = true;
_icon = false;
setAttributes(getPopupUtility(), this);
updateSize();
return;
} else if (val instanceof javax.swing.ImageIcon) {
setIcon((javax.swing.ImageIcon) val);
setText(null);
_text = false;
_icon = true;
updateSize();
return;
} else if (val instanceof Number) {
setText(val.toString());
setIcon(null);
_text = true;
_icon = false;
updateSize();
return;
} else if (val instanceof Reportable) {
setText(((Reportable)val).toReportString());
setIcon(null);
_text = true;
_icon = false;
updateSize();
return;
} else {
log.warn("can't display current value of " + getNamedMemory().getName()
+ ", val= " + val + " of Class " + val.getClass().getName());
}
} else {
// map exists, use it
NamedIcon newicon = map.get(key.toString());
if (newicon != null) {
setText(null);
super.setIcon(newicon);
_text = false;
_icon = true;
updateSize();
return;
} else {
// no match, use default
setIcon(getDefaultIcon());
setText(null);
_text = false;
_icon = true;
updateSize();
}
}
} else {
setIcon(null);
setText(defaultText);
_text = true;
_icon = false;
updateSize();
}
}
private JCheckBoxMenuItem updateBlockItem = new JCheckBoxMenuItem("Update Block Details");
@Override
public boolean showPopUp(JPopupMenu popup) {
if (isEditable()) {
popup.add(updateBlockItem);
updateBlockItem.setSelected(updateBlockValueOnChange());
updateBlockItem.addActionListener((java.awt.event.ActionEvent e) -> {
updateBlockValueOnChange(updateBlockItem.isSelected());
});
} // end of selectable
return super.showPopUp(popup);
}
@Override
public void setMemory(String pName) {
super.setMemory(pName);
lBlock = jmri.InstanceManager.getDefault(LayoutBlockManager.class).getBlockWithMemoryAssigned(getMemory());
}
@Override
protected void setValue(Object obj) {
if (updateBlockValue && lBlock != null) {
lBlock.getBlock().setValue(obj);
} else {
getMemory().setValue(obj);
updateSize();
}
}
@Override
protected void addRosterToIcon(RosterEntry roster) {
if (!jmri.InstanceManager.getDefault(LayoutBlockManager.class).isAdvancedRoutingEnabled() || lBlock == null) {
super.addRosterToIcon(roster);
return;
}
int paths = lBlock.getNumberOfThroughPaths();
jmri.Block srcBlock = null;
jmri.Block desBlock = null;
for (int i = 0; i < paths; i++) {
if (lBlock.isThroughPathActive(i)) {
srcBlock = lBlock.getThroughPathSource(i);
desBlock = lBlock.getThroughPathDestination(i);
break;
}
}
int dirA;
int dirB;
if (srcBlock != null && desBlock != null) {
dirA = lBlock.getNeighbourDirection(srcBlock);
dirB = lBlock.getNeighbourDirection(desBlock);
} else {
dirA = jmri.Path.EAST;
dirB = jmri.Path.WEST;
}
Object[] options = {"Facing " + jmri.Path.decodeDirection(dirB),
"Facing " + jmri.Path.decodeDirection(dirA),
"Do Not Add"};
int n = JOptionPane.showOptionDialog(this,
"Would you like to assign loco "
+ roster.titleString() + " to this location",
"Assign Loco",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[2]);
if (n == 2) {
return;
}
if (n == 0) {
flipRosterIcon = true;
if (updateBlockValue) {
lBlock.getBlock().setDirection(dirB);
}
} else {
flipRosterIcon = false;
if (updateBlockValue) {
lBlock.getBlock().setDirection(dirA);
}
}
if (getMemory().getValue() == roster) {
//No change in the loco but a change in direction facing might have occurred
updateIconFromRosterVal(roster);
} else {
setValue(roster);
}
}
private final static Logger log = LoggerFactory.getLogger(MemoryIcon.class);
}