001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.InstanceManager;
004import jmri.jmrit.logixng.MaleSocket;
005import jmri.jmrit.logixng.DigitalBooleanActionManager;
006import jmri.jmrit.logixng.actions.DigitalBooleanLogixAction;
007
008import org.jdom2.Attribute;
009import org.jdom2.Element;
010
011/**
012 * Handle XML configuration for ActionLightXml objects.
013 *
014 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
015 * @author Daniel Bergqvist Copyright (C) 2019
016 */
017public class DigitalBooleanLogixActionXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
018
019    public DigitalBooleanLogixActionXml() {
020    }
021
022    /**
023     * Default implementation for storing the contents of a SE8cSignalHead
024     *
025     * @param o Object to store, of type TripleTurnoutSignalHead
026     * @return Element containing the complete info
027     */
028    @Override
029    public Element store(Object o) {
030        DigitalBooleanLogixAction p = (DigitalBooleanLogixAction) o;
031
032        Element element = new Element("LogixAction");
033        element.setAttribute("class", this.getClass().getName());
034        element.addContent(new Element("systemName").addContent(p.getSystemName()));
035
036        storeCommon(p, element);
037
038        element.setAttribute("trigger", p.getTrigger().name());
039
040        Element e2 = new Element("Socket");
041        e2.addContent(new Element("socketName").addContent(p.getChild(0).getName()));
042        MaleSocket socket = p.getSocket().getConnectedSocket();
043        String socketSystemName;
044        if (socket != null) {
045            socketSystemName = socket.getSystemName();
046        } else {
047            socketSystemName = p.getSocketSystemName();
048        }
049        if (socketSystemName != null) {
050            e2.addContent(new Element("systemName").addContent(socketSystemName));
051        }
052        element.addContent(e2);
053
054        return element;
055    }
056
057    @Override
058    public boolean load(Element shared, Element perNode) {
059
060        Attribute triggerAttribute = shared.getAttribute("trigger");
061
062        DigitalBooleanLogixAction.When trigger = DigitalBooleanLogixAction.When.Either;
063        try {
064            trigger = DigitalBooleanLogixAction.When.valueOf(triggerAttribute.getValue());
065        } catch (IllegalArgumentException e) {
066            // Handle backwards compability pre JMRI 5.5.5
067            switch (triggerAttribute.getValue()) {
068                case "CHANGE_TO_TRUE":
069                    trigger = DigitalBooleanLogixAction.When.True;
070                    break;
071                case "CHANGE_TO_FALSE":
072                    trigger = DigitalBooleanLogixAction.When.False;
073                    break;
074                case "CHANGE":
075                    trigger = DigitalBooleanLogixAction.When.Either;
076                    break;
077                default:
078                    throw e;
079            }
080        }
081
082        String sys = getSystemName(shared);
083        String uname = getUserName(shared);
084        DigitalBooleanLogixAction h = new DigitalBooleanLogixAction(sys, uname, trigger);
085
086        loadCommon(h, shared);
087
088        Element socketName = shared.getChild("Socket").getChild("socketName");
089        h.getChild(0).setName(socketName.getTextTrim());
090        Element socketSystemName = shared.getChild("Socket").getChild("systemName");
091        if (socketSystemName != null) {
092            h.setActionSocketSystemName(socketSystemName.getTextTrim());
093        }
094
095        InstanceManager.getDefault(DigitalBooleanActionManager.class).registerAction(h);
096        return true;
097    }
098
099//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(OnChangeActionXml.class);
100}