001package jmri.jmrit.roster.configurexml;
002
003import jmri.ConfigureManager;
004import jmri.InstanceManager;
005import jmri.jmrit.roster.Roster;
006import jmri.jmrit.roster.RosterConfigManager;
007import jmri.jmrit.roster.RosterConfigPane;
008import jmri.profile.Profile;
009import jmri.profile.ProfileManager;
010
011import org.jdom2.Element;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * Handle XML persistance of Roster default values.
017 * <p>
018 * This class is named as being the persistant form of the RosterConfigPane
019 * class, but there's no object of that form created when this is read back.
020 * Instead, this persists static members of the roster.Roster class.
021 *
022 * @author Bob Jacobsen Copyright: Copyright (c) 2003
023 */
024public class RosterConfigPaneXml extends jmri.configurexml.AbstractXmlAdapter {
025
026    public RosterConfigPaneXml() {
027    }
028
029    /**
030     * Default implementation for storing the static contents
031     *
032     * @param o Object to store
033     * @return Element containing the complete info
034     */
035    @Override
036    public Element store(Object o) {
037
038        RosterConfigPane p = (RosterConfigPane) o;
039        // any reason to write?
040        if ((p.getSelectedItem() == null || p.getSelectedItem().isEmpty())
041                && p.getDefaultOwner().isEmpty()) {
042            return null;
043        }
044
045        // create and write element
046        Element roster = new Element("roster");
047        if (p.getSelectedItem() != null && !p.getSelectedItem().isEmpty()) {
048            roster.setAttribute("directory", p.getSelectedItem());
049        }
050        roster.setAttribute("class", this.getClass().getName());
051        roster.setAttribute("ownerDefault", p.getDefaultOwner());
052        return roster;
053    }
054
055    @Override
056    public boolean load(Element shared, Element perNode) {
057        boolean result = true;
058        Profile project = ProfileManager.getDefault().getActiveProfile();
059        if (shared.getAttribute("directory") != null) {
060            InstanceManager.getDefault(RosterConfigManager.class).setDirectory(project, shared.getAttribute("directory").getValue());
061            if (log.isDebugEnabled()) {
062                log.debug("set roster location (1): {}", shared.getAttribute("directory").getValue());
063            }
064        }
065        if (shared.getAttribute("ownerDefault") != null) {
066            InstanceManager.getDefault(RosterConfigManager.class).setDefaultOwner(project, shared.getAttribute("ownerDefault").getValue());
067        }
068        ConfigureManager cm = InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
069        if (cm != null) {
070            cm.registerPref(new RosterConfigPane());
071        }
072        return result;
073    }
074
075    /**
076     * Update static data from XML file
077     *
078     * @param element Top level Element to unpack.
079     * @param o       ignored
080     */
081    @Override
082    public void load(Element element, Object o) {
083        if (log.isDebugEnabled()) {
084            log.debug("set roster location (2): {}", element.getAttribute("directory").getValue());
085        }
086        if (element.getAttribute("directory") != null) {
087            Roster.getDefault().setRosterLocation(element.getAttribute("directory").getValue());
088        }
089        ConfigureManager cm = InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
090        if (cm != null) {
091            cm.registerPref(new RosterConfigPane());
092        }
093    }
094    // initialize logging
095    private final static Logger log = LoggerFactory.getLogger(RosterConfigPaneXml.class);
096
097}