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.PositionableCircle;
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 PositionableCircleXml extends PositionableShapeXml {
016
017    public PositionableCircleXml() {
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        PositionableCircle p = (PositionableCircle) o;
029
030        if (!p.isActive()) {
031            return null;  // if flagged as inactive, don't store
032        }
033        Element element = new Element("positionableCircle");
034        storeCommonAttributes(p, element);
035
036        Element elem = new Element("size");
037        elem.setAttribute("radius", "" + p.getWidth());     // actually diameter
038        element.addContent(elem);
039
040        element.setAttribute("class", "jmri.jmrit.display.controlPanelEditor.shape.configurexml.PositionableCircleXml");
041        return element;
042    }
043
044    /**
045     * Create a PositionableShape, then add to a target JLayeredPane
046     *
047     * @param element Top level Element to unpack.
048     * @param o       Editor as an Object
049     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
050     *                   required by the input XML
051     */
052    @Override
053    public void load(Element element, Object o) throws JmriConfigureXmlException {
054        // create the objects
055        Editor ed = (Editor) o;
056        PositionableCircle ps = new PositionableCircle(ed);
057
058        Element elem = element.getChild("size");
059        ps.setWidth(getInt(elem, "radius"));    // actually diameter - too late to change name
060
061        // get object class and determine editor being used
062        try {
063            ed.putItem(ps);
064        } catch (Positionable.DuplicateIdException e) {
065            throw new JmriConfigureXmlException("Positionable id is not unique", e);
066        }
067        // load individual item's option settings after editor has set its global settings
068        loadCommonAttributes(ps, Editor.MARKERS, element);
069    }
070}