Search in sources :

Example 1 with SignalSpeedMap

use of jmri.implementation.SignalSpeedMap 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)

Example 2 with SignalSpeedMap

use of jmri.implementation.SignalSpeedMap in project JMRI by JMRI.

the class WarrantPreferences method loadSpeedMapFromOldXml.

// Avoid firePropertyChange until SignalSpeedMap is completely loaded
private void loadSpeedMapFromOldXml() {
    SignalSpeedMap map = jmri.InstanceManager.getNullableDefault(SignalSpeedMap.class);
    if (map == null) {
        log.error("Cannot find signalSpeeds.xml file.");
        return;
    }
    Iterator<String> it = map.getValidSpeedNames().iterator();
    LinkedHashMap<String, Float> names = new LinkedHashMap<>();
    while (it.hasNext()) {
        String name = it.next();
        names.put(name, map.getSpeed(name));
    }
    // OK, no firePropertyChange
    this.setSpeedNames(names);
    Enumeration<String> en = map.getAppearanceIterator();
    LinkedHashMap<String, String> heads = new LinkedHashMap<>();
    while (en.hasMoreElements()) {
        String name = en.nextElement();
        heads.put(name, map.getAppearanceSpeed(name));
    }
    // no firePropertyChange
    this.setAppearances(heads);
    this._msIncrTime = map.getStepDelay();
    this._throttleIncr = map.getStepIncrement();
}
Also used : SignalSpeedMap(jmri.implementation.SignalSpeedMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)2 SignalSpeedMap (jmri.implementation.SignalSpeedMap)2 Attribute (org.jdom2.Attribute)1 DataConversionException (org.jdom2.DataConversionException)1 Element (org.jdom2.Element)1