001package jmri.jmrit.display.configurexml;
002
003import jmri.configurexml.JmriConfigureXmlException;
004import jmri.jmrit.catalog.NamedIcon;
005import jmri.jmrit.display.Editor;
006import jmri.jmrit.display.LightIcon;
007import jmri.jmrit.display.Positionable;
008
009import org.jdom2.Element;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Handle configuration for display.LightIcon objects.
015 *
016 * @author Pete Cressman Copyright: Copyright (c) 2011
017 */
018public class LightIconXml extends PositionableLabelXml {
019
020    public LightIconXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a LightIcon
025     *
026     * @param o Object to store, of type LightIcon
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031
032        LightIcon p = (LightIcon) o;
033        if (!p.isActive()) {
034            return null;  // if flagged as inactive, don't store
035        }
036        Element element = new Element("LightIcon");
037        element.setAttribute("light", p.getLight().getSystemName());
038        storeCommonAttributes(p, element);
039
040        Element elem = new Element("icons");
041        elem.addContent(storeIcon("on", p.getOnIcon()));
042        elem.addContent(storeIcon("off", p.getOffIcon()));
043        elem.addContent(storeIcon("unknown", p.getUnknownIcon()));
044        elem.addContent(storeIcon("inconsistent", p.getInconsistentIcon()));
045        element.addContent(elem);
046
047        storeLogixNG_Data(p, element);
048
049        element.setAttribute("class", "jmri.jmrit.display.configurexml.LightIconXml");
050
051        return element;
052    }
053
054    /**
055     * Create a PositionableLabel, then add to a target JLayeredPane
056     *
057     * @param element Top level Element to unpack.
058     * @param o       Editor as an Object
059     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
060     *                   required by the input XML
061     */
062    @Override
063    public void load(Element element, Object o) throws JmriConfigureXmlException {
064        // create the objects
065        Editor p = (Editor) o;
066
067        LightIcon l = new LightIcon(p);
068
069        String name;
070        try {
071            name = element.getAttribute("light").getValue();
072        } catch (NullPointerException e) {
073            log.error("incorrect information for light; must use light name");
074            p.loadFailed();
075            return;
076        }
077        l.setLight(name);
078
079        Element icons = element.getChild("icons");
080        if (icons == null) {
081            if (log.isDebugEnabled()) {
082                log.debug("Main element of Light {}has no icons", name);
083            }
084        } else {
085            NamedIcon icon = loadIcon(l, "on", icons, "LightIcon \"" + name + "\": icon \"on\" ", p);
086            if (icon != null) {
087                l.setOnIcon(icon);
088            } else {
089                log.info("LightIcon \"{}\": icon \"on\" removed", name);
090                return;
091            }
092            icon = loadIcon(l, "off", icons, "LightIcon \"" + name + "\": icon \"off\" ", p);
093            if (icon != null) {
094                l.setOffIcon(icon);
095            } else {
096                log.info("LightIcon \"{}\": icon \"off\" removed", name);
097                return;
098            }
099            icon = loadIcon(l, "unknown", icons, "LightIcon \"" + name + "\": icon \"unknown\" ", p);
100            if (icon != null) {
101                l.setUnknownIcon(icon);
102            } else {
103                log.info("LightIcon \"{}\": icon \"unknown\" removed", name);
104                return;
105            }
106            icon = loadIcon(l, "inconsistent", icons, "LightIcon \"" + name + "\": icon \"inconsistent\" ", p);
107            if (icon != null) {
108                l.setInconsistentIcon(icon);
109            } else {
110                log.info("LightIcon \"{}\": icon \"inconsistent\" removed", name);
111                return;
112            }
113        }
114
115        try {
116            p.putItem(l);
117        } catch (Positionable.DuplicateIdException e) {
118            // This should never happen
119            log.error("Editor.putItem() with null id has thrown DuplicateIdException", e);
120        }
121
122        loadLogixNG_Data(l, element);
123
124        // load individual item's option settings after editor has set its global settings
125        loadCommonAttributes(l, Editor.LIGHTS, element);
126    }
127
128    private final static Logger log = LoggerFactory.getLogger(LightIconXml.class);
129}