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