Search in sources :

Example 51 with DataConversionException

use of org.jdom2.DataConversionException in project JMRI by JMRI.

the class RpsPositionIconXml method load.

/**
     * Create a PositionableLabel, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    Editor ed = (Editor) o;
    RpsPositionIcon l = new RpsPositionIcon(ed);
    // create the objects
    String name = element.getAttribute("active").getValue();
    NamedIcon active = NamedIcon.getIconByName(name);
    if (active == null) {
        active = ed.loadFailed("RpsPositionIcon: icon \"active\" ", name);
        if (active == null) {
            log.info("RpsPositionIcon: icon \"active\" removed for url= " + name);
            return;
        }
    }
    l.setActiveIcon(active);
    name = element.getAttribute("error").getValue();
    NamedIcon error = NamedIcon.getIconByName(name);
    if (error == null) {
        error = ed.loadFailed("RpsPositionIcon: icon \"error\" ", name);
        if (error == null) {
            log.info("RpsPositionIcon: \"error\" removed for url= " + name);
            return;
        }
    }
    l.setErrorIcon(error);
    try {
        Attribute a = element.getAttribute("rotate");
        if (a != null) {
            int rotation = element.getAttribute("rotate").getIntValue();
            active.setRotation(rotation, l);
            error.setRotation(rotation, l);
        }
    } catch (org.jdom2.DataConversionException e) {
    }
    Attribute a = element.getAttribute("momentary");
    if ((a != null) && a.getValue().equals("true")) {
        l.setMomentary(true);
    } else {
        l.setMomentary(false);
    }
    a = element.getAttribute("showid");
    if ((a != null) && a.getValue().equals("true")) {
        l.setShowID(true);
    } else {
        l.setShowID(false);
    }
    a = element.getAttribute("filter");
    if (a != null) {
        l.setFilter(a.getValue());
    }
    double sxScale = 0.;
    double syScale = 0.;
    int sxOrigin = 0;
    int syOrigin = 0;
    try {
        sxScale = element.getAttribute("sxscale").getDoubleValue();
        syScale = element.getAttribute("syscale").getDoubleValue();
        sxOrigin = element.getAttribute("sxorigin").getIntValue();
        syOrigin = element.getAttribute("syorigin").getIntValue();
    } catch (NullPointerException e1) {
        log.error("missing transform attribute");
    } catch (org.jdom2.DataConversionException e2) {
        log.error("failed to convert transform attributes");
    }
    l.setTransform(sxScale, syScale, sxOrigin, syOrigin);
    NamedIcon icon = loadIcon(l, "active", element, "RpsPositionIcon ", ed);
    if (icon != null) {
        l.setActiveIcon(icon);
    }
    icon = loadIcon(l, "error", element, "RpsPositionIcon ", ed);
    if (icon != null) {
        l.setErrorIcon(icon);
    }
    ed.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.SENSORS, element);
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Attribute(org.jdom2.Attribute) Editor(jmri.jmrit.display.Editor) RpsPositionIcon(jmri.jmrit.display.RpsPositionIcon)

Example 52 with DataConversionException

use of org.jdom2.DataConversionException in project JMRI by JMRI.

the class MemoryIconXml method load.

/**
     * Load, starting with the memoryicon element, then all the value-icon pairs
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    Editor ed = null;
    MemoryIcon l;
    if (o instanceof LayoutEditor) {
        ed = (LayoutEditor) o;
        l = new jmri.jmrit.display.layoutEditor.MemoryIcon("   ", (LayoutEditor) ed);
    } else if (o instanceof jmri.jmrit.display.Editor) {
        ed = (Editor) o;
        l = new MemoryIcon("", ed);
    } else {
        log.error("Unrecognizable class - " + o.getClass().getName());
        return;
    }
    String name;
    Attribute attr = element.getAttribute("memory");
    if (attr == null) {
        log.error("incorrect information for a memory location; must use memory name");
        ed.loadFailed();
        return;
    } else {
        name = attr.getValue();
    }
    loadTextInfo(l, element);
    Memory m = jmri.InstanceManager.memoryManagerInstance().getMemory(name);
    if (m != null) {
        l.setMemory(name);
    } else {
        log.error("Memory named '" + attr.getValue() + "' not found.");
        ed.loadFailed();
    }
    Attribute a = element.getAttribute("selectable");
    if (a != null && a.getValue().equals("yes")) {
        l.setSelectable(true);
    } else {
        l.setSelectable(false);
    }
    a = element.getAttribute("updateBlockValue");
    if (a != null && a.getValue().equals("yes")) {
        l.updateBlockValueOnChange(true);
    }
    // get the icon pairs
    List<Element> items = element.getChildren("memorystate");
    for (int i = 0; i < items.size(); i++) {
        // get the class, hence the adapter object to do loading
        Element item = items.get(i);
        String iconName = item.getAttribute("icon").getValue();
        NamedIcon icon = NamedIcon.getIconByName(iconName);
        if (icon == null) {
            icon = ed.loadFailed("Memory " + name, iconName);
            if (icon == null) {
                log.info("Memory \"" + name + "\" icon removed for url= " + iconName);
            }
        }
        if (icon != null) {
            String keyValue = item.getAttribute("value").getValue();
            l.addKeyAndIcon(icon, keyValue);
        }
    }
    ed.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.MEMORIES, element);
    int x = 0;
    int y = 0;
    try {
        x = element.getAttribute("x").getIntValue();
        y = element.getAttribute("y").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert positional attribute");
    }
    l.setOriginalLocation(x, y);
    l.displayState();
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) MemoryIcon(jmri.jmrit.display.MemoryIcon) Attribute(org.jdom2.Attribute) Memory(jmri.Memory) Element(org.jdom2.Element) Editor(jmri.jmrit.display.Editor) LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) Editor(jmri.jmrit.display.Editor)

Example 53 with DataConversionException

use of org.jdom2.DataConversionException in project JMRI by JMRI.

the class MemoryInputIconXml method load.

/**
     * Load, starting with the memoryInputIcon element, then all the value-icon
     * pairs
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    // create the objects
    Editor p = (Editor) o;
    int nCol = 2;
    try {
        nCol = element.getAttribute("colWidth").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert colWidth attribute");
    }
    MemoryInputIcon l = new MemoryInputIcon(nCol, p);
    loadTextInfo(l, element);
    String name;
    Attribute attr = element.getAttribute("memory");
    if (attr == null) {
        log.error("incorrect information for a memory location; must use memory name");
        p.loadFailed();
        return;
    } else {
        name = attr.getValue();
    }
    Memory m = jmri.InstanceManager.memoryManagerInstance().getMemory(name);
    if (m != null) {
        l.setMemory(name);
    } else {
        log.error("Memory named '" + attr.getValue() + "' not found.");
        p.loadFailed();
        return;
    }
    p.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.MEMORIES, element);
    javax.swing.JComponent textField = l.getTextComponent();
    jmri.jmrit.display.PositionablePopupUtil util = l.getPopupUtility();
    if (util.hasBackground()) {
        textField.setBackground(util.getBackground());
    } else {
        textField.setBackground(null);
        textField.setOpaque(false);
    }
}
Also used : Attribute(org.jdom2.Attribute) Memory(jmri.Memory) MemoryInputIcon(jmri.jmrit.display.MemoryInputIcon) Editor(jmri.jmrit.display.Editor)

Example 54 with DataConversionException

use of org.jdom2.DataConversionException in project JMRI by JMRI.

the class PositionableLabelXml method loadTextInfo.

protected void loadTextInfo(Positionable l, Element element) {
    if (log.isDebugEnabled()) {
        log.debug("loadTextInfo");
    }
    jmri.jmrit.display.PositionablePopupUtil util = l.getPopupUtility();
    if (util == null) {
        log.warn("PositionablePopupUtil is null! " + element.toString());
        return;
    }
    Attribute a = element.getAttribute("size");
    try {
        if (a != null) {
            util.setFontSize(a.getFloatValue());
        }
    } catch (DataConversionException ex) {
        log.warn("invalid size attribute value");
    }
    a = element.getAttribute("style");
    try {
        if (a != null) {
            int style = a.getIntValue();
            int drop = 0;
            switch(style) {
                case 0:
                    //0 Normal
                    drop = 1;
                    break;
                case 2:
                    //italic
                    drop = 1;
                    break;
                default:
                    // fall through
                    break;
            }
            util.setFontStyle(style, drop);
        }
    } catch (DataConversionException ex) {
        log.warn("invalid style attribute value");
    }
    // set color if needed
    try {
        int red = element.getAttribute("red").getIntValue();
        int blue = element.getAttribute("blue").getIntValue();
        int green = element.getAttribute("green").getIntValue();
        util.setForeground(new Color(red, green, blue));
    } catch (org.jdom2.DataConversionException e) {
        log.warn("Could not parse color attributes!");
    } catch (NullPointerException e) {
    // considered normal if the attributes are not present
    }
    a = element.getAttribute("hasBackground");
    if (a != null) {
        util.setHasBackground("yes".equals(a.getValue()));
    } else {
        util.setHasBackground(true);
    }
    if (util.hasBackground()) {
        try {
            int red = element.getAttribute("redBack").getIntValue();
            int blue = element.getAttribute("blueBack").getIntValue();
            int green = element.getAttribute("greenBack").getIntValue();
            util.setBackgroundColor(new Color(red, green, blue));
        } catch (org.jdom2.DataConversionException e) {
            log.warn("Could not parse background color attributes!");
        } catch (NullPointerException e) {
            // if the attributes are not listed, we consider the background as clear.
            util.setHasBackground(false);
        }
    }
    int fixedWidth = 0;
    int fixedHeight = 0;
    try {
        fixedHeight = element.getAttribute("fixedHeight").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.warn("Could not parse fixed Height attributes!");
    } catch (NullPointerException e) {
    // considered normal if the attributes are not present
    }
    try {
        fixedWidth = element.getAttribute("fixedWidth").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.warn("Could not parse fixed Width attribute!");
    } catch (NullPointerException e) {
    // considered normal if the attributes are not present
    }
    if (!(fixedWidth == 0 && fixedHeight == 0)) {
        util.setFixedSize(fixedWidth, fixedHeight);
    }
    if ((util.getFixedWidth() == 0) || (util.getFixedHeight() == 0)) {
        try {
            util.setMargin(element.getAttribute("margin").getIntValue());
        } catch (org.jdom2.DataConversionException e) {
            log.warn("Could not parse margin attribute!");
        } catch (NullPointerException e) {
        // considered normal if the attributes are not present
        }
    }
    try {
        util.setBorderSize(element.getAttribute("borderSize").getIntValue());
        int red = element.getAttribute("redBorder").getIntValue();
        int blue = element.getAttribute("blueBorder").getIntValue();
        int green = element.getAttribute("greenBorder").getIntValue();
        util.setBorderColor(new Color(red, green, blue));
    } catch (org.jdom2.DataConversionException e) {
        log.warn("Could not parse border attributes!");
    } catch (NullPointerException e) {
    // considered normal if the attribute not present
    }
    a = element.getAttribute("justification");
    if (a != null) {
        util.setJustification(a.getValue());
    } else {
        util.setJustification("left");
    }
    a = element.getAttribute("orientation");
    if (a != null) {
        util.setOrientation(a.getValue());
    } else {
        util.setOrientation("horizontal");
    }
    int deg = 0;
    try {
        a = element.getAttribute("degrees");
        if (a != null) {
            deg = a.getIntValue();
            l.rotate(deg);
        }
    } catch (DataConversionException ex) {
        log.warn("invalid 'degrees' value (non integer)");
    }
    if (deg == 0 && util.hasBackground()) {
        l.setOpaque(true);
    }
}
Also used : PositionablePopupUtil(jmri.jmrit.display.PositionablePopupUtil) Attribute(org.jdom2.Attribute) Color(java.awt.Color) DataConversionException(org.jdom2.DataConversionException) DataConversionException(org.jdom2.DataConversionException)

Example 55 with DataConversionException

use of org.jdom2.DataConversionException in project JMRI by JMRI.

the class PositionableLabelXml method load.

/**
     * Create a PositionableLabel, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    // create the objects
    PositionableLabel l = null;
    // get object class and determine editor being used
    Editor editor = (Editor) o;
    if (element.getAttribute("icon") != null) {
        NamedIcon icon;
        String name = element.getAttribute("icon").getValue();
        //            if (log.isDebugEnabled()) log.debug("icon attribute= "+name);
        if (name.equals("yes")) {
            icon = getNamedIcon("icon", element, "PositionableLabel ", editor);
        } else {
            icon = NamedIcon.getIconByName(name);
            if (icon == null) {
                icon = editor.loadFailed("PositionableLabel", name);
                if (icon == null) {
                    log.info("PositionableLabel icon removed for url= " + name);
                    return;
                }
            }
        }
        // abort if name != yes and have null icon
        if (icon == null && !name.equals("yes")) {
            log.info("PositionableLabel icon removed for url= " + name);
            return;
        }
        l = new PositionableLabel(icon, editor);
        try {
            Attribute a = element.getAttribute("rotate");
            if (a != null && icon != null) {
                int rotation = element.getAttribute("rotate").getIntValue();
                icon.setRotation(rotation, l);
            }
        } catch (org.jdom2.DataConversionException e) {
        }
        if (name.equals("yes")) {
            NamedIcon nIcon = loadIcon(l, "icon", element, "PositionableLabel ", editor);
            if (nIcon != null) {
                l.updateIcon(nIcon);
            } else {
                log.info("PositionableLabel icon removed for url= " + name);
                return;
            }
        } else {
            l.updateIcon(icon);
        }
    }
    if (element.getAttribute("text") != null) {
        if (l == null) {
            l = new PositionableLabel(element.getAttribute("text").getValue(), editor);
        }
        loadTextInfo(l, element);
    } else if (l == null) {
        log.error("PositionableLabel is null!");
        if (log.isDebugEnabled()) {
            java.util.List<Attribute> attrs = element.getAttributes();
            log.debug("\tElement Has " + attrs.size() + " Attributes:");
            for (int i = 0; i < attrs.size(); i++) {
                Attribute a = attrs.get(i);
                log.debug("\t\t" + a.getName() + " = " + a.getValue());
            }
            java.util.List<Element> kids = element.getChildren();
            log.debug("\tElementHas " + kids.size() + " children:");
            for (int i = 0; i < kids.size(); i++) {
                Element e = kids.get(i);
                log.debug("\t\t" + e.getName() + " = \"" + e.getValue() + "\"");
            }
        }
        editor.loadFailed();
        return;
    }
    editor.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.LABELS, element);
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) PositionableLabel(jmri.jmrit.display.PositionableLabel) DataConversionException(org.jdom2.DataConversionException) Editor(jmri.jmrit.display.Editor)

Aggregations

Element (org.jdom2.Element)49 Attribute (org.jdom2.Attribute)48 DataConversionException (org.jdom2.DataConversionException)18 Editor (jmri.jmrit.display.Editor)13 NamedIcon (jmri.jmrit.catalog.NamedIcon)11 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)9 Color (java.awt.Color)7 Point2D (java.awt.geom.Point2D)6 Point (java.awt.Point)5 ConfigureManager (jmri.ConfigureManager)4 AbstractXmlAdapter (jmri.configurexml.AbstractXmlAdapter)4 XmlAdapter (jmri.configurexml.XmlAdapter)4 DataElement (pcgen.core.doomsdaybook.DataElement)4 Block (jmri.Block)3 Memory (jmri.Memory)3 SignalHead (jmri.SignalHead)3 Turnout (jmri.Turnout)3 Portal (jmri.jmrit.logix.Portal)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 SortOrder (javax.swing.SortOrder)2