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 display.LinkingLabel objects
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2013
016 */
017public class LinkingLabelXml extends PositionableLabelXml {
018
019    public LinkingLabelXml() {
020        super();
021    }
022
023    /**
024     * Default implementation for storing the contents of a LinkingLabel
025     *
026     * @param o Object to store, of type LinkingLabel
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        LinkingLabel p = (LinkingLabel) o;
032
033        if (!p.isActive()) {
034            return null;  // if flagged as inactive, don't store
035        }
036        Element element = new Element("linkinglabel");
037        storeCommonAttributes(p, element);
038
039        if (p.isText()) {
040            if (p.getUnRotatedText() != null) {
041                element.setAttribute("text", p.getUnRotatedText());
042            }
043            storeTextInfo(p, element);
044        }
045
046        if (p.isIcon() && p.getIcon() != null) {
047            element.setAttribute("icon", "yes");
048            element.addContent(storeIcon("icon", (NamedIcon) p.getIcon()));
049        }
050
051        element.addContent(new Element("url").addContent(p.getURL()));
052
053        storeLogixNG_Data(p, element);
054
055        element.setAttribute("class", "jmri.jmrit.display.configurexml.LinkingLabelXml");
056        return element;
057    }
058
059    /**
060     * Create LinkingLabel, then add to a target JLayeredPane
061     *
062     * @param element Top level Element to unpack.
063     * @param o       Editor as an Object
064     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
065     *                   required by the input XML
066     */
067    @Override
068    public void load(Element element, Object o) throws JmriConfigureXmlException {
069        // create the objects
070        LinkingLabel l;
071
072        String url = element.getChild("url").getText();
073
074        // get object class and determine editor being used
075        Editor editor = (Editor) o;
076        if (element.getAttribute("icon") != null) {
077            NamedIcon icon;
078            String name = element.getAttribute("icon").getValue();
079//            if (log.isDebugEnabled()) log.debug("icon attribute= "+name);
080            if (name.equals("yes")) {
081                icon = getNamedIcon("icon", element, "LinkingLabel ", editor);
082            } else {
083                icon = NamedIcon.getIconByName(name);
084                if (icon == null) {
085                    icon = editor.loadFailed("LinkingLabel", name);
086                    if (icon == null) {
087                        log.info("LinkingLabel icon removed for url= {}", name);
088                        return;
089                    }
090                }
091            }
092            // abort if name != yes and have null icon
093            if (icon == null && !name.equals("yes")) {
094                log.info("LinkingLabel icon removed for url= {}", name);
095                return;
096            }
097            l = new LinkingLabel(icon, editor, url);
098            l.setPopupUtility(null);        // no text
099            try {
100                Attribute a = element.getAttribute("rotate");
101                if (a != null && icon != null) {
102                    int rotation = element.getAttribute("rotate").getIntValue();
103                    icon.setRotation(rotation, l);
104                }
105            } catch (org.jdom2.DataConversionException e) {
106            }
107
108            if (name.equals("yes")) {
109                NamedIcon nIcon = loadIcon(l, "icon", element, "LinkingLabel ", editor);
110                if (nIcon != null) {
111                    l.updateIcon(nIcon);
112                } else {
113                    log.info("LinkingLabel icon removed for url= {}", name);
114                    return;
115                }
116            } else {
117                l.updateIcon(icon);
118            }
119
120            //l.setSize(l.getPreferredSize().width, l.getPreferredSize().height);
121        } else if (element.getAttribute("text") != null) {
122            l = new LinkingLabel(element.getAttribute("text").getValue(), editor, url);
123            loadTextInfo(l, element);
124
125        } else {
126            log.error("LinkingLabel is null!");
127            if (log.isDebugEnabled()) {
128                java.util.List<Attribute> attrs = element.getAttributes();
129                log.debug("\tElement Has {} Attributes:", attrs.size());
130                for (Attribute a : attrs) {
131                    log.debug("\tattribute:\t{} = {}", a.getName(), a.getValue());
132                }
133                java.util.List<Element> kids = element.getChildren();
134                log.debug("\tElementHas {} children:", kids.size());
135                for (Element e : kids) {
136                    log.debug("\tchild:\t{} = \"{}\"", e.getName(), e.getValue());
137                }
138            }
139            editor.loadFailed();
140            return;
141        }
142        try {
143            editor.putItem(l);
144        } catch (Positionable.DuplicateIdException e) {
145            throw new JmriConfigureXmlException("Positionable id is not unique", e);
146        }
147
148        loadLogixNG_Data(l, element);
149
150        // load individual item's option settings after editor has set its global settings
151        loadCommonAttributes(l, Editor.LABELS, element);
152    }
153
154    private final static Logger log = LoggerFactory.getLogger(LinkingLabelXml.class);
155}