001package jmri.jmrix.dccpp.swing.messages.configurexml;
002
003import java.awt.Dimension;
004import java.util.List;
005
006import jmri.configurexml.JmriConfigureXmlException;
007import jmri.jmrix.dccpp.DCCppSystemConnectionMemo;
008import jmri.jmrix.dccpp.swing.messages.MessagesConfiguration;
009import jmri.util.ColorUtil;
010
011import org.jdom2.Element;
012
013/** Handle configuration for MessagesConfiguration objects. */
014public class MessagesConfigurationXml {
015
016    public static Element store(MessagesConfiguration p) {
017
018        Element element = new Element("dccex_messages_config");
019
020        if (p.getMemo() != null) {
021            element.addContent(new Element("systemConnection")
022                    .addContent(p.getMemo().getSystemPrefix()));
023        }
024        element.addContent(new Element("expirySeconds")
025                .addContent(Integer.toString(p.getExpirySeconds())));
026        element.addContent(new Element("maxLines")
027                .addContent(Integer.toString(p.getMaxLines())));
028        element.addContent(new Element("autoOpen")
029                .addContent(p.isAutoOpen() ? "true" : "false"));
030        element.addContent(new Element("playSound")
031                .addContent(p.isPlaySound() ? "true" : "false"));
032        element.addContent(new Element("soundFile")
033                .addContent(p.getSoundFile()));
034        element.addContent(new Element("foreground")
035                .addContent(ColorUtil.colorToColorName(p.getMessageForeground())));
036        element.addContent(new Element("background")
037                .addContent(ColorUtil.colorToColorName(p.getMessageBackground())));
038        Dimension size = p.getMessagesSize();
039        if (size != null) {
040            element.addContent(new Element("numColumns")
041                    .addContent(Integer.toString(size.width)));
042            element.addContent(new Element("numLines")
043                    .addContent(Integer.toString(size.height)));
044        }
045
046        return element;
047    }
048
049    public static void load(MessagesConfiguration p, Element elem, boolean ignoreMemo)
050            throws JmriConfigureXmlException {
051
052        Element element = elem.getChild("dccex_messages_config");
053
054        if (!ignoreMemo) {
055            DCCppSystemConnectionMemo memo = null;
056            String systemConnectionName = "Unknown connection";
057            Element systemConnection = element.getChild("systemConnection");
058            if (systemConnection != null) {
059                systemConnectionName = systemConnection.getTextTrim();
060                List<DCCppSystemConnectionMemo> systemConnections =
061                        jmri.InstanceManager.getList(DCCppSystemConnectionMemo.class);
062                for (DCCppSystemConnectionMemo m : systemConnections) {
063                    if (m.getSystemPrefix().equals(systemConnectionName)) {
064                        memo = m;
065                        break;
066                    }
067                }
068            }
069            if (memo == null) {
070                throw new JmriConfigureXmlException("Cannot find connection: " + systemConnectionName);
071            }
072            p.setMemo(memo);
073        }
074
075        String value = element.getChildTextTrim("expirySeconds");
076        if (value != null) {
077            p.setExpirySeconds(Integer.parseInt(value));
078        }
079        value = element.getChildTextTrim("maxLines");
080        if (value != null) {
081            p.setMaxLines(Integer.parseInt(value));
082        }
083        value = element.getChildTextTrim("autoOpen");
084        if (value != null) {
085            p.setAutoOpen("true".equals(value));
086        }
087        value = element.getChildTextTrim("playSound");
088        if (value != null) {
089            p.setPlaySound("true".equals(value));
090        }
091        value = element.getChildTextTrim("soundFile");
092        if (value != null) {
093            p.setSoundFile(value);
094        }
095        value = element.getChildTextTrim("foreground");
096        if (value != null) {
097            p.setMessageForeground(ColorUtil.stringToColor(value));
098        }
099        value = element.getChildTextTrim("background");
100        if (value != null) {
101            p.setMessageBackground(ColorUtil.stringToColor(value));
102        }
103        String numColumns = element.getChildTextTrim("numColumns");
104        String numLines = element.getChildTextTrim("numLines");
105        if (numColumns != null && numLines != null) {
106            p.setMessagesSize(new Dimension(Integer.parseInt(numColumns), Integer.parseInt(numLines)));
107        }
108    }
109
110}