Search in sources :

Example 11 with Attribute

use of ucar.nc2.Attribute in project JMRI by JMRI.

the class MultiSensorIconXml method load.

/**
     * Create a PositionableLabel, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor an Object
     */
@Override
public void load(Element element, Object o) {
    Editor pe = (Editor) o;
    MultiSensorIcon l = new MultiSensorIcon(pe);
    // create the objects
    int rotation = 0;
    try {
        rotation = element.getAttribute("rotate").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
    } catch (NullPointerException e) {
    // considered normal if the attributes are not present
    }
    NamedIcon icon = loadSensorIcon("inactive", rotation, l, element, pe);
    if (icon != null) {
        l.setInactiveIcon(icon);
    } else {
        return;
    }
    icon = loadSensorIcon("unknown", rotation, l, element, pe);
    if (icon != null) {
        l.setUnknownIcon(icon);
    } else {
        return;
    }
    icon = loadSensorIcon("inconsistent", rotation, l, element, pe);
    if (icon != null) {
        l.setInconsistentIcon(icon);
    } else {
        return;
    }
    Attribute a = element.getAttribute("updown");
    if ((a != null) && a.getValue().equals("true")) {
        l.setUpDown(true);
    } else {
        l.setUpDown(false);
    }
    // get the icon pairs & load
    List<Element> items = element.getChildren();
    for (int i = 0; i < items.size(); i++) {
        // get the class, hence the adapter object to do loading
        Element item = items.get(i);
        if (item.getAttribute("sensor") != null) {
            String sensor = item.getAttribute("sensor").getValue();
            if (item.getAttribute("url") != null) {
                String name = item.getAttribute("url").getValue();
                icon = NamedIcon.getIconByName(name);
                if (icon == null) {
                    icon = pe.loadFailed("MultiSensor \"" + l.getNameString() + "\" ", name);
                    if (icon == null) {
                        log.error("MultiSensor \"" + l.getNameString() + "\" removed for url= " + name);
                        return;
                    }
                }
                try {
                    int deg = 0;
                    a = item.getAttribute("degrees");
                    if (a != null) {
                        deg = a.getIntValue();
                        double scale = 1.0;
                        a = item.getAttribute("scale");
                        if (a != null) {
                            scale = item.getAttribute("scale").getDoubleValue();
                        }
                        icon.setLoad(deg, scale, l);
                    }
                    if (deg == 0) {
                        a = item.getAttribute("rotate");
                        if (a != null) {
                            rotation = a.getIntValue();
                            icon.setRotation(rotation, l);
                        }
                    }
                } catch (org.jdom2.DataConversionException dce) {
                }
            } else {
                String name = item.getAttribute("icon").getValue();
                icon = NamedIcon.getIconByName(name);
                if (icon == null) {
                    icon = pe.loadFailed("MultiSensor \"" + l.getNameString(), name);
                    if (icon == null) {
                        log.info("MultiSensor \"" + l.getNameString() + " removed for url= " + name);
                        return;
                    }
                }
                if (rotation != 0) {
                    icon.setRotation(rotation, l);
                }
            }
            l.addEntry(sensor, icon);
        }
    }
    pe.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) MultiSensorIcon(jmri.jmrit.display.MultiSensorIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Editor(jmri.jmrit.display.Editor)

Example 12 with Attribute

use of ucar.nc2.Attribute in project JMRI by JMRI.

the class PositionableLabelXml method loadIcon.

public NamedIcon loadIcon(PositionableLabel l, String attrName, Element element, String name, Editor ed) {
    NamedIcon icon = getNamedIcon(attrName, element, name, ed);
    if (icon != null) {
        try {
            int deg = 0;
            double scale = 1.0;
            Element elem = element.getChild(attrName);
            if (elem != null) {
                Attribute a = elem.getAttribute("degrees");
                if (a != null) {
                    deg = a.getIntValue();
                }
                a = elem.getAttribute("scale");
                if (a != null) {
                    scale = elem.getAttribute("scale").getDoubleValue();
                }
                icon.setLoad(deg, scale, l);
                if (deg == 0) {
                    // "rotate" attribute is JMRI 2.9.3 and before
                    a = elem.getAttribute("rotate");
                    if (a != null) {
                        int rotation = a.getIntValue();
                        // 2.9.3 and before, only unscaled icons rotate
                        if (scale == 1.0) {
                            icon.setRotation(rotation, l);
                        }
                    }
                    // "rotation" element is JMRI 2.9.4 and after
                    Element e = elem.getChild("rotation");
                    if (e != null) {
                        // ver 2.9.4 allows orthogonal rotations of scaled icons
                        int rotation = Integer.parseInt(e.getText());
                        icon.setRotation(rotation, l);
                    }
                }
            }
        } catch (org.jdom2.DataConversionException dce) {
        }
    }
    return icon;
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) DataConversionException(org.jdom2.DataConversionException)

Example 13 with Attribute

use of ucar.nc2.Attribute in project JMRI by JMRI.

the class SensorIconXml 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;
    SensorIcon l;
    String name;
    Attribute attr = element.getAttribute("sensor");
    if (attr == null) {
        log.error("incorrect information for sensor; must use sensor name");
        ed.loadFailed();
        return;
    } else {
        name = attr.getValue();
    }
    _icon = true;
    if (element.getAttribute("icon") != null) {
        String yesno = element.getAttribute("icon").getValue();
        if ((yesno != null) && (!yesno.equals(""))) {
            if (yesno.equals("yes")) {
                _icon = true;
            } else if (yesno.equals("no")) {
                _icon = false;
            }
        }
    }
    if (_icon) {
        l = new SensorIcon(new NamedIcon("resources/icons/smallschematics/tracksegments/circuit-error.gif", "resources/icons/smallschematics/tracksegments/circuit-error.gif"), ed);
    } else {
        l = new SensorIcon("  ", ed);
    }
    int rotation = 0;
    try {
        rotation = element.getAttribute("rotate").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
    } catch (NullPointerException e) {
    // considered normal if the attributes are not present
    }
    if (loadSensorIcon("active", rotation, l, element, name, ed) == null) {
        return;
    }
    if (loadSensorIcon("inactive", rotation, l, element, name, ed) == null) {
        return;
    }
    if (loadSensorIcon("unknown", rotation, l, element, name, ed) == null) {
        return;
    }
    if (loadSensorIcon("inconsistent", rotation, l, element, name, ed) == null) {
        return;
    }
    Element elem = element.getChild("iconmaps");
    if (elem != null) {
        attr = elem.getAttribute("family");
        if (attr != null) {
            l.setFamily(attr.getValue());
        }
    }
    Attribute a = element.getAttribute("momentary");
    if ((a != null) && a.getValue().equals("true")) {
        l.setMomentary(true);
    } else {
        l.setMomentary(false);
    }
    loadTextInfo(l, element);
    l.setSensor(name);
    ed.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.SENSORS, element);
    if (l.isIcon() && l.getText() != null) {
        l.setOpaque(false);
    }
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) SensorIcon(jmri.jmrit.display.SensorIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Editor(jmri.jmrit.display.Editor)

Example 14 with Attribute

use of ucar.nc2.Attribute in project JMRI by JMRI.

the class SwitchboardEditorXml method load.

/**
     * Create a SwitchboardEditor object, then register and fill it, then pop it in a
     * JFrame
     *
     * @param shared Top level Element to unpack.
     * @return true if successful
     */
@Override
public boolean load(Element shared, Element perNode) {
    boolean result = true;
    // find coordinates
    int x = 0;
    int y = 0;
    int height = 400;
    int width = 300;
    int rangemin = 1;
    int rangemax = 32;
    int columns = 4;
    String type = "T";
    String connection = "I";
    String shape = "key";
    String name;
    try {
        x = shared.getAttribute("x").getIntValue();
        y = shared.getAttribute("y").getIntValue();
        height = shared.getAttribute("height").getIntValue();
        width = shared.getAttribute("width").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert Switchboard's attribute");
        result = false;
    }
    // find the name
    // this will be replaced by the name as stored NOI18N
    name = "Switchboard";
    if (shared.getAttribute("name") != null) {
        name = shared.getAttribute("name").getValue();
    }
    // confirm that panel hasn't already been loaded
    if (jmri.jmrit.display.PanelMenu.instance().isPanelNameUsed(name)) {
        log.warn("File contains a panel with the same name (" + name + ") as an existing panel");
        result = false;
    }
    SwitchboardEditor panel = new SwitchboardEditor(name);
    //panel.makeFrame(name);
    jmri.jmrit.display.PanelMenu.instance().addEditorPanel(panel);
    panel.getTargetFrame().setLocation(x, y);
    panel.getTargetFrame().setSize(width, height);
    panel.setTitle();
    // Load editor option flags. This has to be done before the content
    // items are loaded, to preserve the individual item settings.
    Attribute a;
    boolean value = true;
    if ((a = shared.getAttribute("editable")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setAllEditable(value);
    value = true;
    if ((a = shared.getAttribute("showtooltips")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setAllShowTooltip(value);
    value = true;
    if ((a = shared.getAttribute("controlling")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setAllControlling(value);
    value = false;
    if ((a = shared.getAttribute("hide")) != null && a.getValue().equals("yes")) {
        value = true;
    }
    panel.setShowHidden(value);
    value = true;
    if ((a = shared.getAttribute("panelmenu")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setPanelMenuVisible(value);
    String state = "both";
    if ((a = shared.getAttribute("scrollable")) != null) {
        state = a.getValue();
    }
    panel.setScroll(state);
    value = false;
    if ((a = shared.getAttribute("hideunconnected")) != null && a.getValue().equals("yes")) {
        value = true;
    }
    panel.setHideUnconnected(value);
    try {
        rangemin = shared.getAttribute("rangemin").getIntValue();
        rangemax = shared.getAttribute("rangemax").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert Switchboard's range");
        result = false;
    }
    panel.setPanelMenuRangeMin(rangemin);
    panel.setPanelMenuRangeMax(rangemax);
    type = shared.getAttribute("type").getValue();
    panel.setSwitchType(type);
    connection = shared.getAttribute("connection").getValue();
    panel.setSwitchManu(connection);
    shape = shared.getAttribute("shape").getValue();
    panel.setSwitchShape(shape);
    try {
        columns = shared.getAttribute("columns").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert Switchboard's column count");
        result = false;
    }
    panel.setColumns(columns);
    String defaultTextColor = "black";
    if (shared.getAttribute("defaulttextcolor") != null) {
        defaultTextColor = shared.getAttribute("defaulttextcolor").getValue();
    }
    panel.setDefaultTextColor(defaultTextColor);
    // set color if needed
    try {
        int red = shared.getAttribute("redBackground").getIntValue();
        int blue = shared.getAttribute("blueBackground").getIntValue();
        int green = shared.getAttribute("greenBackground").getIntValue();
        //panel.setBackground(new Color(red, green, blue));
        panel.setDefaultBackgroundColor(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
    }
    //set the (global) editor display widgets to their flag settings
    panel.initView();
    // load the contents with their individual option settings
    List<Element> items = shared.getChildren();
    for (int i = 0; i < items.size(); i++) {
        // get the class, hence the adapter object to do loading
        Element item = items.get(i);
        String adapterName = item.getAttribute("class").getValue();
        log.debug("load via " + adapterName);
        try {
            XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
            // and do it
            adapter.load(item, panel);
            if (!panel.loadOK()) {
                result = false;
            }
        } catch (Exception e) {
            log.error("Exception while loading " + item.getName() + ":" + e);
            result = false;
            e.printStackTrace();
        }
    }
    // dispose of url correction data
    panel.disposeLoadData();
    // display the results, with the editor in back
    panel.pack();
    panel.setAllEditable(panel.isEditable());
    // we don't pack the target frame here, because size was specified
    // TODO: Work out why, when calling this method, panel size is increased
    // vertically (at least on MS Windows)
    // always show the panel
    panel.getTargetFrame().setVisible(true);
    // register the resulting panel for later configuration
    ConfigureManager cm = InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
    if (cm != null) {
        cm.registerUser(panel);
    }
    // reset the size and position, in case the display caused it to change
    panel.getTargetFrame().setLocation(x, y);
    panel.getTargetFrame().setSize(width, height);
    panel.updatePressed();
    return result;
}
Also used : Attribute(org.jdom2.Attribute) Color(java.awt.Color) Element(org.jdom2.Element) Point(java.awt.Point) SwitchboardEditor(jmri.jmrit.display.switchboardEditor.SwitchboardEditor) ConfigureManager(jmri.ConfigureManager) AbstractXmlAdapter(jmri.configurexml.AbstractXmlAdapter) XmlAdapter(jmri.configurexml.XmlAdapter)

Example 15 with Attribute

use of ucar.nc2.Attribute in project JMRI by JMRI.

the class WarrantPreferences method loadSpeedMap.

// Avoid firePropertyChange until SignalSpeedMap is completely loaded
private boolean loadSpeedMap(Element child) {
    if (child == null) {
        return false;
    }
    Element rampParms = child.getChild(STEP_INCREMENTS);
    if (rampParms == null) {
        return false;
    }
    Attribute a;
    if ((a = rampParms.getAttribute(TIME_INCREMENT)) != null) {
        try {
            this._msIncrTime = a.getIntValue();
        } catch (DataConversionException ex) {
            this._msIncrTime = 500;
            log.error("Unable to read ramp time increment. Setting to default value (500ms).", ex);
        }
    }
    if ((a = rampParms.getAttribute(RAMP_INCREMENT)) != null) {
        try {
            this._throttleIncr = a.getFloatValue();
        } catch (DataConversionException ex) {
            this._throttleIncr = 0.03f;
            log.error("Unable to read ramp throttle increment. Setting to default value (0.03).", ex);
        }
    }
    if ((a = rampParms.getAttribute(THROTTLE_SCALE)) != null) {
        try {
            _throttleScale = a.getFloatValue();
        } catch (DataConversionException ex) {
            _throttleScale = .90f;
            log.error("Unable to read throttle scale. Setting to default value (0.90f).", ex);
        }
    }
    rampParms = child.getChild(SPEED_NAME_PREFS);
    if (rampParms == null) {
        return false;
    }
    if ((a = rampParms.getAttribute("percentNormal")) != null) {
        if (a.getValue().equals("yes")) {
            _interpretation = 1;
        } else {
            _interpretation = 2;
        }
    }
    if ((a = rampParms.getAttribute(INTERPRETATION)) != null) {
        try {
            _interpretation = a.getIntValue();
        } catch (DataConversionException ex) {
            _interpretation = 1;
            log.error("Unable to read interpetation of Speed Map. Setting to default value % normal.", ex);
        }
    }
    HashMap<String, Float> map = new LinkedHashMap<>();
    List<Element> list = rampParms.getChildren();
    for (int i = 0; i < list.size(); i++) {
        String name = list.get(i).getName();
        Float speed = 0f;
        try {
            speed = Float.valueOf(list.get(i).getText());
        } catch (NumberFormatException nfe) {
            log.error("Speed names has invalid content for {} = ", name, list.get(i).getText());
        }
        log.debug("Add {}, {} to AspectSpeed Table", name, speed);
        map.put(name, speed);
    }
    // no firePropertyChange
    this.setSpeedNames(map);
    rampParms = child.getChild(APPEARANCE_PREFS);
    if (rampParms == null) {
        return false;
    }
    LinkedHashMap<String, String> heads = new LinkedHashMap<>();
    list = rampParms.getChildren();
    for (int i = 0; i < list.size(); i++) {
        String name = Bundle.getMessage(list.get(i).getName());
        String speed = list.get(i).getText();
        heads.put(name, speed);
    }
    // no firePropertyChange
    this.setAppearances(heads);
    // Now set SignalSpeedMap members.
    SignalSpeedMap speedMap = jmri.InstanceManager.getDefault(SignalSpeedMap.class);
    speedMap.setRampParams(_msIncrTime, _msIncrTime);
    speedMap.setDefaultThrottleFactor(_throttleScale);
    speedMap.setLayoutScale(_scale);
    speedMap.setAspects(new HashMap<>(this._speedNames), _interpretation);
    speedMap.setAppearances(new HashMap<>(this._headAppearances));
    return true;
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) LinkedHashMap(java.util.LinkedHashMap) SignalSpeedMap(jmri.implementation.SignalSpeedMap) DataConversionException(org.jdom2.DataConversionException)

Aggregations

Attribute (org.jdom2.Attribute)106 Element (org.jdom2.Element)75 Editor (jmri.jmrit.display.Editor)15 DataConversionException (org.jdom2.DataConversionException)14 NamedIcon (jmri.jmrit.catalog.NamedIcon)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)12 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)9 List (java.util.List)7 Color (java.awt.Color)6 File (java.io.File)6 HashMap (java.util.HashMap)6 ConfigureManager (jmri.ConfigureManager)6 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)5 Asn1Object (com.android.hotspot2.asn1.Asn1Object)5 Asn1Oid (com.android.hotspot2.asn1.Asn1Oid)5 OidMappings (com.android.hotspot2.asn1.OidMappings)5 ASN1Encodable (com.android.org.bouncycastle.asn1.ASN1Encodable)5 ASN1EncodableVector (com.android.org.bouncycastle.asn1.ASN1EncodableVector)5 ASN1Set (com.android.org.bouncycastle.asn1.ASN1Set)5