001package apps.configurexml;
002
003import apps.SystemConsoleConfigPanel;
004import apps.systemconsole.SystemConsolePreferencesManager;
005import jmri.ConfigureManager;
006import jmri.InstanceManager;
007import org.jdom2.Element;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Handle XML persistence of SystemConsoleConfigPanel objects.
013 * <hr>
014 * This file is part of JMRI.
015 * <p>
016 * JMRI is free software; you can redistribute it and/or modify it under the
017 * terms of version 2 of the GNU General Public License as published by the Free
018 * Software Foundation. See the "COPYING" file for a copy of this license.
019 * <p>
020 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
021 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
022 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
023 *
024 * @author Matthew Harris copyright (c) 2010
025 * @see apps.SystemConsoleConfigPanel
026 */
027public class SystemConsoleConfigPanelXml extends jmri.configurexml.AbstractXmlAdapter {
028
029    public SystemConsoleConfigPanelXml() {
030    }
031
032    /**
033     * Arrange for console settings to be stored
034     *
035     * @param o Object to store, of type SystemConsole
036     * @return Element containing the complete info
037     */
038    @Override
039    public Element store(Object o) {
040
041        Element e = new Element("console");
042        e.setAttribute("class", this.getClass().getName());
043        SystemConsolePreferencesManager manager = InstanceManager.getDefault(SystemConsolePreferencesManager.class);
044        e.setAttribute("scheme", "" + manager.getScheme());
045        e.setAttribute("fontsize", "" + manager.getFontSize());
046        e.setAttribute("fontstyle", "" + manager.getFontStyle());
047        e.setAttribute("wrapstyle", "" + manager.getWrapStyle());
048
049        return e;
050    }
051
052    /**
053     * Object should be loaded after basic GUI constructed
054     *
055     * @return true to defer loading
056     * @see jmri.configurexml.AbstractXmlAdapter#loadDeferred()
057     * @see jmri.configurexml.XmlAdapter#loadDeferred()
058     */
059    @Override
060    public boolean loadDeferred() {
061        return true;
062    }
063
064    @Override
065    public boolean load(Element shared, Element perNode) {
066        boolean result = true;
067        String value;
068        SystemConsolePreferencesManager manager = InstanceManager.getDefault(SystemConsolePreferencesManager.class);
069
070        try {
071            if ((value = shared.getAttributeValue("scheme")) != null) {
072                manager.setScheme(Integer.parseInt(value));
073            }
074
075            if ((value = shared.getAttributeValue("fontsize")) != null) {
076                manager.setFontSize(Integer.parseInt(value));
077            }
078
079            if ((value = shared.getAttributeValue("fontstyle")) != null) {
080                manager.setFontStyle(Integer.parseInt(value));
081            }
082
083            if ((value = shared.getAttributeValue("wrapstyle")) != null) {
084                manager.setWrapStyle(Integer.parseInt(value));
085            }
086
087        } catch (NumberFormatException ex) {
088            log.error("NumberFormatException while setting System Console parameters", ex);
089            result = false;
090        }
091
092        // As we've had a load request, register the system console with the
093        // preference manager
094        ConfigureManager cm = jmri.InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
095        if (cm != null) {
096            cm.registerPref(new SystemConsoleConfigPanel());
097        }
098
099        return result;
100    }
101
102    // initialize logging
103    private static final Logger log = LoggerFactory.getLogger(SystemConsoleConfigPanelXml.class);
104
105}