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