001package jmri.jmrix.bidib.simulator.configurexml;
002
003import jmri.jmrix.SerialPortAdapter;
004//import jmri.jmrix.PortAdapter;
005import jmri.jmrix.configurexml.AbstractConnectionConfigXml;
006import jmri.jmrix.bidib.simulator.ConnectionConfig;
007import jmri.jmrix.bidib.simulator.BiDiBSimulatorAdapter;
008import org.jdom2.Attribute;
009import org.jdom2.Element;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Handle XML persistence of layout connections by persisting the
015 * BiDiBSimulatorAdapter (and connections). Note this is named as the XML version
016 * of a ConnectionConfig object, but it's actually persisting the
017 * BiDiBSimulatorAdapter.
018 * <p>
019 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class
020 * is the one actually registered. Reads are brought here directly via the class
021 * attribute in the XML.
022 *
023 * @author Bob Jacobsen Copyright: Copyright (c) 2003
024 * @author Eckart Meyer Copyright (C) 2019
025 */
026public class ConnectionConfigXml extends AbstractConnectionConfigXml {
027
028    public ConnectionConfigXml() {
029        super();
030    }
031
032    protected SerialPortAdapter adapter;
033
034    /**
035     * A Simulator connection (normaly) needs no extra information, so we reimplement the
036     * superclass method to just write the necessary parts plus the simulator file name.
037     *
038     * @return Formatted element containing no attributes except the class name
039     */
040    @Override
041    public Element store(Object o) {
042        log.debug("store");
043        getInstance(o);
044
045        Element e = new Element("connection");
046        storeCommon(e, adapter);
047
048        if (adapter.getCurrentPortName() != null) {
049            e.setAttribute("simulationFile", ((BiDiBSimulatorAdapter)adapter).getSimulationFile());
050        } else {
051            e.setAttribute("simulationFile", "noneSelected");
052        }
053
054        e.setAttribute("class", this.getClass().getName());
055
056        extendElement(e);
057
058        return e;
059    }
060
061    // TODO: should be reworked ...
062    @Override
063    public boolean load(Element shared, Element perNode) {
064        boolean result = true;
065        // start the "connection"
066        getInstance();
067        log.debug("load, adapter: {}", adapter);
068
069        java.util.List<Attribute> al = perNode.getAttributes();
070        log.debug("load: attr list: {}", al);
071        //Attribute a = perNode.getAttribute("simulationFile");
072        //boolean b = a.isSpecified();
073        String simulationFile = perNode.getAttribute("simulationFile").getValue();
074        ((BiDiBSimulatorAdapter)adapter).setSimulationFile(simulationFile);
075
076        loadCommon(shared, perNode, adapter);
077
078        // register, so can be picked up next time
079        register();
080
081        if (adapter.getDisabled()) {
082            unpackElement(shared, perNode);
083            return result;
084        }
085
086//        // check if the simulation file exists. - CAN'T DO
087//        String status = adapter.openPort(simulationFile, "JMRI app");
088//        if (status != null) {
089//            // indicates an error, return it
090//            handleException(status, "opening connection", null, null, null);
091//            // now force end to operation
092//            log.debug("load failed");
093//            return false;
094//        }
095
096
097        adapter.configure();
098
099        // once all the configure processing has happened, do any
100        // extra config
101        unpackElement(shared, perNode);
102
103        return result;
104    }
105
106    @Override
107    protected void getInstance() {
108        log.debug("BiDiB ConnectionConfigXml.getInstance: {}", adapter);
109        if (adapter == null) {
110            adapter = new BiDiBSimulatorAdapter();
111            log.debug("-- adapter created: {}", adapter);
112        }
113    }
114
115    protected void getInstance(Object object) {
116        adapter = ((ConnectionConfig) object).getAdapter();
117    }
118
119    @Override
120    protected void register() {
121        this.register(new ConnectionConfig(adapter));
122    }
123
124    @Override
125    protected void dispose() {
126        adapter.dispose();
127    }
128
129//    /**
130//     * Customizable method if you need to add anything more
131//     *
132//     * @param e Element being created, update as needed
133//     */
134//    @Override
135//    protected void extendElement(Element e) {
136//        if (adapter.getSystemConnectionMemo() != null) {
137//            e.setAttribute("simulationFile", ((BiDiBSimulatorAdapter)adapter).getSimulationFile());
138//        }
139//    }
140//
141//    @Override
142//    protected void unpackElement(Element shared, Element perNode) {
143//        if (shared.getAttribute("simulationFile") != null) {
144//            ((BiDiBSimulatorAdapter)adapter).setSimulationFile(shared.getAttribute("simulationFile").getValue());
145//        }
146//    }
147    // initialize logging
148    private static final Logger log = LoggerFactory.getLogger(ConnectionConfigXml.class);
149
150}