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