001package jmri.jmrit.logixng.expressions.configurexml;
002
003import java.awt.GraphicsEnvironment;
004import java.util.ResourceBundle;
005import javax.swing.JOptionPane;
006
007import jmri.*;
008import jmri.configurexml.JmriConfigureXmlException;
009import jmri.Block;
010import jmri.BlockManager;
011import jmri.jmrit.logixng.*;
012import jmri.jmrit.logixng.expressions.ExpressionBlock;
013import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
014import jmri.jmrit.logixng.util.parser.ParserException;
015
016import org.jdom2.Element;
017
018/**
019 * Handle XML configuration for ExpressionBlockXml objects.
020 *
021 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
022 * @author Daniel Bergqvist Copyright 2021
023 * @author Dave Sand Copyright (C) 2021
024 */
025public class ExpressionBlockXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
026
027    static final ResourceBundle rb = ResourceBundle.getBundle("jmri.jmrit.logixng.actions.ActionBundle");
028
029    public ExpressionBlockXml() {
030    }
031
032    /**
033     * Default implementation for storing the contents of a ExpressionBlock
034     *
035     * @param o Object to store, of type ExpressionBlock
036     * @return Element containing the complete info
037     */
038    @Override
039    public Element store(Object o) {
040        ExpressionBlock p = (ExpressionBlock) o;
041
042        Element element = new Element("ExpressionBlock");
043        element.setAttribute("class", this.getClass().getName());
044        element.addContent(new Element("systemName").addContent(p.getSystemName()));
045
046        storeCommon(p, element);
047
048        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Block>();
049        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
050
051        element.addContent(new Element("is_isNot").addContent(p.get_Is_IsNot().name()));
052
053        element.addContent(new Element("stateAddressing").addContent(p.getStateAddressing().name()));
054        element.addContent(new Element("blockState").addContent(p.getBeanState().name()));
055        element.addContent(new Element("stateReference").addContent(p.getStateReference()));
056        element.addContent(new Element("stateLocalVariable").addContent(p.getStateLocalVariable()));
057        element.addContent(new Element("stateFormula").addContent(p.getStateFormula()));
058
059        element.addContent(new Element("dataAddressing").addContent(p.getDataAddressing().name()));
060        element.addContent(new Element("dataReference").addContent(p.getDataReference()));
061        element.addContent(new Element("dataLocalVariable").addContent(p.getDataLocalVariable()));
062        element.addContent(new Element("dataFormula").addContent(p.getDataFormula()));
063
064        element.addContent(new Element("blockValue").addContent(p.getBlockValue()));
065
066        return element;
067    }
068
069    @Override
070    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
071        String sys = getSystemName(shared);
072        String uname = getUserName(shared);
073        ExpressionBlock h = new ExpressionBlock(sys, uname);
074
075        loadCommon(h, shared);
076
077        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Block>();
078        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
079        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "block");
080
081        Element is_IsNot = shared.getChild("is_isNot");
082        if (is_IsNot != null) {
083            h.set_Is_IsNot(Is_IsNot_Enum.valueOf(is_IsNot.getTextTrim()));
084        }
085
086        try {
087            Element elem = shared.getChild("stateAddressing");
088            if (elem != null) {
089                h.setStateAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
090            }
091
092            Element blockState = shared.getChild("blockState");
093            if (blockState != null) {
094
095                String state = blockState.getTextTrim();
096                // deprecated 4.23.5 remove 4.25.1
097                if (state.equals("MemoryMatches")) {
098                    state = "ValueMatches";
099                }
100                h.setBeanState(ExpressionBlock.BlockState.valueOf(state));
101            }
102
103            elem = shared.getChild("stateReference");
104            if (elem != null) h.setStateReference(elem.getTextTrim());
105
106            elem = shared.getChild("stateLocalVariable");
107            if (elem != null) h.setStateLocalVariable(elem.getTextTrim());
108
109            elem = shared.getChild("stateFormula");
110            if (elem != null) h.setStateFormula(elem.getTextTrim());
111
112
113            elem = shared.getChild("dataAddressing");
114            if (elem != null) {
115                h.setDataAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
116            }
117
118            elem = shared.getChild("dataReference");
119            if (elem != null) h.setDataReference(elem.getTextTrim());
120
121            elem = shared.getChild("dataLocalVariable");
122            if (elem != null) h.setDataLocalVariable(elem.getTextTrim());
123
124            elem = shared.getChild("dataFormula");
125            if (elem != null) h.setDataFormula(elem.getTextTrim());
126
127            elem = shared.getChild("blockValue");
128            if (elem != null) h.setBlockValue(elem.getTextTrim());
129
130            // deprecated 4.23.5 remove 4.25.1
131            elem = shared.getChild("blockConstant");
132            if (elem != null) h.setBlockValue(elem.getTextTrim());
133
134            // deprecated 4.23.5 remove 4.25.1
135            elem = shared.getChild("blockMemory");
136//             if (elem != null) h.setBlockMemory(elem.getTextTrim());
137            if (elem != null) {
138                String memoryName = elem.getTextTrim();
139                h.setBlockValue(">>> " + elem.getTextTrim() + " <<<");
140                if (!GraphicsEnvironment.isHeadless() && !Boolean.getBoolean("jmri.test.no-dialogs")) {
141                    JOptionPane.showMessageDialog(null,
142                            rb.getString("ActionBlock_MemoryChange"),
143                            rb.getString("ActionBlock_MemoryTitle") + " " + memoryName,
144                            JOptionPane.WARNING_MESSAGE);
145                }
146            }
147
148        } catch (ParserException e) {
149            throw new JmriConfigureXmlException(e);
150        }
151
152        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
153        return true;
154    }
155
156//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionBlockXml.class);
157}