001package jmri.jmrit.logixng.actions.configurexml;
002
003import java.util.List;
004
005import jmri.InstanceManager;
006import jmri.SystemConnectionMemo;
007import jmri.configurexml.JmriConfigureXmlException;
008import jmri.jmrit.logixng.*;
009import jmri.jmrit.logixng.actions.ProgramOnMain;
010import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectComboBoxXml;
011import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectEnumXml;
012import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectIntegerXml;
013
014import org.jdom2.Element;
015
016/**
017 * Handle XML configuration for ActionLightXml objects.
018 *
019 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
020 * @author Daniel Bergqvist Copyright (C) 2024
021 */
022public class ProgramOnMainXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
023
024    /**
025     * Default implementation for storing the contents of a SE8cSignalHead
026     *
027     * @param o Object to store, of type TripleTurnoutSignalHead
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        ProgramOnMain p = (ProgramOnMain) o;
033
034        var selectProgrammingModeXml = new LogixNG_SelectComboBoxXml();
035        var selectLongOrShortAddressXml = new LogixNG_SelectEnumXml<ProgramOnMain.LongOrShortAddress>();
036        var selectAddressXml = new LogixNG_SelectIntegerXml();
037        var selectCVXml = new LogixNG_SelectIntegerXml();
038        var selectValueXml = new LogixNG_SelectIntegerXml();
039
040        Element element = new Element("ProgramOnMain");
041        element.setAttribute("class", this.getClass().getName());
042        element.addContent(new Element("systemName").addContent(p.getSystemName()));
043
044        storeCommon(p, element);
045
046        Element e2 = new Element("Socket");
047        e2.addContent(new Element("socketName").addContent(p.getExecuteSocket().getName()));
048        MaleSocket socket = p.getExecuteSocket().getConnectedSocket();
049        String socketSystemName;
050        if (socket != null) {
051            socketSystemName = socket.getSystemName();
052        } else {
053            socketSystemName = p.getExecuteSocketSystemName();
054        }
055        if (socketSystemName != null) {
056            e2.addContent(new Element("systemName").addContent(socketSystemName));
057        }
058        element.addContent(e2);
059
060        element.addContent(selectProgrammingModeXml.store(
061                p.getSelectProgrammingMode(), "programmingMode"));
062        element.addContent(selectLongOrShortAddressXml.store(p.getSelectLongOrShortAddress(), "longOrShortAddress"));
063        element.addContent(selectAddressXml.store(p.getSelectAddress(), "address"));
064        element.addContent(selectCVXml.store(p.getSelectCV(), "cv"));
065        element.addContent(selectValueXml.store(p.getSelectValue(), "value"));
066
067        if (p.getMemo() != null) {
068            element.addContent(new Element("systemConnection")
069                    .addContent(p.getMemo().getSystemPrefix()));
070        }
071
072        element.addContent(new Element("localVariableForStatus").addContent(p.getLocalVariableForStatus()));
073        element.addContent(new Element("wait").addContent(p.getWait() ? "yes" : "no"));
074
075        return element;
076    }
077
078    @Override
079    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
080
081        String sys = getSystemName(shared);
082        String uname = getUserName(shared);
083        ProgramOnMain h = new ProgramOnMain(sys, uname);
084
085        var selectProgrammingModeXml = new LogixNG_SelectComboBoxXml();
086        var selectLongOrShortAddressXml = new LogixNG_SelectEnumXml<ProgramOnMain.LongOrShortAddress>();
087        var selectAddressXml = new LogixNG_SelectIntegerXml();
088        var selectCVXml = new LogixNG_SelectIntegerXml();
089        var selectValueXml = new LogixNG_SelectIntegerXml();
090
091        loadCommon(h, shared);
092
093        Element executeSocket = shared.getChild("Socket");
094        if ((executeSocket == null) && (shared.getChild("Socket") != null)) {
095            executeSocket = shared.getChild("Socket");
096        }
097        if (executeSocket != null) {
098            Element socketNameElement = executeSocket.getChild("socketName");
099            String socketName = socketNameElement.getTextTrim();
100            Element socketSystemNameElement = executeSocket.getChild("systemName");
101            String socketSystemName = null;
102            if (socketSystemNameElement != null) {
103                socketSystemName = socketSystemNameElement.getTextTrim();
104            }
105
106            h.getExecuteSocket().setName(socketName);
107            h.setExecuteSocketSystemName(socketSystemName);
108        }
109
110        selectProgrammingModeXml.load(shared.getChild("programmingMode"), h.getSelectProgrammingMode());
111        selectLongOrShortAddressXml.load(shared.getChild("longOrShortAddress"), h.getSelectLongOrShortAddress());
112        selectAddressXml.load(shared.getChild("address"), h.getSelectAddress());
113        selectCVXml.load(shared.getChild("cv"), h.getSelectCV());
114        selectValueXml.load(shared.getChild("value"), h.getSelectValue());
115
116        Element systemConnection = shared.getChild("systemConnection");
117        if (systemConnection != null) {
118            String systemConnectionName = systemConnection.getTextTrim();
119            List<SystemConnectionMemo> systemConnections =
120                    jmri.InstanceManager.getList(SystemConnectionMemo.class);
121
122            for (SystemConnectionMemo memo : systemConnections) {
123                if (memo.getSystemPrefix().equals(systemConnectionName)) {
124                    h.setMemo(memo);
125                    break;
126                }
127            }
128        }
129
130        Element elem = shared.getChild("localVariableForStatus");  // NOI18N
131        h.setLocalVariableForStatus((elem != null) ? elem.getValue() : "");
132
133        Element wait = shared.getChild("wait");
134        if (wait != null) {
135            h.setWait("yes".equals(wait.getTextTrim()));
136        } else {
137            h.setWait(true);
138        }
139
140        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
141        return true;
142    }
143
144//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ProgramOnMainXml.class);
145}