001package jmri.jmrix.dccpp.swing.virtuallcd.configurexml;
002
003import jmri.jmrit.display.configurexml.*;
004
005
006import jmri.configurexml.JmriConfigureXmlException;
007import jmri.jmrit.display.*;
008import jmri.jmrix.dccpp.swing.virtuallcd.VirtualLcdPositionable;
009
010import org.jdom2.Element;
011
012/**
013 * Handle configuration for VirtualLcdPositionable objects.
014 *
015 * @author Howard G. Penny  Copyright (c) 2005
016 * @author Daniel Bergqvist Copyright (c) 2026
017 */
018public class VirtualLcdPositionableXml
019        extends PositionableLabelXml {
020
021    public VirtualLcdPositionableXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of an VirtualLcdPositionable
026     *
027     * @param o Object to store, of type VirtualLcdPositionable
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032
033        VirtualLcdPositionable p = (VirtualLcdPositionable) o;
034        if (!p.isActive()) {
035            return null; // if flagged as inactive, don't store
036        }
037
038        Element element = new Element("dccex_virtual_lcd");
039
040        element.addContent(VirtualLCDConfigurationXml.store(p.getVirtualLCDPanel()));
041
042        // include contents
043        if (p.getId() != null) element.setAttribute("id", p.getId());
044        element.setAttribute("x", "" + p.getX());
045        element.setAttribute("y", "" + p.getY());
046
047        element.setAttribute("class", this.getClass().getName());
048
049        storeLogixNG_Data(p, element);
050
051        return element;
052    }
053
054    @Override
055    public boolean load(Element shared, Element perNode) {
056        log.error("Invalid method called");
057        return false;
058    }
059
060    /**
061     * Create an VirtualLcdPositionable, then add to a target JLayeredPane
062     *
063     * @param element Top level Element to unpack.
064     * @param o       an Editor as an Object
065     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
066     *                   required by the input XML
067     */
068    @Override
069    public void load(Element element, Object o) throws JmriConfigureXmlException {
070        // get object class and create the clock object
071        Editor ed = (Editor) o;
072
073        VirtualLcdPositionable l = new VirtualLcdPositionable(ed);
074
075        VirtualLCDConfigurationXml.load(l.getVirtualLCDPanel(), element, false);
076
077        l.initComponents();
078
079        // find coordinates
080        int x = 0;
081        int y = 0;
082        try {
083            if (element.getAttribute("id") != null) {
084                try {
085                    l.setId(element.getAttribute("id").getValue());
086                } catch (Positionable.DuplicateIdException e) {
087                    throw new JmriConfigureXmlException("Positionable id is not unique", e);
088                }
089            }
090            x = element.getAttribute("x").getIntValue();
091            y = element.getAttribute("y").getIntValue();
092        } catch (org.jdom2.DataConversionException e) {
093            log.error("failed to convert positional attribute");
094        }
095        l.setOpaque(false);
096        l.setLocation(x, y);
097
098        // add the Virtual LCD to the panel
099        l.setDisplayLevel(Editor.CLOCK);
100        try {
101            ed.putItem(l, true);
102        } catch (Positionable.DuplicateIdException e) {
103            throw new JmriConfigureXmlException("Positionable id is not unique", e);
104        }
105
106        loadLogixNG_Data(l, element);
107    }
108
109    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(VirtualLcdPositionableXml.class);
110}