001package jmri.jmrit.display.configurexml;
002
003import jmri.configurexml.JmriConfigureXmlException;
004import jmri.jmrit.catalog.NamedIcon;
005import jmri.jmrit.display.*;
006
007import org.jdom2.Attribute;
008import org.jdom2.Element;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Handle configuration for rps.RpsPositionIcon objects
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2006
016 */
017public class RpsPositionIconXml extends PositionableLabelXml {
018
019    public RpsPositionIconXml() {
020    }
021
022    /**
023     * Default implementation for storing the contents of a RpsPositionIcon
024     *
025     * @param o Object to store, of type RpsPositionIcon
026     * @return Element containing the complete info
027     */
028    @Override
029    public Element store(Object o) {
030
031        RpsPositionIcon p = (RpsPositionIcon) o;
032        if (!p.isActive()) {
033            return null;  // if flagged as inactive, don't store
034        }
035        Element element = new Element("sensoricon");
036        storeCommonAttributes(p, element);
037        // include contents
038        element.setAttribute("active", p.getActiveIcon().getURL());
039        element.setAttribute("error", p.getErrorIcon().getURL());
040        element.setAttribute("rotate", String.valueOf(p.getActiveIcon().getRotation()));
041        element.setAttribute("momentary", p.getMomentary() ? "true" : "false");
042
043        element.setAttribute("sxscale", "" + p.getXScale());
044        element.setAttribute("syscale", "" + p.getYScale());
045        element.setAttribute("sxorigin", "" + p.getXOrigin());
046        element.setAttribute("syorigin", "" + p.getYOrigin());
047
048        element.setAttribute("showid", p.isShowID() ? "true" : "false");
049
050        if (p.getFilter() != null) {
051            element.setAttribute("filter", "" + p.getFilter());
052        }
053
054        element.addContent(storeIcon("active", p.getActiveIcon()));
055        element.addContent(storeIcon("error", p.getErrorIcon()));
056
057        storeLogixNG_Data(p, element);
058
059        element.setAttribute("class", "jmri.jmrit.display.configurexml.RpsPositionIconXml");
060        return element;
061    }
062
063    /**
064     * Create a PositionableLabel, then add to a target JLayeredPane
065     *
066     * @param element Top level Element to unpack.
067     * @param o       an Editor as an Object
068     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
069     *                   required by the input XML
070     */
071    @Override
072    public void load(Element element, Object o) throws JmriConfigureXmlException {
073        Editor ed = (Editor) o;
074        RpsPositionIcon l = new RpsPositionIcon(ed);
075
076        // create the objects
077        String name = element.getAttribute("active").getValue();
078        NamedIcon active = NamedIcon.getIconByName(name);
079        if (active == null) {
080            active = ed.loadFailed("RpsPositionIcon: icon \"active\" ", name);
081            if (active == null) {
082                log.info("RpsPositionIcon: icon \"active\" removed for url= {}", name);
083                return;
084            }
085        }
086        l.setActiveIcon(active);
087
088        name = element.getAttribute("error").getValue();
089        NamedIcon error = NamedIcon.getIconByName(name);
090        if (error == null) {
091            error = ed.loadFailed("RpsPositionIcon: icon \"error\" ", name);
092            if (error == null) {
093                log.info("RpsPositionIcon: \"error\" removed for url= {}", name);
094                return;
095            }
096        }
097        l.setErrorIcon(error);
098
099        try {
100            Attribute a = element.getAttribute("rotate");
101            if (a != null) {
102                int rotation = element.getAttribute("rotate").getIntValue();
103                active.setRotation(rotation, l);
104                error.setRotation(rotation, l);
105            }
106        } catch (org.jdom2.DataConversionException e) {
107        }
108
109        Attribute a = element.getAttribute("momentary");
110        if ((a != null) && a.getValue().equals("true")) {
111            l.setMomentary(true);
112        } else {
113            l.setMomentary(false);
114        }
115
116        a = element.getAttribute("showid");
117        if ((a != null) && a.getValue().equals("true")) {
118            l.setShowID(true);
119        } else {
120            l.setShowID(false);
121        }
122
123        a = element.getAttribute("filter");
124        if (a != null) {
125            l.setFilter(a.getValue());
126        }
127
128        double sxScale = 0.;
129        double syScale = 0.;
130        int sxOrigin = 0;
131        int syOrigin = 0;
132        try {
133            sxScale = element.getAttribute("sxscale").getDoubleValue();
134            syScale = element.getAttribute("syscale").getDoubleValue();
135            sxOrigin = element.getAttribute("sxorigin").getIntValue();
136            syOrigin = element.getAttribute("syorigin").getIntValue();
137        } catch (NullPointerException e1) {
138            log.error("missing transform attribute");
139        } catch (org.jdom2.DataConversionException e2) {
140            log.error("failed to convert transform attributes");
141        }
142        l.setTransform(sxScale, syScale, sxOrigin, syOrigin);
143
144        NamedIcon icon = loadIcon(l, "active", element, "RpsPositionIcon ", ed);
145        if (icon != null) {
146            l.setActiveIcon(icon);
147        }
148        icon = loadIcon(l, "error", element, "RpsPositionIcon ", ed);
149        if (icon != null) {
150            l.setErrorIcon(icon);
151        }
152        try {
153            ed.putItem(l);
154        } catch (Positionable.DuplicateIdException e) {
155            throw new JmriConfigureXmlException("Positionable id is not unique", e);
156        }
157
158        loadLogixNG_Data(l, element);
159
160        // load individual item's option settings after editor has set its global settings
161        loadCommonAttributes(l, Editor.SENSORS, element);
162    }
163
164    private final static Logger log = LoggerFactory.getLogger(RpsPositionIconXml.class);
165
166}