001package jmri.jmrit.display.configurexml;
002
003import jmri.configurexml.JmriConfigureXmlException;
004import jmri.jmrit.catalog.NamedIcon;
005import jmri.jmrit.display.Editor;
006import jmri.jmrit.display.LocoIcon;
007import jmri.jmrit.roster.Roster;
008import jmri.jmrit.roster.RosterEntry;
009
010import org.jdom2.Element;
011import org.slf4j.Logger;
012import org.slf4j.LoggerFactory;
013
014/**
015 * Handle configuration for display.LocoIcon objects.
016 *
017 * @author Bob Jacobsen Copyright: Copyright (c) 2002
018 */
019public class LocoIconXml extends PositionableLabelXml {
020
021    public LocoIconXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a LocoIcon
026     *
027     * @param o Object to store, of type LocoIcon
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032
033        LocoIcon p = (LocoIcon) o;
034        if (!p.isActive()) {
035            return null;  // if flagged as inactive, don't store
036        }
037        Element element = new Element("locoicon");
038        storeCommonAttributes(p, element);
039
040        // include contents
041        if (p.getUnRotatedText() != null) {
042            element.setAttribute("text", p.getUnRotatedText());
043        }
044        storeTextInfo(p, element);
045        element.setAttribute("icon", "yes");
046        element.setAttribute("dockX", "" + p.getDockX());
047        element.setAttribute("dockY", "" + p.getDockY());
048        element.addContent(storeIcon("icon", (NamedIcon) p.getIcon()));
049        RosterEntry entry = p.getRosterEntry();
050        if (entry != null) {
051            element.setAttribute("rosterentry", entry.getId());
052        }
053
054        storeLogixNG_Data(p, element);
055
056        element.setAttribute("class", "jmri.jmrit.display.configurexml.LocoIconXml");
057
058        return element;
059    }
060
061    /**
062     * Create a PositionableLabel, then add to a target JLayeredPane
063     *
064     * @param element Top level Element to unpack.
065     * @param o       an Editor as an Object
066     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
067     *                   required by the input XML
068     */
069    @Override
070    public void load(Element element, Object o) throws JmriConfigureXmlException {
071        Editor ed = (Editor) o;
072        LocoIcon l = new LocoIcon(ed);
073
074        // create the objects
075        String textName = "error";
076        try {
077            textName = element.getAttribute("text").getValue();
078        } catch (Exception e) {
079            log.error("failed to get loco text attribute", e);
080        }
081        String name = "error";
082        NamedIcon icon;
083        try {
084            name = element.getAttribute("icon").getValue();
085        } catch (Exception e) {
086            log.error("failed to get icon attribute", e);
087        }
088        if (name.equals("yes")) {
089            icon = loadIcon(l, "icon", element, "LocoIcon", ed);
090        } else {
091            icon = NamedIcon.getIconByName(name);
092            if (icon == null) {
093                icon = ed.loadFailed("LocoIcon", name);
094                if (icon == null) {
095                    log.info("LocoIcon icon removed for url= {}", name);
096                    return;
097                }
098            }
099        }
100        l.updateIcon(icon);
101
102        try {
103            int x = element.getAttribute("dockX").getIntValue();
104            int y = element.getAttribute("dockY").getIntValue();
105            l.setDockingLocation(x, y);
106            //           l.dock();
107        } catch (Exception e) {
108            log.warn("failed to get docking location", e);
109        }
110
111        String rosterId = null;
112        try {
113            rosterId = element.getAttribute("rosterentry").getValue();
114            RosterEntry entry = Roster.getDefault().entryFromTitle(rosterId);
115            l.setRosterEntry(entry);
116        } catch (Exception e) {
117            log.debug("no roster entry for {}", rosterId, e);
118        }
119        ed.putLocoIcon(l, textName);
120        // load individual item's option settings after editor has set its global settings
121        loadCommonAttributes(l, Editor.MARKERS, element);
122        loadTextInfo(l, element);
123
124        loadLogixNG_Data(l, element);
125
126        l.init();  // to detect "background" color for use in Tracker, examine icon file
127    }
128
129    private final static Logger log = LoggerFactory.getLogger(LocoIconXml.class);
130}