001package jmri.web.servlet.panel;
002
003import java.util.List;
004import javax.servlet.annotation.WebServlet;
005import javax.servlet.http.HttpServlet;
006import javax.swing.JFrame;
007import jmri.configurexml.ConfigXmlManager;
008import jmri.jmrit.display.switchboardEditor.SwitchboardEditor;
009import jmri.jmrit.display.switchboardEditor.BeanSwitch;
010import jmri.server.json.JSON;
011import org.jdom2.Document;
012import org.jdom2.Element;
013import org.jdom2.output.Format;
014import org.jdom2.output.XMLOutputter;
015import org.openide.util.lookup.ServiceProvider;
016import org.slf4j.Logger;
017import org.slf4j.LoggerFactory;
018
019/**
020 * Return xml (for specified SwitchBoard) suitable for use by external clients.
021 * <p>
022 * See JMRI Web Server - Panel Servlet Help in help/en/html/web/PanelServlet.shtml for an example description of
023 * the interaction between the Web Servlets, the Web Browser and the JMRI application.
024 *
025 * @author Egbert Broerse (C) 2017, 2020 -- based on ControlPanelServlet.java by Randall Wood
026 */
027@WebServlet(name = "SwitchboardServlet",
028        urlPatterns = {"/panel/Switchboard"})
029@ServiceProvider(service = HttpServlet.class)
030public class SwitchboardServlet extends AbstractPanelServlet {
031
032    @Override
033    protected String getPanelType() {
034        return "Switchboard";
035    }
036
037    @Override
038    protected String getXmlPanel(String name) {
039        log.debug("Getting {} for {}", getPanelType(), name);
040        SwitchboardEditor editor = (SwitchboardEditor) getEditor(name);
041        if (editor == null) {
042            log.warn("Requested Switchboard [{}] does not exist.", name);
043            return "ERROR Requested panel [" + name + "] does not exist.";
044        }
045
046        Element panel = new Element("panel");
047
048        JFrame frame = editor.getTargetFrame();
049
050        panel.setAttribute("name", name);
051        panel.setAttribute("paneltype", getPanelType());
052        panel.setAttribute("height", Integer.toString(frame.getContentPane().getHeight()));
053        panel.setAttribute("width", Integer.toString(frame.getContentPane().getWidth()));
054        panel.setAttribute("panelheight", Integer.toString(editor.getTargetPanel().getHeight()));
055        panel.setAttribute("panelwidth", Integer.toString(editor.getTargetPanel().getWidth()));
056        // add more properties
057        panel.setAttribute("showtooltips", (editor.showToolTip()) ? "yes" : "no");
058        panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
059
060        panel.setAttribute("hideunconnected", (editor.hideUnconnected()) ? "yes" : "no");
061        panel.setAttribute("rangemin", Integer.toString(editor.getPanelMenuRangeMin()));
062        panel.setAttribute("rangemax", Integer.toString(editor.getPanelMenuRangeMax()));
063        panel.setAttribute("type", editor.getSwitchType());
064        panel.setAttribute("connection", editor.getSwitchManu());
065        panel.setAttribute("shape", editor.getSwitchShape());
066        panel.setAttribute("rows", Integer.toString(editor.getRows()));
067        panel.setAttribute("total", Integer.toString(editor.getTotal()));
068        panel.setAttribute("showusername", editor.showUserName());
069
070        panel.setAttribute("defaulttextcolor", editor.getDefaultTextColor());
071        panel.setAttribute("activecolor", editor.getActiveSwitchColor());
072        panel.setAttribute("inactivecolor", editor.getInactiveSwitchColor());
073        log.debug("webserver Switchboard panel attribs ready");
074
075        Element color = new Element("backgroundColor");
076        if (editor.getBackgroundColor() == null) {
077            color.setAttribute("red", Integer.toString(192));
078            color.setAttribute("green", Integer.toString(192));
079            color.setAttribute("blue", Integer.toString(192));
080        } else {
081            color.setAttribute("red", Integer.toString(editor.getBackgroundColor().getRed()));
082            color.setAttribute("green", Integer.toString(editor.getBackgroundColor().getGreen()));
083            color.setAttribute("blue", Integer.toString(editor.getBackgroundColor().getBlue()));
084        }
085        panel.addContent(color);
086
087
088        // include switches
089        List<BeanSwitch> _switches = editor.getSwitches(); // call method in SwitchboardEditor
090        log.debug("SwbServlet contains {} switches", _switches.size());
091        for (BeanSwitch sub : _switches) {
092            if (sub != null) {
093                try {
094                    Element e = ConfigXmlManager.elementFromObject(sub);
095                    if (e != null) {
096                        log.debug("element name: {}", e.getName());
097                        e.setAttribute("label", sub.getNameString()); // either the system name or the label
098                        e.setAttribute("connected", "false");
099                        if (sub.getNamedBean() == null) { // skip unconnected switch
100                            log.debug("switch {} NOT connected", sub.getNameString()); // use label instead
101                        } else {
102                            try {
103                                e.setAttribute(JSON.ID, sub.getNamedBean().getSystemName());
104                                e.setAttribute("connected", "true"); // activate click action via class
105                            } catch (NullPointerException ex) {
106                                log.debug("{} {} does not have a SystemName", e.getName(), e.getAttribute(JSON.NAME));
107                            }
108                        }
109                        // read shared attribs to use in beanswitch
110                        e.setAttribute("textcolor", editor.getDefaultTextColor());
111                        e.setAttribute("type", editor.getSwitchType());
112                        e.setAttribute("connection", editor.getSwitchManu());
113                        e.setAttribute("shape", editor.getSwitchShape());
114                        // process and add
115                        parsePortableURIs(e);
116                        panel.addContent(e);
117                    }
118                } catch (Exception ex) {
119                    log.error("Error reading xml panel element: {}", ex, ex);
120                }
121            }
122        }
123
124        Document doc = new Document(panel);
125        XMLOutputter out = new XMLOutputter();
126        out.setFormat(Format.getPrettyFormat()
127                .setLineSeparator(System.getProperty("line.separator"))
128                .setTextMode(Format.TextMode.TRIM));
129
130        return out.outputString(doc);
131    }
132
133    @Override
134    protected String getJsonPanel(String name) {
135        // TODO Auto-generated method stub
136        return "ERROR JSON support not implemented";
137    }
138
139    private final static Logger log = LoggerFactory.getLogger(SwitchboardServlet.class);
140
141}