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.PositionableRectangle;
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 PositionableRectangleXml extends PositionableShapeXml {
016
017    public PositionableRectangleXml() {
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        PositionableRectangle p = (PositionableRectangle) o;
029
030        if (!p.isActive()) {
031            return null;  // if flagged as inactive, don't store
032        }
033        Element element = new Element("positionableRectangle");
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.PositionableRectangleXml");
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     * @throws JmriConfigureXmlException when a error prevents creating the objects as as
051     *                   required by the input XML
052     */
053    @Override
054    public void load(Element element, Object o) throws JmriConfigureXmlException {
055        // create the objects
056        Editor ed = (Editor) o;
057        PositionableRectangle ps = new PositionableRectangle(ed);
058
059        Element elem = element.getChild("size");
060        ps.setWidth(getInt(elem, "width"));
061        ps.setHeight(getInt(elem, "height"));
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}