001package apps.startup.configurexml;
002
003import apps.startup.TriggerRouteModel;
004
005import jmri.InstanceManager;
006import jmri.configurexml.AbstractXmlAdapter;
007import jmri.util.startup.StartupActionsManager;
008
009import org.jdom2.Attribute;
010import org.jdom2.Element;
011
012/**
013 * Handle XML persistence for {@link apps.startup.TriggerRouteModel} objects
014 * and set the defined {@link jmri.Route} during application start.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2003
017 * @author Ken Cameron Copyright: 2014(c)
018 * @author Randall Wood (c) 2016
019 * @see apps.startup.TriggerRouteModelFactory
020 */
021public class TriggerRouteModelXml extends AbstractXmlAdapter {
022
023    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TriggerRouteModelXml.class);
024
025    public TriggerRouteModelXml() {
026    }
027
028    /**
029     * Default implementation for storing the model contents
030     *
031     * @param o Object to store, of type PerformActonModel
032     * @return Element containing the complete info
033     */
034    @Override
035    public Element store(Object o) {
036        Element e = new Element("perform"); // NOI18N
037        e.setAttribute("name", ((TriggerRouteModel) o).getName());
038        e.setAttribute("type", "Action");
039        e.setAttribute("enabled", ((TriggerRouteModel) o).isEnabled() ? "yes" : "no");
040        e.setAttribute("class", this.getClass().getName());
041        return e;
042    }
043
044    /**
045     * Object should be loaded after basic GUI constructed
046     *
047     * @return true to defer loading
048     * @see jmri.configurexml.AbstractXmlAdapter#loadDeferred()
049     * @see jmri.configurexml.XmlAdapter#loadDeferred()
050     */
051    @Override
052    public boolean loadDeferred() {
053        return true;
054    }
055
056    @Override
057    public boolean load(Element shared, Element perNode) {
058        boolean result = true;
059        String userName = shared.getAttribute("name").getValue();
060
061        TriggerRouteModel m = new TriggerRouteModel();
062        m.setUserName(userName);
063
064        Attribute enabled = shared.getAttribute("enabled");
065        if (enabled != null) {
066            m.setEnabled("yes".equals(enabled.getValue()));
067        } else {
068            m.setEnabled(true);
069        }
070
071        // store the model
072        InstanceManager.getDefault(StartupActionsManager.class).addAction(m);
073        return result;
074    }
075
076    /**
077     * Update static data from XML file
078     *
079     * @param element Top level Element to unpack.
080     * @param o       ignored
081     */
082    @Override
083    public void load(Element element, Object o) {
084        log.error("Unexpected call of load(Element, Object)");
085    }
086
087}