/
LayoutPanelServlet.java
292 lines (266 loc) · 12.3 KB
/
LayoutPanelServlet.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
package jmri.web.servlet.panel;
import java.awt.Color;
import java.util.List;
import javax.annotation.Nonnull;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import jmri.*;
import jmri.jmrit.display.Positionable;
import jmri.jmrit.display.layoutEditor.*;
import jmri.util.ColorUtil;
import org.jdom2.*;
import org.jdom2.output.*;
import org.openide.util.lookup.ServiceProvider;
import org.slf4j.*;
/**
* Return xml (for specified LayoutPanel) suitable for use by external clients.
* <p>
* See JMRI Web Server - Panel Servlet Help in help/en/html/web/PanelServlet.shtml for an example description of
* the interaction between the Web Servlets, the Web Browser and the JMRI application.
*
* @author mstevetodd -- based on PanelServlet.java by Randall Wood
*/
@WebServlet(name = "LayoutPanelServlet",
urlPatterns = {"/panel/Layout"})
@ServiceProvider(service = HttpServlet.class)
public class LayoutPanelServlet extends AbstractPanelServlet {
private final static Logger log = LoggerFactory.getLogger(LayoutPanelServlet.class);
@Override
protected String getPanelType() {
return "LayoutPanel";
}
@Override
protected String getXmlPanel(String name) {
log.debug("Getting {} for {}", getPanelType(), name);
LayoutEditor editor = (LayoutEditor) getEditor(name);
if (editor == null) {
log.warn("Requested LayoutPanel [{}] does not exist.", name);
return "ERROR Requested panel [" + name + "] does not exist.";
}
Element panel = new Element("panel");
panel.setAttribute("name", name);
panel.setAttribute("paneltype", getPanelType());
panel.setAttribute("height", Integer.toString(editor.gContext.getLayoutHeight()));
panel.setAttribute("width", Integer.toString(editor.gContext.getLayoutWidth()));
panel.setAttribute("panelheight", Integer.toString(editor.gContext.getLayoutHeight()));
panel.setAttribute("panelwidth", Integer.toString(editor.gContext.getLayoutWidth()));
panel.setAttribute("showtooltips", (editor.showToolTip()) ? "yes" : "no");
panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
panel.setAttribute("xscale", Float.toString((float) editor.gContext.getXScale()));
panel.setAttribute("yscale", Float.toString((float) editor.gContext.getYScale()));
panel.setAttribute("mainlinetrackwidth", Integer.toString(editor.gContext.getMainlineTrackWidth()));
panel.setAttribute("sidetrackwidth", Integer.toString(editor.gContext.getSidelineTrackWidth()));
panel.setAttribute("mainlineblockwidth", Integer.toString(editor.gContext.getMainlineBlockWidth()));
panel.setAttribute("sidetrackblockwidth", Integer.toString(editor.gContext.getSidelineBlockWidth()));
panel.setAttribute("turnoutcircles", (editor.getTurnoutCircles()) ? "yes" : "no");
panel.setAttribute("turnoutcirclesize", Integer.toString(editor.getTurnoutCircleSize()));
panel.setAttribute("turnoutdrawunselectedleg", (editor.isTurnoutDrawUnselectedLeg()) ? "yes" : "no");
if (editor.getBackgroundColor() == null) {
panel.setAttribute("backgroundcolor", ColorUtil.colorToColorName(Color.lightGray));
} else {
panel.setAttribute("backgroundcolor", ColorUtil.colorToColorName(editor.getBackgroundColor()));
}
panel.setAttribute("defaulttrackcolor", editor.getDefaultTrackColor());
panel.setAttribute("defaultoccupiedtrackcolor", editor.getDefaultOccupiedTrackColor());
panel.setAttribute("defaultalternativetrackcolor", editor.getDefaultAlternativeTrackColor());
panel.setAttribute("defaulttextcolor", editor.getDefaultTextColor());
panel.setAttribute("turnoutcirclecolor", editor.getTurnoutCircleColor());
panel.setAttribute("turnoutcirclethrowncolor", editor.getTurnoutCircleThrownColor());
panel.setAttribute("turnoutfillcontrolcircles", (editor.isTurnoutFillControlCircles()) ? "yes" : "no");
// include positionable elements
List<Positionable> contents = editor.getContents();
log.debug("Number of positionable elements: {}", contents.size());
for (Positionable sub : contents) {
if (sub != null) {
try {
panel.addContent(positionableElement(sub));
} catch (Exception ex) {
log.error("Error storing panel positionable element: {}", ex);
}
}
}
// include LayoutBlocks
LayoutBlockManager lbm = InstanceManager.getDefault(LayoutBlockManager.class);
java.util.Iterator<LayoutBlock> iter = lbm.getNamedBeanSet().iterator();
SensorManager sm = InstanceManager.sensorManagerInstance();
int num = 0;
while (iter.hasNext()) {
LayoutBlock b = iter.next();
if (b == null) {
log.error("LayoutBlock null during LayoutBlock store");
continue;
}
if (b.getUseCount() > 0) {
// save only those LayoutBlocks that are in use--skip abandoned ones
Element elem = new Element("layoutblock").setAttribute("systemName", b.getSystemName());
String uname = b.getUserName();
if (uname != null && !uname.isEmpty()) {
elem.setAttribute("username", uname);
}
// get occupancy sensor from layoutblock if it is valid
if (!b.getOccupancySensorName().isEmpty()) {
Sensor s = sm.getSensor(b.getOccupancySensorName());
if (s != null) {
elem.setAttribute("occupancysensor", s.getSystemName()); //send systemname
}
//if layoutblock has no occupancy sensor, use one from block, if it is populated
} else {
if (b.getBlock() != null) {
Sensor s = b.getBlock().getSensor();
if (s != null) {
elem.setAttribute("occupancysensor", s.getSystemName()); //send systemname
}
}
}
elem.setAttribute("occupiedsense", Integer.toString(b.getOccupiedSense()));
elem.setAttribute("trackcolor", ColorUtil.colorToColorName(b.getBlockTrackColor()));
elem.setAttribute("occupiedcolor", ColorUtil.colorToColorName(b.getBlockOccupiedColor()));
elem.setAttribute("extracolor", ColorUtil.colorToColorName(b.getBlockExtraColor()));
if (!b.getMemoryName().isEmpty()) {
elem.setAttribute("memory", b.getMemory().getSystemName());
}
if (!b.useDefaultMetric()) {
elem.addContent(new Element("metric").addContent(Integer.toString(b.getBlockMetric())));
}
//add to the panel xml
panel.addContent(elem);
num++;
}
}
log.debug("Number of layoutblock elements: {}", num);
// include LayoutTrackViews
List<LayoutTrackView> layoutTrackViews = editor.getLayoutTrackViews();
log.debug("Number of LayoutTrack elements: {}", layoutTrackViews.size());
for (Object sub : layoutTrackViews) {
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
replaceUserNames(e);
if (sub instanceof LayoutTurntable) {
List<Element> raytracks = e.getChildren("raytrack");
for (Element raytrack : raytracks) {
replaceUserNameAttribute(raytrack, "turnout", "turnout");
}
}
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel LayoutTrack element: {}", e);
}
}
// include LayoutShapes
List<LayoutShape> layoutShapes = editor.getLayoutShapes();
for (Object sub : layoutShapes) {
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel LayoutShape element: {}", e);
}
}
log.debug("Number of LayoutShape elements: {}", layoutShapes.size());
//write out formatted document
Document doc = new Document(panel);
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat()
.setLineSeparator(System.getProperty("line.separator"))
.setTextMode(Format.TextMode.TRIM));
return fmt.outputString(doc);
}
/**
* Replace userName value of attrName with systemName for type attrType.
*
* @param e element to be updated
* @param beanType bean type to use for userName lookup
* @param attrName attribute name to replace
*
*/
private void replaceUserNameAttribute(@Nonnull Element e, @Nonnull String beanType, @Nonnull String attrName) {
String sn = "";
Attribute a = e.getAttribute(attrName);
if (a == null) {
return;
}
String un = a.getValue();
switch (beanType) {
case "turnout":
Turnout t = InstanceManager.getDefault(TurnoutManager.class).getTurnout(un);
if (t == null) {
return;
}
sn = t.getSystemName();
break;
case "layoutBlock":
LayoutBlock lb = InstanceManager.getDefault(LayoutBlockManager.class).getLayoutBlock(un);
if (lb == null) {
return;
}
sn = lb.getSystemName();
break;
default:
return;
}
if (!un.equals(sn)) {
a.setValue(sn);
log.debug("systemName '{}' replaced userName '{}' for {}", sn, un, attrName);
}
}
/**
* Replace child element value of attrName with systemName for type attrType.
*
* @param e element to be updated
* @param beanType bean type to use for userName lookup
* @param childName child element name whose text will be replaced
*
*/
private void replaceUserNameChild(@Nonnull Element e, @Nonnull String beanType, @Nonnull String childName) {
String sn = "";
Element c = e.getChild(childName);
if (c == null) {
return;
}
String un = c.getText();
switch (beanType) {
case "turnout":
Turnout t = InstanceManager.getDefault(TurnoutManager.class).getTurnout(un);
if (t == null) {
return;
}
sn = t.getSystemName();
break;
default:
return;
}
if (!un.equals(sn)) {
c.setText(sn);
log.debug("systemName '{}' replaced userName '{}' for {}", sn, un, childName);
}
}
/**
* Update the element replacing username with systemname for known
* attributes and children.
*
* @param e element to be updated
*/
private void replaceUserNames(@Nonnull Element e) {
replaceUserNameAttribute(e, "turnout", "turnoutname");
replaceUserNameAttribute(e, "turnout", "secondturnoutname");
// block names for turnouts
replaceUserNameAttribute(e, "layoutBlock", "blockname");
replaceUserNameAttribute(e, "layoutBlock", "blockbname");
replaceUserNameAttribute(e, "layoutBlock", "blockcname");
replaceUserNameAttribute(e, "layoutBlock", "blockdname");
// block names for level crossings
replaceUserNameAttribute(e, "layoutBlock", "blocknameac");
replaceUserNameAttribute(e, "layoutBlock", "blocknamebd");
replaceUserNameChild(e, "turnout", "turnout");
replaceUserNameChild(e, "turnout", "turnoutB");
}
@Override
protected String getJsonPanel(String name) {
// TODO Auto-generated method stub
return "ERROR JSON support not implemented";
}
}