001package jmri.jmrit.logixng.actions.configurexml;
002
003import java.util.List;
004import java.util.Set;
005
006import jmri.InstanceManager;
007import jmri.jmrit.logixng.DigitalActionManager;
008import jmri.jmrit.logixng.MaleSocket;
009import jmri.jmrit.logixng.actions.ShowDialog;
010import jmri.jmrit.logixng.actions.ShowDialog.Button;
011import jmri.jmrit.logixng.util.parser.ParserException;
012
013import org.jdom2.Element;
014
015/**
016 * Handle XML configuration for ShowDialog objects.
017 *
018 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
019 * @author Daniel Bergqvist Copyright (C) 2021
020 */
021public class ShowDialogXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
022
023    public ShowDialogXml() {
024    }
025    
026    /**
027     * Default implementation for storing the contents of a SE8cSignalHead
028     *
029     * @param o Object to store, of type TripleLightSignalHead
030     * @return Element containing the complete info
031     */
032    @Override
033    public Element store(Object o) {
034        ShowDialog p = (ShowDialog) o;
035
036        Element element = new Element("ShowDialog");
037        element.setAttribute("class", this.getClass().getName());
038        element.addContent(new Element("systemName").addContent(p.getSystemName()));
039        
040        storeCommon(p, element);
041        
042        Element e2 = new Element("ValidateSocket");
043        e2.addContent(new Element("socketName").addContent(p.getValidateSocket().getName()));
044        MaleSocket socket = p.getValidateSocket().getConnectedSocket();
045        String socketSystemName;
046        if (socket != null) {
047            socketSystemName = socket.getSystemName();
048        } else {
049            socketSystemName = p.getValidateSocketSystemName();
050        }
051        if (socketSystemName != null) {
052            e2.addContent(new Element("systemName").addContent(socketSystemName));
053        }
054        element.addContent(e2);
055        
056        e2 = new Element("ExecuteSocket");
057        e2.addContent(new Element("socketName").addContent(p.getExecuteSocket().getName()));
058        socket = p.getExecuteSocket().getConnectedSocket();
059        if (socket != null) {
060            socketSystemName = socket.getSystemName();
061        } else {
062            socketSystemName = p.getExecuteSocketSystemName();
063        }
064        if (socketSystemName != null) {
065            e2.addContent(new Element("systemName").addContent(socketSystemName));
066        }
067        element.addContent(e2);
068        
069        Element buttons = new Element("Buttons");
070        for (Button button : Button.values()) {
071            if (p.getEnabledButtons().contains(button)) {
072                buttons.addContent(new Element("name").addContent(button.name()));
073            }
074        }
075        element.addContent(buttons);
076        
077        element.addContent(new Element("formatType").addContent(p.getFormatType().name()));
078        element.addContent(new Element("format").addContent(p.getFormat()));
079        
080        element.addContent(new Element("localVariableForSelectedButton").addContent(p.getLocalVariableForSelectedButton()));
081        element.addContent(new Element("localVariableForInputString").addContent(p.getLocalVariableForInputString()));
082        element.addContent(new Element("modal").addContent(p.getModal() ? "yes" : "no"));
083        element.addContent(new Element("multiLine").addContent(p.getMultiLine() ? "yes" : "no"));
084        
085        Element parameters = new Element("DataList");
086        for (ShowDialog.Data data : p.getDataList()) {
087            Element elementParameter = new Element("Data");
088            elementParameter.addContent(new Element("type").addContent(data.getDataType().name()));
089            elementParameter.addContent(new Element("data").addContent(data.getData()));
090            parameters.addContent(elementParameter);
091        }
092        element.addContent(parameters);
093        
094        return element;
095    }
096    
097    @Override
098    public boolean load(Element shared, Element perNode) {
099        String sys = getSystemName(shared);
100        String uname = getUserName(shared);
101        ShowDialog h = new ShowDialog(sys, uname);
102        
103        loadCommon(h, shared);
104        
105        Element validateSocket = shared.getChild("ValidateSocket");
106        if (validateSocket != null) {
107            Element socketNameElement = validateSocket.getChild("socketName");
108            String socketName = socketNameElement.getTextTrim();
109            Element socketSystemNameElement = validateSocket.getChild("systemName");
110            String socketSystemName = null;
111            if (socketSystemNameElement != null) {
112                socketSystemName = socketSystemNameElement.getTextTrim();
113            }
114
115            h.getValidateSocket().setName(socketName);
116            h.setValidateSocketSystemName(socketSystemName);
117        }
118        
119        Element executeSocket = shared.getChild("ExecuteSocket");
120        // Keep this if statement for backwards compability with JMRI 4.26. Remove for 5.2
121        if ((executeSocket == null) && (shared.getChild("Socket") != null)) {
122            executeSocket = shared.getChild("Socket");
123        }
124        if (executeSocket != null) {
125            Element socketNameElement = executeSocket.getChild("socketName");
126            String socketName = socketNameElement.getTextTrim();
127            Element socketSystemNameElement = executeSocket.getChild("systemName");
128            String socketSystemName = null;
129            if (socketSystemNameElement != null) {
130                socketSystemName = socketSystemNameElement.getTextTrim();
131            }
132
133            h.getExecuteSocket().setName(socketName);
134            h.setExecuteSocketSystemName(socketSystemName);
135        }
136        
137        List<Element> buttons = shared.getChild("Buttons").getChildren();  // NOI18N
138        Set<Button> enabledButtons = h.getEnabledButtons();
139        enabledButtons.clear();
140        for (Element e : buttons) {
141            enabledButtons.add(Button.valueOf(e.getTextTrim()));
142        }
143        
144        Element elem = shared.getChild("formatType");  // NOI18N
145        h.setFormatType((elem != null) ? ShowDialog.FormatType.valueOf(elem.getTextTrim()) : ShowDialog.FormatType.OnlyText);
146        
147        elem = shared.getChild("format");  // NOI18N
148        h.setFormat((elem != null) ? elem.getValue() : "");
149        
150        elem = shared.getChild("localVariableForSelectedButton");  // NOI18N
151        h.setLocalVariableForSelectedButton((elem != null) ? elem.getValue() : "");
152        
153        // Keep for backwards compability with 4.26. Changed in 4.99.1 / 5.0
154        elem = shared.getChild("localVariable");  // NOI18N
155        if (elem != null) {
156            h.setLocalVariableForSelectedButton(elem.getValue());
157        }
158        
159        elem = shared.getChild("localVariableForInputString");  // NOI18N
160        h.setLocalVariableForInputString((elem != null) ? elem.getValue() : "");
161        
162        elem = shared.getChild("modal");  // NOI18N
163        h.setModal((elem != null) ? elem.getTextTrim().equals("yes") : false);  // NOI18N
164        
165        elem = shared.getChild("multiLine");  // NOI18N
166        h.setMultiLine((elem != null) ? elem.getTextTrim().equals("yes") : false);  // NOI18N
167        
168        List<Element> dataList = shared.getChild("DataList").getChildren();  // NOI18N
169        log.debug("Found {} dataList", dataList.size() );  // NOI18N
170        
171        for (Element e : dataList) {
172            ShowDialog.DataType type = ShowDialog.DataType.LocalVariable;
173            Element elementType = e.getChild("type");
174            if (elementType != null) {
175                type = ShowDialog.DataType.valueOf(elementType.getTextTrim());
176            }
177            
178            Element elementName = e.getChild("data");
179            
180            if (elementName == null) throw new IllegalArgumentException("Element 'name' does not exists");
181            
182            try {
183                h.getDataList().add(new ShowDialog.Data(type, elementName.getTextTrim()));
184            } catch (ParserException ex) {
185                log.warn("Could not add {} to data list : {}", elementName.getTextTrim(), ex.getMessage());
186            }
187        }
188        
189        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
190        return true;
191    }
192    
193    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ShowDialogXml.class);
194}