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