001package jmri.util.startup.configurexml;
002
003
004import jmri.util.startup.PerformActionModel;
005import jmri.util.startup.StartupActionsManager;
006import jmri.InstanceManager;
007
008import org.jdom2.Attribute;
009import org.jdom2.Element;
010
011/**
012 * Handle XML persistence of PerformActionModel objects.
013 *
014 * @author Bob Jacobsen Copyright (c) 2003
015 * @see jmri.util.startup.PerformActionModelFactory
016 */
017public class PerformActionModelXml extends jmri.configurexml.AbstractXmlAdapter {
018
019    public PerformActionModelXml() {
020        // no state to set
021    }
022
023    /**
024     * Default implementation for storing the model contents
025     *
026     * @param o Object to store, of type PerformActonModel
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        Element element = new Element("perform");
032        PerformActionModel g = (PerformActionModel) o;
033
034        element.setAttribute("name", g.getClassName());
035        element.setAttribute("type", "Action");
036        element.setAttribute("enabled", g.isEnabled() ? "yes" : "no");
037        element.setAttribute("class", this.getClass().getName());
038        Element property = new Element("property"); // NOI18N
039        property.setAttribute("name", "systemPrefix"); // NOI18N
040        property.setAttribute("value", g.getSystemPrefix());
041        element.addContent(property);
042        return element;
043    }
044
045    /**
046     * Object should be loaded after basic GUI constructed
047     *
048     * @return true to defer loading
049     * @see jmri.configurexml.AbstractXmlAdapter#loadDeferred()
050     * @see jmri.configurexml.XmlAdapter#loadDeferred()
051     */
052    @Override
053    public boolean loadDeferred() {
054        return true;
055    }
056
057    @Override
058    public boolean load(Element shared, Element perNode) {
059        boolean result = true;
060        String className = shared.getAttribute("name").getValue();
061        PerformActionModel model = new PerformActionModel();
062
063        Attribute enabled = shared.getAttribute("enabled");
064        if (enabled != null) {
065            model.setEnabled("yes".equals(enabled.getValue()));
066        } else {
067            model.setEnabled(true);
068        }
069
070        model.setClassName(className);
071        shared.getChildren("property").forEach(child -> { // NOI18N
072            String value = child.getAttributeValue("value"); // NOI18N
073            if (child.getAttributeValue("name").equals("systemPrefix") // NOI18N
074                    && value != null) {
075                model.setSystemPrefix(value);
076            }
077        });
078        InstanceManager.getDefault(StartupActionsManager.class).addAction(model);
079        return result;
080    }
081
082    // initialize logging
083    // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PerformActionModelXml.class);
084
085}