001package jmri.jmrix.ecos.networkdriver.configurexml;
002
003import java.util.List;
004import jmri.jmrix.configurexml.AbstractNetworkConnectionConfigXml;
005import jmri.jmrix.ecos.EcosPreferences;
006import jmri.jmrix.ecos.networkdriver.ConnectionConfig;
007import jmri.jmrix.ecos.networkdriver.NetworkDriverAdapter;
008import org.jdom2.Element;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Handle XML persistance of layout connections by persistening the
014 * NetworkDriverAdapter (and connections).
015 * <p>
016 * Note this is named as the XML version of a ConnectionConfig object, but it's
017 * actually persisting the NetworkDriverAdapter.
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 (c) 2003, 2008
024 */
025public class ConnectionConfigXml extends AbstractNetworkConnectionConfigXml {
026
027    @Override
028    protected void getInstance() {
029        adapter = new NetworkDriverAdapter();
030    }
031
032    @Override
033    protected void getInstance(Object object) {
034        adapter = ((ConnectionConfig) object).getAdapter();
035    }
036
037    @Override
038    protected void extendElement(Element e) {
039        Element ecosPrefElem = new Element("commandStationPreferences");
040        EcosPreferences p = ((jmri.jmrix.ecos.EcosSystemConnectionMemo) adapter.getSystemConnectionMemo()).getPreferenceManager();
041        if (p == null) {
042            log.warn("Null EcosPrefManager");
043            return;
044        }
045        if (p.getAddTurnoutsToEcos() == 0x01) {
046            ecosPrefElem.setAttribute("addTurnoutToCS", "no");
047        } else if (p.getAddTurnoutsToEcos() == 0x02) {
048            ecosPrefElem.setAttribute("addTurnoutToCS", "yes");
049        }
050
051        if (p.getRemoveTurnoutsFromEcos() == 0x01) {
052            ecosPrefElem.setAttribute("removeTurnoutFromCS", "no");
053        } else if (p.getRemoveTurnoutsFromEcos() == 0x02) {
054            ecosPrefElem.setAttribute("removeTurnoutFromCS", "yes");
055        }
056
057        if (p.getAddTurnoutsToJMRI() == 0x01) {
058            ecosPrefElem.setAttribute("addTurnoutToJMRI", "no");
059        } else if (p.getAddTurnoutsToJMRI() == 0x02) {
060            ecosPrefElem.setAttribute("addTurnoutToJMRI", "yes");
061        }
062
063        if (p.getRemoveTurnoutsFromJMRI() == 0x01) {
064            ecosPrefElem.setAttribute("removeTurnoutFromJMRI", "no");
065        } else if (p.getRemoveTurnoutsFromJMRI() == 0x02) {
066            ecosPrefElem.setAttribute("removeTurnoutFromJMRI", "yes");
067        }
068
069        if (p.getLocoMaster() > 0x00) {
070            ecosPrefElem.setAttribute("locoMaster", p.getLocoMasterAsString());
071        }
072
073        if (p.getAddLocoToEcos() == 0x01) {
074            ecosPrefElem.setAttribute("addLocoToCS", "no");
075        }
076        if (p.getAddLocoToEcos() == 0x02) {
077            ecosPrefElem.setAttribute("addLocoToCS", "yes");
078        }
079
080        if (p.getRemoveLocoFromEcos() == 0x01) {
081            ecosPrefElem.setAttribute("removeLocoFromCS", "no");
082        } else if (p.getRemoveLocoFromEcos() == 0x02) {
083            ecosPrefElem.setAttribute("removeLocoFromCS", "yes");
084        }
085
086        if (p.getAddLocoToJMRI() == 0x01) {
087            ecosPrefElem.setAttribute("addLocoToJMRI", "no");
088        } else if (p.getAddLocoToJMRI() == 0x02) {
089            ecosPrefElem.setAttribute("addLocoToJMRI", "yes");
090        }
091
092        if (p.getRemoveLocoFromJMRI() == 0x01) {
093            ecosPrefElem.setAttribute("removeLocoFromJMRI", "no");
094        } else if (p.getRemoveLocoFromJMRI() == 0x01) { // was 0x02 intended here?
095            ecosPrefElem.setAttribute("removeLocoFromJMRI", "yes");
096        }
097
098        if (p.getAdhocLocoFromEcos() == 0x01) {
099            ecosPrefElem.setAttribute("removeAdhocLocoFromCS", "no");
100        } else if (p.getAdhocLocoFromEcos() == 0x02) {
101            ecosPrefElem.setAttribute("removeAdhocLocoFromCS", "yes");
102        }
103
104        if (p.getForceControlFromEcos() == 0x01) {
105            ecosPrefElem.setAttribute("forceControlFromCS", "no");
106        } else if (p.getForceControlFromEcos() == 0x02) {
107            ecosPrefElem.setAttribute("forceControlCS", "yes");
108        }
109
110        //if(!p.getDefaultEcosProtocol().equals("DCC128")) ecosPrefElem.setAttribute("defaultCSProtocol", p.getDefaultEcosProtocol());
111        if (p.getEcosLocoDescription() != null) {
112            if (!p.getEcosLocoDescription().equals("")) {
113                ecosPrefElem.setAttribute("defaultCSLocoDescription", p.getEcosLocoDescription());
114            }
115        }
116
117        if (p.getLocoControl()) {
118            ecosPrefElem.setAttribute("locoControl", "force");
119        }
120
121        ecosPrefElem.setAttribute("ecosRosterAttribute", p.getRosterAttribute());
122        e.addContent(ecosPrefElem);
123    }
124
125    @Override
126    protected void unpackElement(Element shared, Element perNode) {
127        List<Element> ecosPref = shared.getChildren("commandStationPreferences");
128        EcosPreferences p = ((jmri.jmrix.ecos.EcosSystemConnectionMemo) adapter.getSystemConnectionMemo()).getPreferenceManager();
129        if (p == null) {
130            log.warn("Null EcosPrefManager");
131            return;
132        }
133        for (Element element : ecosPref) {
134            if (element.getAttribute("addTurnoutToCS") != null) {
135                String yesno = element.getAttribute("addTurnoutToCS").getValue();
136                if ((yesno != null) && (!yesno.equals(""))) {
137                    if (yesno.equals("yes")) {
138                        p.setAddTurnoutsToEcos(0x02);
139                    } else if (yesno.equals("no")) {
140                        p.setAddTurnoutsToEcos(0x01);
141                    }
142                }
143            }
144            if (element.getAttribute("removeTurnoutFromCS") != null) {
145                String yesno = element.getAttribute("removeTurnoutFromCS").getValue();
146                if ((yesno != null) && (!yesno.equals(""))) {
147                    if (yesno.equals("yes")) {
148                        p.setRemoveTurnoutsFromEcos(0x02);
149                    } else if (yesno.equals("no")) {
150                        p.setRemoveTurnoutsFromEcos(0x01);
151                    }
152                }
153            }
154
155            if (element.getAttribute("addTurnoutToJMRI") != null) {
156                String yesno = element.getAttribute("addTurnoutToJMRI").getValue();
157                if ((yesno != null) && (!yesno.equals(""))) {
158                    if (yesno.equals("yes")) {
159                        p.setAddTurnoutsToJMRI(0x02);
160                    } else if (yesno.equals("no")) {
161                        p.setAddTurnoutsToJMRI(0x01);
162                    }
163                }
164            }
165
166            if (element.getAttribute("removeTurnoutFromJMRI") != null) {
167                String yesno = element.getAttribute("removeTurnoutFromJMRI").getValue();
168                if ((yesno != null) && (!yesno.equals(""))) {
169                    if (yesno.equals("yes")) {
170                        p.setRemoveTurnoutsFromJMRI(0x02);
171                    } else if (yesno.equals("no")) {
172                        p.setRemoveTurnoutsFromJMRI(0x01);
173                    }
174                }
175            }
176
177            if (element.getAttribute("addLocoToCS") != null) {
178                String yesno = element.getAttribute("addLocoToCS").getValue();
179                if ((yesno != null) && (!yesno.equals(""))) {
180                    if (yesno.equals("yes")) {
181                        p.setAddLocoToEcos(0x02);
182                    } else if (yesno.equals("no")) {
183                        p.setAddLocoToEcos(0x01);
184                    }
185                }
186            }
187
188            if (element.getAttribute("removeLocoFromCS") != null) {
189                String yesno = element.getAttribute("removeLocoFromCS").getValue();
190                if ((yesno != null) && (!yesno.equals(""))) {
191                    if (yesno.equals("yes")) {
192                        p.setRemoveLocoFromEcos(0x02);
193                    } else if (yesno.equals("no")) {
194                        p.setRemoveLocoFromEcos(0x01);
195                    }
196                }
197            }
198
199            if (element.getAttribute("addLocoToJMRI") != null) {
200                String yesno = element.getAttribute("addLocoToJMRI").getValue();
201                if ((yesno != null) && (!yesno.equals(""))) {
202                    if (yesno.equals("yes")) {
203                        p.setAddLocoToJMRI(0x02);
204                    } else if (yesno.equals("no")) {
205                        p.setAddLocoToJMRI(0x01);
206                    }
207                }
208            }
209
210            if (element.getAttribute("removeLocoFromJMRI") != null) {
211                String yesno = element.getAttribute("removeLocoFromJMRI").getValue();
212                if ((yesno != null) && (!yesno.equals(""))) {
213                    if (yesno.equals("yes")) {
214                        p.setRemoveLocoFromJMRI(0x02);
215                    } else if (yesno.equals("no")) {
216                        p.setRemoveLocoFromJMRI(0x01);
217                    }
218                }
219            }
220
221            if (element.getAttribute("removeLocoFromJMRI") != null) {
222                String yesno = element.getAttribute("removeLocoFromJMRI").getValue();
223                if ((yesno != null) && (!yesno.equals(""))) {
224                    if (yesno.equals("yes")) {
225                        p.setRemoveLocoFromJMRI(0x02);
226                    } else if (yesno.equals("no")) {
227                        p.setRemoveLocoFromJMRI(0x01);
228                    }
229                }
230            }
231
232            if (element.getAttribute("locoMaster") != null) {
233                p.setLocoMaster(element.getAttribute("locoMaster").getValue());
234            }
235
236            if (element.getAttribute("removeAdhocLocoFromCS") != null) {
237                String yesno = element.getAttribute("removeAdhocLocoFromCS").getValue();
238                if ((yesno != null) && (!yesno.equals(""))) {
239                    if (yesno.equals("yes")) {
240                        p.setAdhocLocoFromEcos(0x02);
241                    } else if (yesno.equals("no")) {
242                        p.setAdhocLocoFromEcos(0x01);
243                    }
244                }
245            }
246            /*if (ecosPref.get(i).getAttribute("defaultCSProtocol") != null){
247             p.setDefaultEcosProtocol(ecosPref.get(i).getAttribute("defaultCSProtocol").getValue());
248             }*/
249            if (element.getAttribute("defaultCSLocoDescription") != null) {
250                p.setEcosLocoDescription(element.getAttribute("defaultCSLocoDescription").getValue());
251            }
252
253            if (element.getAttribute("ecosRosterAttribute") != null) {
254                p.setRosterAttribute(element.getAttribute("ecosRosterAttribute").getValue());
255            }
256            if (element.getAttribute("locoControl") != null) {
257                p.setLocoControl(true);
258            }
259
260            p.resetChangeMade();
261        }
262        p.setPreferencesLoaded();
263    }
264
265    @Override
266    protected void register() {
267        this.register(new ConnectionConfig(adapter));
268    }
269
270    private final static Logger log = LoggerFactory.getLogger(ConnectionConfigXml.class);
271
272}