001package jmri.jmrit.display.configurexml;
002
003import java.awt.Color;
004
005import jmri.configurexml.JmriConfigureXmlException;
006import jmri.jmrit.display.*;
007import jmri.util.ColorUtil;
008
009import org.jdom2.Element;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Handle configuration for display.AnalogClock2Display objects.
015 *
016 * @author Howard G. Penny Copyright (c) 2005
017 */
018public class AnalogClock2DisplayXml
019        extends PositionableLabelXml {
020
021    public AnalogClock2DisplayXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of an AnalogClock2Display
026     *
027     * @param o Object to store, of type TurnoutIcon
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032
033        AnalogClock2Display p = (AnalogClock2Display) o;
034        if (!p.isActive()) {
035            return null; // if flagged as inactive, don't store
036        }
037
038        Element element = new Element("fastclock");
039
040        // include contents
041        if (p.getId() != null) element.setAttribute("id", p.getId());
042        element.setAttribute("x", "" + p.getX());
043        element.setAttribute("y", "" + p.getY());
044        element.setAttribute("scale", "" + p.getScale());
045        element.setAttribute("color", "" + ColorUtil.colorToColorName(p.getColor()));
046        String link = p.getURL();
047        if (link != null && link.trim().length() > 0) {
048            element.setAttribute("link", link);
049        }
050
051        element.setAttribute("class",
052                "jmri.jmrit.display.configurexml.AnalogClock2DisplayXml");
053
054        storeLogixNG_Data(p, element);
055
056        return element;
057    }
058
059    @Override
060    public boolean load(Element shared, Element perNode) {
061        log.error("Invalid method called");
062        return false;
063    }
064
065    /**
066     * Create an AnalogClock2Display, then add to a target JLayeredPane
067     *
068     * @param element Top level Element to unpack.
069     * @param o       an Editor as an Object
070     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
071     *                   required by the input XML
072     */
073    @Override
074    public void load(Element element, Object o) throws JmriConfigureXmlException {
075        // get object class and create the clock object
076        Editor ed = (Editor) o;
077        AnalogClock2Display l = new AnalogClock2Display(ed);
078
079        // find coordinates
080        int x = 0;
081        int y = 0;
082        double scale = 1.0;
083        Color color = Color.black;
084        try {
085            if (element.getAttribute("id") != null) {
086                try {
087                    l.setId(element.getAttribute("id").getValue());
088                } catch (Positionable.DuplicateIdException e) {
089                    throw new JmriConfigureXmlException("Positionable id is not unique", e);
090                }
091            }
092            x = element.getAttribute("x").getIntValue();
093            y = element.getAttribute("y").getIntValue();
094            if (element.getAttribute("scale") != null) {
095                scale = element.getAttribute("scale").getDoubleValue();
096            }
097            try {
098                if (element.getAttribute("color") != null) {
099                    color = ColorUtil.stringToColor(element.getAttribute("color").getValue());
100                }
101            } catch (IllegalArgumentException e) {
102                log.error("Invalid color {}; using black", element.getAttribute("color").getValue());
103            }
104        } catch (org.jdom2.DataConversionException e) {
105            log.error("failed to convert positional attribute");
106        }
107        if (element.getAttribute("link") != null) {
108            l.setULRL(element.getAttribute("link").getValue());
109        }
110        l.setOpaque(false);
111        l.update();
112        l.setLocation(x, y);
113        if (scale != 1.0 && 10.0 > scale && scale > 0.1) {
114            l.setScale(scale);
115        }
116        l.setColor(color);
117
118        // add the clock to the panel
119        l.setDisplayLevel(Editor.CLOCK);
120        try {
121            ed.putItem(l);
122        } catch (Positionable.DuplicateIdException e) {
123            throw new JmriConfigureXmlException("Positionable id is not unique", e);
124        }
125
126        loadLogixNG_Data(l, element);
127    }
128
129    private static final Logger log = LoggerFactory.getLogger(AnalogClock2DisplayXml.class);
130}