001package jmri.jmrit.symbolicprog.configurexml;
002
003import jmri.ConfigureManager;
004import jmri.InstanceManager;
005import jmri.jmrit.symbolicprog.ProgrammerConfigManager;
006import jmri.jmrit.symbolicprog.ProgrammerConfigPane;
007import org.jdom2.Attribute;
008import org.jdom2.Element;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Handle XML persistance of symbolic programmer default values.
014 * <p>
015 * This class is named as being the persistant form of the ProgrammerConfigPane
016 * class, but there's no object of that form created when this is read back.
017 * Instead, this persists static members of the symbolicprog.CombinedLocoSelPane
018 * class.
019 *
020 * @author Bob Jacobsen Copyright: Copyright (c) 2003
021 */
022public class ProgrammerConfigPaneXml extends jmri.configurexml.AbstractXmlAdapter {
023
024    public ProgrammerConfigPaneXml() {
025    }
026
027    /**
028     * Default implementation for storing the static contents
029     *
030     * @param o Object to store, of type PositionableLabel
031     * @return Element containing the complete info
032     */
033    @Override
034    public Element store(Object o) {
035        ProgrammerConfigPane p = (ProgrammerConfigPane) o;
036        Element programmer = new Element("programmer");
037        if (p.getSelectedItem() != null) {
038            programmer.setAttribute("defaultFile", p.getSelectedItem());
039        }
040        programmer.setAttribute("verifyBeforeWrite", "no");
041        if (!p.getShowEmptyTabs()) {
042            programmer.setAttribute("showEmptyPanes", "no");
043        }
044        if (p.getShowCvNums()) {
045            programmer.setAttribute("showCvNumbers", "yes");
046        }
047        if (p.getCanCacheDefault()) {
048            programmer.setAttribute("canCacheDefault", "yes");
049        } else {
050            programmer.setAttribute("canCacheDefault", "no");
051        }
052        if (p.getDoConfirmRead()) {
053            programmer.setAttribute("doConfirmRead", "yes");
054        } else {
055            programmer.setAttribute("doConfirmRead", "no");
056        }
057        programmer.setAttribute("class", this.getClass().getName());
058        return programmer;
059    }
060
061    @Override
062    public boolean load(Element shared, Element perNode) {
063        boolean result = true;
064
065        if (shared.getAttribute("defaultFile") != null) {
066            if (log.isDebugEnabled()) {
067                log.debug("set programmer default file: {}", shared.getAttribute("defaultFile").getValue());
068            }
069            InstanceManager.getDefault(ProgrammerConfigManager.class).setDefaultFile(shared.getAttribute("defaultFile").getValue());
070        }
071
072        Attribute a;
073        if (null != (a = shared.getAttribute("showEmptyPanes"))) {
074            InstanceManager.getDefault(ProgrammerConfigManager.class).setShowEmptyPanes(!a.getValue().equals("no"));
075        }
076        if (null != (a = shared.getAttribute("showCvNumbers"))) {
077            InstanceManager.getDefault(ProgrammerConfigManager.class).setShowCvNumbers(a.getValue().equals("yes"));
078        }
079        if (null != (a = shared.getAttribute("canCacheDefault"))) {
080            InstanceManager.getDefault(ProgrammerConfigManager.class).setCanCacheDefault(a.getValue().equals("yes"));
081        }
082        if (null != (a = shared.getAttribute("doConfirmRead"))) {
083            InstanceManager.getDefault(ProgrammerConfigManager.class).setDoConfirmRead(a.getValue().equals("yes"));
084        }
085        ConfigureManager cm = InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
086        if (cm != null) {
087            cm.registerPref(new ProgrammerConfigPane());
088        }
089        return result;
090    }
091
092    /**
093     * Update static data from XML file
094     *
095     * @param element Top level Element to unpack.
096     * @param o       ignored
097     */
098    @Override
099    public void load(Element element, Object o) {
100        log.warn("unexpected call of 2nd load form");
101    }
102    // initialize logging
103    private final static Logger log = LoggerFactory.getLogger(ProgrammerConfigPaneXml.class);
104
105}