001package jmri.util.startup.configurexml;
002
003import jmri.util.startup.PerformScriptModel;
004
005import jmri.util.startup.StartupActionsManager;
006import jmri.InstanceManager;
007import jmri.util.FileUtil;
008
009import org.jdom2.Attribute;
010import org.jdom2.Element;
011
012/**
013 * Handle XML persistence of PerformScriptModel objects
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2003
016 * @author Ken Cameron Copyright: Copyright (c) 2014
017 * @see jmri.util.startup.PerformScriptModelFactory
018 */
019public class PerformScriptModelXml extends jmri.configurexml.AbstractXmlAdapter {
020
021    public PerformScriptModelXml() {
022    }
023
024    /**
025     * Default implementation for storing the model contents
026     *
027     * @param o Object to store, of type PerformActonModel
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        Element e = new Element("perform");
033        PerformScriptModel g = (PerformScriptModel) o;
034
035        e.setAttribute("name", FileUtil.getPortableFilename(g.getFileName()));
036        e.setAttribute("type", "ScriptFile");
037        e.setAttribute("enabled", g.isEnabled() ? "yes" : "no");
038        e.setAttribute("class", this.getClass().getName());
039        return e;
040    }
041
042    /**
043     * Object should be loaded after basic GUI constructed
044     *
045     * @return true to defer loading
046     * @see jmri.configurexml.AbstractXmlAdapter#loadDeferred()
047     * @see jmri.configurexml.XmlAdapter#loadDeferred()
048     */
049    @Override
050    public boolean loadDeferred() {
051        return true;
052    }
053
054    @Override
055    public boolean load(Element shared, Element perNode) {
056        boolean result = true;
057        String fileName = shared.getAttribute("name").getValue();
058        fileName = FileUtil.getAbsoluteFilename(fileName);
059        PerformScriptModel m = new PerformScriptModel();
060
061        Attribute enabled = shared.getAttribute("enabled");
062        if (enabled != null) {
063            m.setEnabled("yes".equals(enabled.getValue()));
064        } else {
065            m.setEnabled(true);
066        }
067
068        m.setFileName(fileName);
069        InstanceManager.getDefault(StartupActionsManager.class).addAction(m);
070        return result;
071    }
072
073    // initialize logging
074//    private final static Logger log = LoggerFactory.getLogger(PerformScriptModelXml.class);
075
076}