001package jmri.jmrit.display.controlPanelEditor.shape.configurexml;
002
003import jmri.configurexml.JmriConfigureXmlException;
004import jmri.jmrit.display.Editor;
005import jmri.jmrit.display.Positionable;
006import jmri.jmrit.display.controlPanelEditor.shape.PositionableEllipse;
007
008import org.jdom2.Element;
009
010/**
011 * Handle configuration for display.PositionableShape objects
012 *
013 * @author Pete Cressman Copyright (c) 2012
014 */
015public class PositionableEllipseXml extends PositionableShapeXml {
016
017    public PositionableEllipseXml() {
018    }
019
020    /**
021     * Default implementation for storing the contents of a PositionableShape
022     *
023     * @param o Object to store, of type PositionableShape
024     * @return Element containing the complete info
025     */
026    @Override
027    public Element store(Object o) {
028        PositionableEllipse p = (PositionableEllipse) o;
029
030        if (!p.isActive()) {
031            return null;  // if flagged as inactive, don't store
032        }
033        Element element = new Element("positionableEllipse");
034        storeCommonAttributes(p, element);
035
036        Element elem = new Element("size");
037        elem.setAttribute("width", "" + p.getWidth());
038        elem.setAttribute("height", "" + p.getHeight());
039        element.addContent(elem);
040
041        element.setAttribute("class", "jmri.jmrit.display.controlPanelEditor.shape.configurexml.PositionableEllipseXml");
042        return element;
043    }
044
045    /**
046     * Create a PositionableShape, then add to a target JLayeredPane
047     *
048     * @param element Top level Element to unpack.
049     * @param o       Editor as an Object
050     */
051    @Override
052    public void load(Element element, Object o) throws JmriConfigureXmlException {
053        // create the objects
054        Editor ed = (Editor) o;
055        PositionableEllipse ps = new PositionableEllipse(ed);
056
057        Element elem = element.getChild("size");
058        ps.setWidth(getInt(elem, "width"));
059        ps.setHeight(getInt(elem, "height"));
060
061        try {
062            ed.putItem(ps);
063        } catch (Positionable.DuplicateIdException e) {
064            throw new JmriConfigureXmlException("Positionable id is not unique", e);
065        }
066        // load individual item's option settings after editor has set its global settings
067        loadCommonAttributes(ps, Editor.MARKERS, element);
068    }
069}