001package jmri.jmrit.display.configurexml;
002
003import jmri.Memory;
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.MemorySpinnerIcon objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2009
016 */
017public class MemoryInputIconXml extends PositionableLabelXml {
018
019    public MemoryInputIconXml() {
020    }
021
022    /**
023     * Default implementation for storing the contents of a MemorySpinnerIcon
024     *
025     * @param o Object to store, of type MemorySpinnerIcon
026     * @return Element containing the complete info
027     */
028    @Override
029    public Element store(Object o) {
030
031        MemoryInputIcon p = (MemoryInputIcon) o;
032
033        Element element = new Element("memoryInputIcon");
034
035        // include attributes
036        element.setAttribute("colWidth", "" + p.getNumColumns());
037        element.setAttribute("memory", p.getNamedMemory().getName());
038        storeCommonAttributes(p, element);
039        storeTextInfo(p, element);
040
041        storeLogixNG_Data(p, element);
042
043        element.setAttribute("class", "jmri.jmrit.display.configurexml.MemoryInputIconXml");
044        return element;
045    }
046
047    /**
048     * Load, starting with the memoryInputIcon element, then all the value-icon
049     * pairs
050     *
051     * @param element Top level Element to unpack.
052     * @param o       an Editor as an Object
053     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
054     *                   required by the input XML
055     */
056    @Override
057    public void load(Element element, Object o) throws JmriConfigureXmlException {
058        // create the objects
059        Editor p = (Editor) o;
060
061        int nCol = 2;
062        try {
063            nCol = element.getAttribute("colWidth").getIntValue();
064        } catch (org.jdom2.DataConversionException e) {
065            log.error("failed to convert colWidth attribute");
066        }
067
068        MemoryInputIcon l = new MemoryInputIcon(nCol, p);
069
070        loadTextInfo(l, element);
071        String name;
072        Attribute attr = element.getAttribute("memory");
073        if (attr == null) {
074            log.error("incorrect information for a memory location; must use memory name");
075            p.loadFailed();
076            return;
077        } else {
078            name = attr.getValue();
079        }
080
081        Memory m = jmri.InstanceManager.memoryManagerInstance().getMemory(name);
082
083        if (m != null) {
084            l.setMemory(name);
085        } else {
086            log.error("Memory named '{}' not found.", attr.getValue());
087            p.loadFailed();
088            return;
089        }
090
091        try {
092            p.putItem(l);
093        } catch (Positionable.DuplicateIdException e) {
094            throw new JmriConfigureXmlException("Positionable id is not unique", e);
095        }
096
097        loadLogixNG_Data(l, element);
098
099        // load individual item's option settings after editor has set its global settings
100        loadCommonAttributes(l, Editor.MEMORIES, element);
101        
102        javax.swing.JComponent textField = l.getTextComponent();
103        jmri.jmrit.display.PositionablePopupUtil util = l.getPopupUtility();
104        if (util.hasBackground()) {
105            textField.setBackground(util.getBackground());            
106        } else {
107            textField.setBackground(null);
108            textField.setOpaque(false);
109        }
110    }
111
112    private final static Logger log = LoggerFactory.getLogger(MemoryInputIconXml.class);
113}