Search in sources :

Example 1 with BeanSwitch

use of jmri.jmrit.display.switchboardEditor.SwitchboardEditor.BeanSwitch in project JMRI by JMRI.

the class SwitchboardServlet method getXmlPanel.

@Override
protected String getXmlPanel(String name) {
    log.debug("Getting {} for {}", getPanelType(), name);
    try {
        SwitchboardEditor editor = (SwitchboardEditor) getEditor(name);
        Element panel = new Element("panel");
        JFrame frame = editor.getTargetFrame();
        panel.setAttribute("name", name);
        panel.setAttribute("height", Integer.toString(frame.getContentPane().getHeight()));
        panel.setAttribute("width", Integer.toString(frame.getContentPane().getWidth()));
        panel.setAttribute("panelheight", Integer.toString(editor.getTargetPanel().getHeight()));
        panel.setAttribute("panelwidth", Integer.toString(editor.getTargetPanel().getWidth()));
        // add more properties
        panel.setAttribute("showtooltips", (editor.showTooltip()) ? "yes" : "no");
        panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
        panel.setAttribute("hideunconnected", (editor.hideUnconnected()) ? "yes" : "no");
        panel.setAttribute("rangemin", Integer.toString(editor.getPanelMenuRangeMin()));
        panel.setAttribute("rangemax", Integer.toString(editor.getPanelMenuRangeMax()));
        panel.setAttribute("type", editor.getSwitchType());
        panel.setAttribute("connection", editor.getSwitchManu());
        panel.setAttribute("shape", editor.getSwitchShape());
        panel.setAttribute("columns", Integer.toString(editor.getColumns()));
        panel.setAttribute("defaulttextcolor", editor.getDefaultTextColor());
        log.debug("webserver Switchboard attribs ready");
        Element bgColor = new Element("backgroundColor");
        if (editor.getBackgroundColor() == null) {
            // set to light grey
            bgColor.setAttribute("red", Integer.toString(192));
            bgColor.setAttribute("green", Integer.toString(192));
            bgColor.setAttribute("blue", Integer.toString(192));
        } else {
            bgColor.setAttribute("red", Integer.toString(editor.getBackgroundColor().getRed()));
            bgColor.setAttribute("green", Integer.toString(editor.getBackgroundColor().getGreen()));
            bgColor.setAttribute("blue", Integer.toString(editor.getBackgroundColor().getBlue()));
        }
        panel.addContent(bgColor);
        Element text = new Element("text");
        text.setAttribute("color", editor.getDefaultTextColor());
        text.setAttribute("content", "For now, Switchboards only present buttons in JMRI WebServer.");
        panel.addContent(text);
        // include switches, Bug: how to delete the old ones?
        // call method in SwitchboardEditor
        List<BeanSwitch> _switches = editor.getSwitches();
        log.debug("SwbServlet N switches: {}", _switches.size());
        for (BeanSwitch sub : _switches) {
            if (sub != null) {
                try {
                    Element e = ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        log.debug("element name: {}", e.getName());
                        // }
                        try {
                            e.setAttribute("label", sub.getNameString());
                            e.setAttribute(JSON.ID, sub.getNamedBean().getSystemName());
                            if (sub.getNamedBean() == null) {
                                e.setAttribute("connected", "false");
                                log.debug("switch {} NOT connected", sub.getNameString());
                            } else {
                                // activate click action via class
                                e.setAttribute("connected", "true");
                            }
                        } catch (NullPointerException ex) {
                            if (sub.getNamedBean() == null) {
                                log.debug("{} {} does not have an associated NamedBean", e.getName(), e.getAttribute(JSON.NAME));
                            } else {
                                log.debug("{} {} does not have a SystemName", e.getName(), e.getAttribute(JSON.NAME));
                            }
                        }
                        // read shared attribs
                        e.setAttribute("textcolor", editor.getDefaultTextColor());
                        e.setAttribute("type", editor.getSwitchType());
                        e.setAttribute("connection", editor.getSwitchManu());
                        e.setAttribute("shape", editor.getSwitchShape());
                        e.setAttribute("columns", Integer.toString(editor.getColumns()));
                        // process and add
                        parsePortableURIs(e);
                        panel.addContent(e);
                    }
                } catch (Exception ex) {
                    log.error("Error reading xml panel element: " + ex, ex);
                }
            }
        }
        Document doc = new Document(panel);
        XMLOutputter out = new XMLOutputter();
        out.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM));
        return out.outputString(doc);
    } catch (NullPointerException ex) {
        log.warn("Requested Switchboard [" + name + "] does not exist.");
        return "ERROR Requested Switchboard [" + name + "] does not exist.";
    }
}
Also used : SwitchboardEditor(jmri.jmrit.display.switchboardEditor.SwitchboardEditor) XMLOutputter(org.jdom2.output.XMLOutputter) JFrame(javax.swing.JFrame) Element(org.jdom2.Element) BeanSwitch(jmri.jmrit.display.switchboardEditor.SwitchboardEditor.BeanSwitch) Document(org.jdom2.Document)

Example 2 with BeanSwitch

use of jmri.jmrit.display.switchboardEditor.SwitchboardEditor.BeanSwitch in project JMRI by JMRI.

the class SwitchboardEditor$BeanSwitchXml method store.

/**
     * Default implementation for storing the contents of a BeanSwitch.
     * Used the display Switchboard switches in JMRI web server.
     *
     * @param o Object to store, of type BeanSwitch
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    SwitchboardEditor.BeanSwitch bs = (SwitchboardEditor.BeanSwitch) o;
    Element element = new Element("beanswitch");
    // include attributes
    element.setAttribute("label", bs.getNameString());
    if (bs.getNamedBean() != null) {
        element.setAttribute("connected", "true");
    } else {
        element.setAttribute("connected", "false");
    }
    // get state textual info (only used for shape 'button')
    // includes beanswitch label to operate like SensorIcon
    // never null
    Element textElement = new Element("activeText");
    textElement.setAttribute("text", bs.getActiveText());
    element.addContent(textElement);
    textElement = new Element("inactiveText");
    textElement.setAttribute("text", bs.getInactiveText());
    element.addContent(textElement);
    textElement = new Element("unknownText");
    textElement.setAttribute("text", bs.getUnknownText());
    element.addContent(textElement);
    textElement = new Element("inconsistentText");
    textElement.setAttribute("text", bs.getInconsistentText());
    element.addContent(textElement);
    String txt = bs.getTooltip();
    if (txt != null) {
        Element elem = new Element("tooltip").addContent(txt);
        element.addContent(elem);
    }
    element.setAttribute("class", "jmri.jmrit.display.switchboardEditor.configurexml.SwitchboardEditor$BeanSwitchXml");
    return element;
}
Also used : SwitchboardEditor(jmri.jmrit.display.switchboardEditor.SwitchboardEditor) BeanSwitch(jmri.jmrit.display.switchboardEditor.SwitchboardEditor.BeanSwitch) Element(org.jdom2.Element) BeanSwitch(jmri.jmrit.display.switchboardEditor.SwitchboardEditor.BeanSwitch)

Aggregations

SwitchboardEditor (jmri.jmrit.display.switchboardEditor.SwitchboardEditor)2 BeanSwitch (jmri.jmrit.display.switchboardEditor.SwitchboardEditor.BeanSwitch)2 Element (org.jdom2.Element)2 JFrame (javax.swing.JFrame)1 Document (org.jdom2.Document)1 XMLOutputter (org.jdom2.output.XMLOutputter)1