001package jmri.jmrix.loconet.hexfile.configurexml;
002
003import java.awt.GraphicsEnvironment;
004import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml;
005import jmri.jmrix.loconet.hexfile.ConnectionConfig;
006import jmri.jmrix.loconet.hexfile.LnHexFilePort;
007import org.jdom2.Element;
008
009/**
010 * Handle XML persistance of layout connections by persistening the HexFIle
011 * LocoNet emuilator (and connections). Note this is named as the XML version of
012 * a ConnectionConfig object, but it's actually persisting the HexFile info.
013 * <p>
014 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class
015 * is the one actually registered. Reads are brought here directly via the class
016 * attribute in the XML.
017 *
018 * @author Bob Jacobsen Copyright: Copyright (c) 2003
019 */
020public class ConnectionConfigXml extends AbstractSerialConnectionConfigXml {
021
022    public ConnectionConfigXml() {
023        super();
024    }
025
026    /**
027     * A HexFile connection needs no extra information, so we reimplement the
028     * superclass method to just write the necessary parts.
029     *
030     * @return Formatted element containing no attributes except the class name
031     */
032    @Override
033    public Element store(Object o) {
034        getInstance(o);
035
036        Element e = new Element("connection"); // NOI18N
037        if (adapter.getSystemConnectionMemo() != null) {
038            e.setAttribute("userName", adapter.getSystemConnectionMemo().getUserName()); // NOI18N
039            e.setAttribute("systemPrefix", adapter.getSystemConnectionMemo().getSystemPrefix()); // NOI18N
040        }
041        if (adapter.getManufacturer() != null) {
042            e.setAttribute("manufacturer", adapter.getManufacturer()); // NOI18N
043        }
044        saveOptions(e, adapter);
045
046        if (adapter.getDisabled()) {
047            e.setAttribute("disabled", "yes"); // NOI18N
048        } else {
049            e.setAttribute("disabled", "no"); // NOI18N
050        }
051
052        e.setAttribute("class", this.getClass().getName()); // NOI18N
053
054        return e;
055    }
056
057    @Override
058    public boolean load(Element shared, Element perNode) {
059        jmri.jmrix.loconet.hexfile.HexFileFrame f = null;
060        jmri.jmrix.loconet.hexfile.HexFileServer hfs = null;
061
062        getInstance();
063        // hex file has no options in the XML
064
065        // create GUI, unless running in headless mode
066        if (!GraphicsEnvironment.isHeadless()) {
067            f = new jmri.jmrix.loconet.hexfile.HexFileFrame();
068            f.setAdapter((LnHexFilePort) adapter);
069            try {
070                f.initComponents();
071            } catch (Exception ex) {
072                //log.error("starting HexFileFrame exception: "+ex.toString());
073            }
074            f.pack();
075            f.setVisible(true);
076        } else {  // create and configure the headless server
077            hfs = new jmri.jmrix.loconet.hexfile.HexFileServer();
078            hfs.setAdapter((LnHexFilePort) adapter);
079        }
080
081        if (shared.getAttribute("option1") != null) { // NOI18N
082            String option1Setting = shared.getAttribute("option1").getValue(); // NOI18N
083            adapter.configureOption1(option1Setting);
084        }
085        if (shared.getAttribute("option2") != null) { // NOI18N
086            String option2Setting = shared.getAttribute("option2").getValue(); // NOI18N
087            adapter.configureOption2(option2Setting);
088        }
089        if (shared.getAttribute("option3") != null) { // NOI18N
090            String option3Setting = shared.getAttribute("option3").getValue(); // NOI18N
091            adapter.configureOption3(option3Setting);
092        }
093        if (shared.getAttribute("option4") != null) { // NOI18N
094            String option4Setting = shared.getAttribute("option4").getValue(); // NOI18N
095            adapter.configureOption4(option4Setting);
096        }
097        loadOptions(shared.getChild("options"), perNode.getChild("options"), adapter); // NOI18N
098        String manufacturer;
099        try {
100            manufacturer = shared.getAttribute("manufacturer").getValue(); // NOI18N
101            adapter.setManufacturer(manufacturer);
102        } catch (NullPointerException ex) { //Considered normal if not present
103
104        }
105        if (adapter.getSystemConnectionMemo() != null) {
106            if (shared.getAttribute("userName") != null) { // NOI18N
107                adapter.getSystemConnectionMemo().setUserName(shared.getAttribute("userName").getValue()); // NOI18N
108            }
109
110            if (shared.getAttribute("systemPrefix") != null) { // NOI18N
111                adapter.getSystemConnectionMemo().setSystemPrefix(shared.getAttribute("systemPrefix").getValue()); // NOI18N
112            }
113        }
114        if (shared.getAttribute("disabled") != null) { // NOI18N
115            String yesno = shared.getAttribute("disabled").getValue(); // NOI18N
116            if ((yesno != null) && (!yesno.equals(""))) {
117                if (yesno.equals("no")) { // NOI18N
118                    adapter.setDisabled(false);
119                } else if (yesno.equals("yes")) { // NOI18N
120                    adapter.setDisabled(true);
121                }
122            }
123        }
124
125        // register, so can be picked up
126        register();
127        if (adapter.getDisabled()) {
128            if (!GraphicsEnvironment.isHeadless() && f != null) {
129                f.setVisible(false);
130            }
131            return true;
132        }
133        if (!GraphicsEnvironment.isHeadless() && f != null) {
134            f.configure();
135        } else if (hfs != null) {
136            hfs.configure();
137        }
138        return true;
139    }
140
141    @Override
142    protected void getInstance(Object object) {
143        adapter = ((ConnectionConfig) object).getAdapter();
144    }
145
146    @Override
147    protected void getInstance() {
148        adapter = new LnHexFilePort();
149    }
150
151    @Override
152    protected void register() {
153        this.register(new ConnectionConfig(adapter));
154    }
155
156}