001package jmri.jmrit.display.layoutEditor.configurexml;
002
003import java.awt.geom.Point2D;
004import jmri.InstanceManager;
005import jmri.jmrit.display.EditorManager;
006import jmri.jmrit.display.layoutEditor.*;
007import org.jdom2.Attribute;
008import org.jdom2.Element;
009
010import javax.annotation.Nonnull;
011
012/**
013 * This module handles configuration for display.PositionablePoint objects for a
014 * LayoutEditor.
015 *
016 * @author David Duchamp Copyright (c) 2007
017 * @author George Warner Copyright (c) 2017-2018
018 */
019public class PositionablePointViewXml extends LayoutTrackViewXml {
020
021    static final EnumIO<PositionablePoint.PointType> pTypeEnumMap = new EnumIoNamesNumbers<>(PositionablePoint.PointType.class);
022
023    public PositionablePointViewXml() {
024    }
025
026    /**
027     * Default implementation for storing the contents of a PositionablePoint
028     *
029     * @param o Object to store, of type PositionablePoint
030     * @return Element containing the complete info
031     */
032    @Override
033    public Element store(Object o) {
034
035        PositionablePointView pv = (PositionablePointView) o;
036        PositionablePoint p = pv.getPoint();
037
038        Element element = new Element("positionablepoint");
039
040        // include attributes
041        element.setAttribute("ident", p.getId());
042        element.setAttribute("type", pTypeEnumMap.outputFromEnum(p.getType()));
043        Point2D coords = pv.getCoordsCenter();
044        element.setAttribute("x", "" + coords.getX());
045        element.setAttribute("y", "" + coords.getY());
046        if (p.getConnect1() != null) {
047            element.setAttribute("connect1name", p.getConnect1().getId());
048        }
049        if (p.getConnect2() != null) {
050            element.setAttribute("connect2name", p.getConnect2().getId());
051        }
052        if (!p.getEastBoundSignal().isEmpty()) {
053            element.setAttribute("eastboundsignal", p.getEastBoundSignal());
054        }
055        if (!p.getWestBoundSignal().isEmpty()) {
056            element.setAttribute("westboundsignal", p.getWestBoundSignal());
057        }
058
059        if (!p.getEastBoundSignalMastName().isEmpty()) {
060            element.setAttribute("eastboundsignalmast", p.getEastBoundSignalMastName());
061        }
062        if (!p.getWestBoundSignalMastName().isEmpty()) {
063            element.setAttribute("westboundsignalmast", p.getWestBoundSignalMastName());
064        }
065
066        if (!p.getEastBoundSensorName().isEmpty()) {
067            element.setAttribute("eastboundsensor", p.getEastBoundSensorName());
068        }
069        if (!p.getWestBoundSensorName().isEmpty()) {
070            element.setAttribute("westboundsensor", p.getWestBoundSensorName());
071        }
072        if (p.getType() == PositionablePoint.PointType.EDGE_CONNECTOR) {
073            element.setAttribute("linkedpanel", p.getLinkedEditorName());
074            element.setAttribute("linkpointid", p.getLinkedPointId());
075        }
076
077        storeLogixNG_Data(pv, element);
078        element.setAttribute("class", "jmri.jmrit.display.layoutEditor.configurexml.PositionablePointXml");
079        return element;
080    }
081
082    @Override
083    public boolean load(@Nonnull Element shared, Element perNode) {
084        log.error("Invalid method called");
085        return false;
086    }
087
088    /**
089     * Load, starting with the layoutblock element, then all the value-icon
090     * pairs
091     *
092     * @param element Top level Element to unpack.
093     * @param o       LayoutEditor as an Object
094     */
095    @Override
096    public void load(Element element, Object o) {
097        // create the objects
098        LayoutEditor p = (LayoutEditor) o;
099
100        // get attributes
101        String name = element.getAttribute("ident").getValue();
102        PositionablePoint.PointType type = PositionablePoint.PointType.ANCHOR;
103        double x = 0.0;
104        double y = 0.0;
105        try {
106            x = element.getAttribute("x").getFloatValue();
107            y = element.getAttribute("y").getFloatValue();
108            type = pTypeEnumMap.inputFromAttribute(element.getAttribute("type"));
109        } catch (org.jdom2.DataConversionException e) {
110            log.error("failed to convert positionablepoint attribute");
111        }
112
113        // create the new PositionablePoint
114        PositionablePoint l = new PositionablePoint(name, type, p);
115        PositionablePointView pv = new PositionablePointView(l, new Point2D.Double(x, y), p);
116        p.addLayoutTrack(l, pv);
117
118        // get remaining attributes
119        Attribute a = element.getAttribute("connect1name");
120        if (a != null) {
121            l.trackSegment1Name = a.getValue();
122        }
123        a = element.getAttribute("connect2name");
124        if (a != null) {
125            l.trackSegment2Name = a.getValue();
126        }
127        a = element.getAttribute("eastboundsignal");
128        if (a != null) {
129            l.setEastBoundSignal(a.getValue());
130        }
131        a = element.getAttribute("westboundsignal");
132        if (a != null) {
133            l.setWestBoundSignal(a.getValue());
134        }
135        a = element.getAttribute("eastboundsignalmast");
136        if (a != null) {
137            l.setEastBoundSignalMast(a.getValue());
138        }
139        a = element.getAttribute("westboundsignalmast");
140        if (a != null) {
141            l.setWestBoundSignalMast(a.getValue());
142        }
143        a = element.getAttribute("eastboundsensor");
144        if (a != null) {
145            l.setEastBoundSensor(a.getValue());
146        }
147        a = element.getAttribute("westboundsensor");
148        if (a != null) {
149            l.setWestBoundSensor(a.getValue());
150        }
151
152        if (type == PositionablePoint.PointType.EDGE_CONNECTOR && element.getAttribute("linkedpanel") != null && element.getAttribute("linkpointid") != null) {
153            String linkedEditorName = element.getAttribute("linkedpanel").getValue();
154            LayoutEditor linkedEditor = InstanceManager.getDefault(EditorManager.class).get(LayoutEditor.class, linkedEditorName);
155            if (linkedEditor != null) {
156                String linkedPoint = element.getAttribute("linkpointid").getValue();
157                for (PositionablePoint point : linkedEditor.getPositionablePoints()) {
158                    if (point.getType() == PositionablePoint.PointType.EDGE_CONNECTOR && point.getId().equals(linkedPoint)) {
159                        point.setLinkedPoint(l);
160                        l.setLinkedPoint(point);
161                        break;
162                    }
163                }
164            }
165        }
166
167        loadLogixNG_Data(pv, element);
168    }
169
170    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PositionablePointViewXml.class);
171}