001package jmri.jmrit.display.switchboardEditor.configurexml;
002
003import jmri.configurexml.AbstractXmlAdapter;
004import jmri.jmrit.display.switchboardEditor.BeanSwitch;
005import org.jdom2.Element;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Handle configuration for display.switchboard.BeanSwitch objects.
011 *
012 * @author Egbert Broerse Copyright: (c) 2017, 2020
013 */
014public class BeanSwitchXml extends AbstractXmlAdapter {
015
016    public BeanSwitchXml() {
017    }
018
019    /**
020     * Default implementation for storing the contents of a BeanSwitch.
021     * Used to display Switchboard switches in JMRI web server.
022     *
023     * @param o Object to store, of type BeanSwitch
024     * @return Element containing the complete info
025     */
026    @Override
027    public Element store(Object o) {
028        BeanSwitch bs = (BeanSwitch) o;
029        Element element = new Element("beanswitch");
030        // include attributes
031        element.setAttribute("label", bs.getNameString());
032        element.setAttribute("username", bs.getUserNameString());
033        if (bs.getNamedBean() != null) {
034            element.setAttribute("connected", "true");
035        } else {
036            element.setAttribute("connected", "false");
037        }
038        // get state textual info (only used for shape 'button')
039        // includes beanswitch label to operate like SensorIcon
040        // never null
041        Element textElement = new Element("activeText");
042        textElement.setAttribute("text", bs.getActiveText());
043        element.addContent(textElement);
044        textElement = new Element("inactiveText");
045        textElement.setAttribute("text", bs.getInactiveText());
046        element.addContent(textElement);
047        textElement = new Element("unknownText");
048        textElement.setAttribute("text", bs.getUnknownText());
049        element.addContent(textElement);
050        textElement = new Element("inconsistentText");
051        textElement.setAttribute("text", bs.getInconsistentText());
052        element.addContent(textElement);
053        String txt = bs.getToolTip();
054        if (txt != null) {
055            Element elem = new Element("tooltip").addContent(txt);
056            element.addContent(elem);
057        }
058        //element.setAttribute("shape", Integer.toString(bs.getShape())); // 2 = drawing, 0 = button = default
059        // is same for all switches, get from SwitchboardEditor editor
060        element.setAttribute("class", BeanSwitchXml.class.getName());
061        return element;
062    }
063
064    /**
065     * Load, starting with the BeanSwitch element, then all the value-icon
066     * pairs. Not currently used because BeanSwitches are auto-generated from SwitchBoard settings.
067     *
068     * @param element Top level Element to unpack.
069     * @param o       an Editor as an Object
070     */
071    @Override
072    public void load(Element element, Object o) {
073        // create the objects
074        //BeanSwitch bs = (BeanSwitch) o;
075        log.debug("load xml called");
076    }
077
078    private static final Logger log = LoggerFactory.getLogger(BeanSwitchXml.class);
079
080}