001package jmri.jmrit.display.layoutEditor.configurexml;
002
003import java.awt.geom.Point2D;
004import jmri.jmrit.display.layoutEditor.LayoutEditor;
005import jmri.jmrit.display.layoutEditor.LevelXing;
006import jmri.jmrit.display.layoutEditor.LevelXingView;
007import jmri.jmrit.display.layoutEditor.TrackSegment;
008import org.jdom2.Attribute;
009import org.jdom2.DataConversionException;
010import org.jdom2.Element;
011
012/**
013 * This module handles configuration for display.LevelXing objects for a
014 * LayoutEditor.
015 *
016 * @author David Duchamp Copyright (c) 2007
017 * @author George Warner Copyright (c) 2017-2019
018 */
019public class LevelXingViewXml extends LayoutTrackViewXml {
020
021    public LevelXingViewXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a LevelXing
026     *
027     * @param o Object to store, of type LevelXing
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032
033
034        LevelXingView lv = (LevelXingView) o;
035        LevelXing lt = lv.getLevelXing();
036
037        Element element = new Element("levelxing");
038
039        // include attributes
040        element.setAttribute("ident", lt.getId());
041        if (!lt.getBlockNameAC().isEmpty()) {
042            element.setAttribute("blocknameac", lt.getBlockNameAC());
043        }
044        if (!lt.getBlockNameBD().isEmpty()) {
045            element.setAttribute("blocknamebd", lt.getBlockNameBD());
046        }
047        if (lt.getConnectA() != null) {
048            element.setAttribute("connectaname", ((TrackSegment) lt.getConnectA()).getId());
049        }
050        if (lt.getConnectB() != null) {
051            element.setAttribute("connectbname", ((TrackSegment) lt.getConnectB()).getId());
052        }
053        if (lt.getConnectC() != null) {
054            element.setAttribute("connectcname", ((TrackSegment) lt.getConnectC()).getId());
055        }
056        if (lt.getConnectD() != null) {
057            element.setAttribute("connectdname", ((TrackSegment) lt.getConnectD()).getId());
058        }
059        if (lv.isHidden()) {
060            element.setAttribute("hidden", "yes");
061        }
062        if (!lt.getSignalAName().isEmpty()) {
063            element.setAttribute("signalaname", lt.getSignalAName());
064        }
065        if (!lt.getSignalBName().isEmpty()) {
066            element.setAttribute("signalbname", lt.getSignalBName());
067        }
068        if (!lt.getSignalCName().isEmpty()) {
069            element.setAttribute("signalcname", lt.getSignalCName());
070        }
071        if (!lt.getSignalDName().isEmpty()) {
072            element.setAttribute("signaldname", lt.getSignalDName());
073        }
074        Point2D coords = lv.getCoordsCenter();
075        element.setAttribute("xcen", "" + coords.getX());
076        element.setAttribute("ycen", "" + coords.getY());
077        coords = lv.getCoordsA();
078        element.setAttribute("xa", "" + coords.getX());
079        element.setAttribute("ya", "" + coords.getY());
080        coords = lv.getCoordsB();
081        element.setAttribute("xb", "" + coords.getX());
082        element.setAttribute("yb", "" + coords.getY());
083
084        if (!lt.getSignalAMastName().isEmpty()) {
085            element.addContent(new Element("signalAMast").addContent(lt.getSignalAMastName()));
086        }
087
088        if (!lt.getSignalBMastName().isEmpty()) {
089            element.addContent(new Element("signalBMast").addContent(lt.getSignalBMastName()));
090        }
091        if (!lt.getSignalCMastName().isEmpty()) {
092            element.addContent(new Element("signalCMast").addContent(lt.getSignalCMastName()));
093        }
094        if (!lt.getSignalDMastName().isEmpty()) {
095            element.addContent(new Element("signalDMast").addContent(lt.getSignalDMastName()));
096        }
097
098        if (!lt.getSensorAName().isEmpty()) {
099            element.addContent(new Element("sensorA").addContent(lt.getSensorAName()));
100        }
101
102        if (!lt.getSensorBName().isEmpty()) {
103            element.addContent(new Element("sensorB").addContent(lt.getSensorBName()));
104        }
105        if (!lt.getSensorCName().isEmpty()) {
106            element.addContent(new Element("sensorC").addContent(lt.getSensorCName()));
107        }
108        if (!lt.getSensorDName().isEmpty()) {
109            element.addContent(new Element("sensorD").addContent(lt.getSensorDName()));
110        }
111
112        storeLogixNG_Data(lv, element);
113        element.setAttribute("class", "jmri.jmrit.display.layoutEditor.configurexml.LevelXingXml"); // temporary // getClass().getName());
114        return element;
115    }
116
117    @Override
118    public boolean load(Element shared, Element perNode) {
119        log.error("Invalid method called");
120        return false;
121    }
122
123    /**
124     * Load, starting with the levelxing element, then all the other data
125     *
126     * @param element Top level Element to unpack.
127     * @param o       LayoutEditor as an Object
128     */
129    @Override
130    public void load(Element element, Object o) {
131        // create the objects
132        LayoutEditor p = (LayoutEditor) o;
133
134        // get center point
135        String name = element.getAttribute("ident").getValue();
136        double x = 0.0;
137        double y = 0.0;
138        try {
139            x = element.getAttribute("xcen").getFloatValue();
140            y = element.getAttribute("ycen").getFloatValue();
141        } catch (org.jdom2.DataConversionException e) {
142            log.error("failed to convert levelxing center  attribute");
143        }
144
145        // create the new LevelXing
146        LevelXing lt = new LevelXing(name, p);
147        LevelXingView lv = new LevelXingView(lt, new Point2D.Double(x, y), p);
148
149        // get remaining attributes
150        Attribute a = element.getAttribute("blocknameac");
151        if (a != null) {
152            lt.tLayoutBlockNameAC = a.getValue();
153        }
154        a = element.getAttribute("blocknamebd");
155        if (a != null) {
156            lt.tLayoutBlockNameBD = a.getValue();
157        }
158        a = element.getAttribute("connectaname");
159        if (a != null) {
160            lt.connectAName = a.getValue();
161        }
162        a = element.getAttribute("connectbname");
163        if (a != null) {
164            lt.connectBName = a.getValue();
165        }
166        a = element.getAttribute("connectcname");
167        if (a != null) {
168            lt.connectCName = a.getValue();
169        }
170        a = element.getAttribute("connectdname");
171        if (a != null) {
172            lt.connectDName = a.getValue();
173        }
174        a = element.getAttribute("signalaname");
175        if (a != null) {
176            lt.setSignalAName(a.getValue());
177        }
178        a = element.getAttribute("signalbname");
179        if (a != null) {
180            lt.setSignalBName(a.getValue());
181        }
182        a = element.getAttribute("signalcname");
183        if (a != null) {
184            lt.setSignalCName(a.getValue());
185        }
186        a = element.getAttribute("signaldname");
187        if (a != null) {
188            lt.setSignalDName(a.getValue());
189        }
190
191        try {
192            x = element.getAttribute("xa").getFloatValue();
193            y = element.getAttribute("ya").getFloatValue();
194        } catch (org.jdom2.DataConversionException e) {
195            log.error("failed to convert levelxing a coords attribute");
196        }
197        lv.setCoordsA(new Point2D.Double(x, y));
198
199        try {
200            x = element.getAttribute("xb").getFloatValue();
201            y = element.getAttribute("yb").getFloatValue();
202        } catch (org.jdom2.DataConversionException e) {
203            log.error("failed to convert levelxing b coords attribute");
204        }
205        lv.setCoordsB(new Point2D.Double(x, y));
206
207        try {
208            lv.setHidden(element.getAttribute("hidden").getBooleanValue());
209        } catch (DataConversionException e1) {
210            log.warn("unable to convert levelxing hidden attribute");
211        } catch (NullPointerException e) {  // considered normal if the attribute is not present
212        }
213
214        if (element.getChild("signalAMast") != null) {
215            String mast = element.getChild("signalAMast").getText();
216            if (mast != null && !mast.isEmpty()) {
217                lt.setSignalAMast(mast);
218            }
219        }
220
221        if (element.getChild("signalBMast") != null) {
222            String mast = element.getChild("signalBMast").getText();
223            if (mast != null && !mast.isEmpty()) {
224                lt.setSignalBMast(mast);
225            }
226        }
227
228        if (element.getChild("signalCMast") != null) {
229            String mast = element.getChild("signalCMast").getText();
230            if (mast != null && !mast.isEmpty()) {
231                lt.setSignalCMast(mast);
232            }
233        }
234
235        if (element.getChild("signalDMast") != null) {
236            String mast = element.getChild("signalDMast").getText();
237            if (mast != null && !mast.isEmpty()) {
238                lt.setSignalDMast(mast);
239            }
240        }
241
242        if (element.getChild("sensorA") != null) {
243            String sensor = element.getChild("sensorA").getText();
244            if (sensor != null && !sensor.isEmpty()) {
245                lt.setSensorAName(sensor);
246            }
247        }
248
249        if (element.getChild("sensorB") != null) {
250            String sensor = element.getChild("sensorB").getText();
251            if (sensor != null && !sensor.isEmpty()) {
252                lt.setSensorBName(sensor);
253            }
254        }
255
256        if (element.getChild("sensorC") != null) {
257            String sensor = element.getChild("sensorC").getText();
258            if (sensor != null && !sensor.isEmpty()) {
259                lt.setSensorCName(sensor);
260            }
261        }
262
263        if (element.getChild("sensorD") != null) {
264            String sensor = element.getChild("sensorD").getText();
265            if (sensor != null && !sensor.isEmpty()) {
266                lt.setSensorDName(sensor);
267            }
268        }
269
270        loadLogixNG_Data(lv, element);
271
272        p.addLayoutTrack(lt, lv);
273    }
274
275    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LevelXingViewXml.class);
276}