001package jmri.jmrit.logixng.actions.configurexml;
002
003import java.util.List;
004
005import jmri.InstanceManager;
006import jmri.jmrit.logixng.DigitalActionManager;
007import jmri.jmrit.logixng.actions.ActionListenOnBeans;
008import jmri.jmrit.logixng.actions.ActionListenOnBeans.NamedBeanReference;
009import jmri.jmrit.logixng.NamedBeanType;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for ActionLightXml objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2019
018 */
019public class ActionListenOnBeansXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public ActionListenOnBeansXml() {
022    }
023    
024    /**
025     * Default implementation for storing the contents of a SE8cSignalHead
026     *
027     * @param o Object to store, of type TripleLightSignalHead
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        ActionListenOnBeans p = (ActionListenOnBeans) o;
033
034        Element element = new Element("ActionListenOnBeans");
035        element.setAttribute("class", this.getClass().getName());
036        element.addContent(new Element("systemName").addContent(p.getSystemName()));
037        
038        storeCommon(p, element);
039        
040        Element parameters = new Element("References");
041        for (NamedBeanReference ref : p.getReferences()) {
042            Element elementParameter = new Element("Reference");
043            elementParameter.addContent(new Element("name").addContent(ref.getName()));
044            elementParameter.addContent(new Element("type").addContent(ref.getType().name()));
045            elementParameter.addContent(new Element("all").addContent(ref.getListenOnAllProperties() ? "yes" : "no"));  // NOI18N
046            parameters.addContent(elementParameter);
047        }
048        element.addContent(parameters);
049        
050        element.addContent(new Element("localVariableNamedBean").addContent(p.getLocalVariableNamedBean()));
051        element.addContent(new Element("localVariableEvent").addContent(p.getLocalVariableEvent()));
052        element.addContent(new Element("localVariableNewValue").addContent(p.getLocalVariableNewValue()));
053        
054        return element;
055    }
056    
057    @Override
058    public boolean load(Element shared, Element perNode) {
059        String sys = getSystemName(shared);
060        String uname = getUserName(shared);
061        ActionListenOnBeans h = new ActionListenOnBeans(sys, uname);
062
063        loadCommon(h, shared);
064        
065        List<Element> parameterList = shared.getChild("References").getChildren();  // NOI18N
066        log.debug("Found {} references", parameterList.size() );  // NOI18N
067        
068        for (Element e : parameterList) {
069            Element elementName = e.getChild("name");
070            
071            NamedBeanType type = null;
072            Element elementType = e.getChild("type");
073            if (elementType != null) {
074                type = NamedBeanType.valueOf(elementType.getTextTrim());
075            }
076            
077            if (elementName == null) throw new IllegalArgumentException("Element 'name' does not exists");
078            if (type == null) throw new IllegalArgumentException("Element 'type' does not exists");
079            
080            String all = "no";  // NOI18N
081            if (e.getChild("all") != null) {  // NOI18N
082                all = e.getChild("all").getValue();  // NOI18N
083            }
084            boolean listenToAll = "yes".equals(all); // NOI18N
085            
086            h.addReference(new NamedBeanReference(elementName.getTextTrim(), type, listenToAll));
087        }
088        
089        Element variableName = shared.getChild("localVariableNamedBean");
090        if (variableName != null) {
091            h.setLocalVariableNamedBean(variableName.getTextTrim());
092        }
093        
094        variableName = shared.getChild("localVariableEvent");
095        if (variableName != null) {
096            h.setLocalVariableEvent(variableName.getTextTrim());
097        }
098        
099        variableName = shared.getChild("localVariableNewValue");
100        if (variableName != null) {
101            h.setLocalVariableNewValue(variableName.getTextTrim());
102        }
103        
104        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
105        return true;
106    }
107    
108    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionListenOnBeansXml.class);
109}