001package jmri.jmrit.display.configurexml;
002
003import jmri.SignalMast;
004import jmri.configurexml.JmriConfigureXmlException;
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 display.SignalMastIcon objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2010
016 */
017public class SignalMastIconXml extends PositionableLabelXml {
018
019    public SignalMastIconXml() {
020    }
021
022    /**
023     * Default implementation for storing the contents of a SignalMastIcon
024     *
025     * @param o Object to store, of type SignalMastIcon
026     * @return Element containing the complete info
027     */
028    @Override
029    public Element store(Object o) {
030
031        SignalMastIcon p = (SignalMastIcon) o;
032        if (!p.isActive()) {
033            return null;  // if flagged as inactive, don't store
034        }
035        Element element = new Element("signalmasticon");
036        element.setAttribute("signalmast", "" + p.getNamedSignalMast().getName());
037        storeCommonAttributes(p, element);
038        element.setAttribute("clickmode", "" + p.getClickMode());
039        element.setAttribute("litmode", "" + p.getLitMode());
040        element.setAttribute("degrees", String.valueOf(p.getDegrees()));
041        element.setAttribute("scale", String.valueOf(p.getScale()));
042        element.setAttribute("imageset", p.useIconSet());
043        element.setAttribute("class", "jmri.jmrit.display.configurexml.SignalMastIconXml");
044        //storeIconInfo(p, element);
045
046        storeLogixNG_Data(p, element);
047
048        return element;
049    }
050
051    /**
052     * Create a SignalMastIcon, then add
053     *
054     * @param element Top level Element to unpack.
055     * @param o       an Editor as an Object
056     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
057     *                   required by the input XML
058     */
059    @Override
060    public void load(Element element, Object o) throws JmriConfigureXmlException {
061        // create the objects
062        Editor ed = (Editor) o;
063        SignalMastIcon l = new SignalMastIcon(ed);
064        String name;
065
066        Attribute attr;
067        /*
068         * We need to set the rotation and scaling first, prior to setting the
069         * signalmast, otherwise we end up in a situation where by the icons do
070         * not get rotated or scaled correctly.
071         **/
072        try {
073            int rotation = 0;
074            double scale = 1.0;
075            attr = element.getAttribute("rotation");    // former attribute name.
076            if (attr != null) {
077                rotation = attr.getIntValue();
078            }
079            attr = element.getAttribute("degrees");
080            if (attr != null) {
081                rotation = attr.getIntValue();
082            }
083            l.rotate(rotation);
084            attr = element.getAttribute("scale");
085            String text = "Error attr null";
086            if (attr != null) {
087                scale = attr.getDoubleValue();
088                text = attr.getValue();
089            }
090            l.setScale(scale);
091            if (log.isDebugEnabled()) {
092                log.debug("Load SignalMast rotation= {} scale= {} attr text= {}", rotation, scale, text);
093            }
094        } catch (org.jdom2.DataConversionException e) {
095            log.error("failed to convert rotation or scale attribute");
096        }
097        attr = element.getAttribute("signalmast");
098        if (attr == null) {
099            log.error("incorrect information for signal mast; must use signalmast name");
100            ed.loadFailed();
101            return;
102        } else {
103            name = attr.getValue();
104            if (log.isDebugEnabled()) {
105                log.debug("Load SignalMast {}", name);
106            }
107        }
108
109        SignalMast sh = jmri.InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(name);
110
111        if (sh != null) {
112            l.setSignalMast(name);
113        } else {
114            log.error("SignalMast named '{}' not found.", attr.getValue());
115            ed.loadFailed();
116            //    return;
117        }
118
119        attr = element.getAttribute("imageset");
120        if (attr != null) {
121            l.useIconSet(attr.getValue());
122        }
123
124        attr = element.getAttribute("imageset");
125        if (attr != null) {
126            l.useIconSet(attr.getValue());
127        }
128
129        try {
130            attr = element.getAttribute("clickmode");
131            if (attr != null) {
132                l.setClickMode(attr.getIntValue());
133            }
134        } catch (org.jdom2.DataConversionException e) {
135            log.error("Failed on clickmode attribute", e);
136        }
137
138        try {
139            attr = element.getAttribute("litmode");
140            if (attr != null) {
141                l.setLitMode(attr.getBooleanValue());
142            }
143        } catch (org.jdom2.DataConversionException e) {
144            log.error("Failed on litmode attribute", e);
145        }
146
147        try {
148            ed.putItem(l);
149        } catch (Positionable.DuplicateIdException e) {
150            throw new JmriConfigureXmlException("Positionable id is not unique", e);
151        }
152
153        loadLogixNG_Data(l, element);
154
155        // load individual item's option settings after editor has set its global settings
156        loadCommonAttributes(l, Editor.SIGNALS, element);
157    }
158
159    private final static Logger log = LoggerFactory.getLogger(SignalMastIconXml.class);
160
161}