001package jmri.jmrit.display.configurexml;
002
003import javax.swing.JTable;
004import javax.swing.table.TableColumnModel;
005
006import jmri.configurexml.JmriConfigureXmlException;
007import jmri.jmrit.display.*;
008import jmri.jmrit.logixng.*;
009import jmri.jmrit.logixng.Module;
010
011import org.jdom2.Attribute;
012import org.jdom2.Element;
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016/**
017 * Handle configuration for display.LogixNGTableIcon objects.
018 *
019 * @author Bob Jacobsen     Copyright: Copyright (c) 2009
020 * @author Daniel Bergqvist Copyright (C) 2025
021 */
022public class LogixNGTableIconXml extends PositionableLabelXml {
023
024    public LogixNGTableIconXml() {
025    }
026
027    /**
028     * Default implementation for storing the contents of a LogixNGTableIcon
029     *
030     * @param o Object to store, of type LogixNGTableIcon
031     * @return Element containing the complete info
032     */
033    @Override
034    public Element store(Object o) {
035
036        LogixNGTableIcon p = (LogixNGTableIcon) o;
037
038        Element element = new Element("logixNGTableIcon");
039
040        // include attributes
041        element.setAttribute("logixNGTable", p.getTableModel().getTable().getDisplayName());
042
043        Module validateModule = p.getTableModel().getValidateModule();
044        if (validateModule != null) {
045            element.setAttribute("validateModule", validateModule.getDisplayName());
046        }
047        element.setAttribute("isEditable", p.getTableModel().isEditable() ? "yes" : "no");
048        element.setAttribute("editableColumns", p.getTableModel().getEditableColumns());
049
050        StringBuilder sb = new StringBuilder();
051        JTable jTable = p.getJTable();
052        TableColumnModel colModel = jTable.getColumnModel();
053        for (int col=0; col < colModel.getColumnCount(); col++) {
054            if (sb.length() > 0) {
055                sb.append(",");
056            }
057            sb.append(colModel.getColumn(col).getWidth());
058        }
059        element.setAttribute("columnWidths", sb.toString());
060
061        storeCommonAttributes(p, element);
062        storeTextInfo(p, element);
063
064        storeLogixNG_Data(p, element);
065
066        element.setAttribute("class", "jmri.jmrit.display.configurexml.LogixNGTableIconXml");
067        return element;
068    }
069
070    /**
071     * Load, starting with the logixNGTableIcon element, then all the value-icon
072     * pairs
073     *
074     * @param element Top level Element to unpack.
075     * @param o       an Editor as an Object
076     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
077     *                   required by the input XML
078     */
079    @Override
080    public void load(Element element, Object o) throws JmriConfigureXmlException {
081        // create the objects
082        Editor p = (Editor) o;
083
084        String name;
085        Attribute attr = element.getAttribute("logixNGTable");
086        if (attr == null) {
087            log.error("incorrect information for a logixNGTable location; must use logixNGTable name");
088            p.loadFailed();
089            return;
090        } else {
091            name = attr.getValue();
092        }
093
094        NamedTable namedTable = jmri.InstanceManager.getDefault(NamedTableManager.class).getNamedTable(name);
095
096        if (namedTable == null) {
097            log.error("LogixNG Table named '{}' not found.", attr.getValue());
098            p.loadFailed();
099            return;
100        }
101
102        LogixNGTableIcon l = new LogixNGTableIcon(name, p);
103
104        attr = element.getAttribute("validateModule");
105        if (attr != null) {
106            name = attr.getValue();
107            Module validateModule = jmri.InstanceManager.getDefault(ModuleManager.class).getModule(name);
108            l.getTableModel().setValidateModule(validateModule);
109        }
110
111        attr = element.getAttribute("isEditable");
112        if (attr != null) {
113            String value = attr.getValue();
114            l.getTableModel().setEditable("yes".equals(value));
115        }
116
117        attr = element.getAttribute("editableColumns");
118        if (attr != null) {
119            l.getTableModel().setEditableColumns(attr.getValue());
120        }
121
122        attr = element.getAttribute("columnWidths");
123        if (attr != null) {
124            String[] widths = attr.getValue().split(",");
125            JTable jTable = l.getJTable();
126            TableColumnModel colModel = jTable.getColumnModel();
127            for (int col=0; col < colModel.getColumnCount(); col++) {
128                if (widths.length >= col+1) {
129                    colModel.getColumn(col).setPreferredWidth(Integer.parseInt(widths[col]));
130                }
131            }
132        }
133
134        loadTextInfo(l, element);
135
136        try {
137            p.putItem(l);
138        } catch (Positionable.DuplicateIdException e) {
139            throw new JmriConfigureXmlException("Positionable id is not unique", e);
140        }
141
142        loadLogixNG_Data(l, element);
143
144        // load individual item's option settings after editor has set its global settings
145        loadCommonAttributes(l, Editor.MEMORIES, element);
146
147        javax.swing.JComponent textField = l.getTextComponent();
148        jmri.jmrit.display.PositionablePopupUtil util = l.getPopupUtility();
149        if (util.hasBackground()) {
150            textField.setBackground(util.getBackground());
151        } else {
152            textField.setBackground(null);
153            textField.setOpaque(false);
154        }
155    }
156
157    private final static Logger log = LoggerFactory.getLogger(LogixNGTableIconXml.class);
158}