Search in sources :

Example 21 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class JmriUserPreferencesManager method readPreferencesState.

private void readPreferencesState() {
    Element element = this.readElement(CLASSPREFS_ELEMENT, CLASSPREFS_NAMESPACE);
    if (element != null) {
        element.getChildren("preferences").stream().forEach((preferences) -> {
            String clazz = preferences.getAttributeValue("class");
            log.debug("Reading class preferences for \"{}\"", clazz);
            preferences.getChildren("multipleChoice").stream().forEach((mc) -> {
                mc.getChildren("option").stream().forEach((option) -> {
                    int value = 0;
                    try {
                        option.getAttribute("value").getIntValue();
                    } catch (DataConversionException ex) {
                        log.error("failed to convert positional attribute");
                    }
                    this.setMultipleChoiceOption(clazz, option.getAttributeValue("item"), value);
                });
            });
            preferences.getChildren("reminderPrompts").stream().forEach((rp) -> {
                rp.getChildren("reminder").stream().forEach((reminder) -> {
                    log.debug("Setting preferences state \"true\" for \"{}\", \"{}\"", clazz, reminder.getText());
                    this.setPreferenceState(clazz, reminder.getText(), true);
                });
            });
        });
    }
}
Also used : Element(org.jdom2.Element) DataConversionException(org.jdom2.DataConversionException) Point(java.awt.Point)

Example 22 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class JmriUserPreferencesManager method saveWindowDetails.

private void saveWindowDetails() {
    this.setChangeMade(false);
    if (this.allowSave) {
        if (!windowDetails.isEmpty()) {
            Element element = new Element(WINDOWS_ELEMENT, WINDOWS_NAMESPACE);
            for (Entry<String, WindowLocations> entry : windowDetails.entrySet()) {
                Element window = new Element("window");
                window.setAttribute("class", entry.getKey());
                if (entry.getValue().saveLocation) {
                    try {
                        window.setAttribute("locX", Double.toString(entry.getValue().getLocation().getX()));
                        window.setAttribute("locY", Double.toString(entry.getValue().getLocation().getY()));
                    } catch (NullPointerException ex) {
                    // Expected if the location has not been set or the window is open
                    }
                }
                if (entry.getValue().saveSize) {
                    try {
                        double height = entry.getValue().getSize().getHeight();
                        double width = entry.getValue().getSize().getWidth();
                        // Do not save the width or height if set to zero
                        if (!(height == 0.0 && width == 0.0)) {
                            window.setAttribute("width", Double.toString(width));
                            window.setAttribute("height", Double.toString(height));
                        }
                    } catch (NullPointerException ex) {
                    // Expected if the size has not been set or the window is open
                    }
                }
                if (!entry.getValue().parameters.isEmpty()) {
                    Element properties = new Element("properties");
                    entry.getValue().parameters.entrySet().stream().map((property) -> {
                        Element propertyElement = new Element("property");
                        propertyElement.addContent(new Element("key").setText(property.getKey()));
                        Object value = property.getValue();
                        if (value != null) {
                            propertyElement.addContent(new Element("value").setAttribute("class", value.getClass().getName()).setText(value.toString()));
                        }
                        return propertyElement;
                    }).forEach((propertyElement) -> {
                        properties.addContent(propertyElement);
                    });
                    window.addContent(properties);
                }
                element.addContent(window);
            }
            this.saveElement(element);
            this.resetChangeMade();
        }
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) ProfileManager(jmri.profile.ProfileManager) Point(java.awt.Point) HashMap(java.util.HashMap) JmriJFrame(jmri.util.JmriJFrame) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) JDOMUtil(jmri.util.jdom.JDOMUtil) ProfileUtils(jmri.profile.ProfileUtils) Map(java.util.Map) Bean(jmri.beans.Bean) Profile(jmri.profile.Profile) Method(java.lang.reflect.Method) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BoxLayout(javax.swing.BoxLayout) InstanceManager(jmri.InstanceManager) UserPreferencesManager(jmri.UserPreferencesManager) Logger(org.slf4j.Logger) JmriException(jmri.JmriException) TableColumnPreferences(jmri.swing.JmriJTablePersistenceManager.TableColumnPreferences) Set(java.util.Set) JOptionPane(javax.swing.JOptionPane) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SortOrder(javax.swing.SortOrder) Dimension(java.awt.Dimension) List(java.util.List) FileUtil(jmri.util.FileUtil) JLabel(javax.swing.JLabel) ConfigureManager(jmri.ConfigureManager) JmriJTablePersistenceManager(jmri.swing.JmriJTablePersistenceManager) Entry(java.util.Map.Entry) JCheckBox(javax.swing.JCheckBox) DataConversionException(org.jdom2.DataConversionException) NodeIdentity(jmri.util.node.NodeIdentity) JPanel(javax.swing.JPanel) Toolkit(java.awt.Toolkit) Element(org.jdom2.Element) Element(org.jdom2.Element)

Example 23 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class AbstractReporterManagerConfigXML method loadReporters.

/**
     * Utility method to load the individual Reporter objects. If there's no
     * additional info needed for a specific Reporter type, invoke this with the
     * parent of the set of Reporter elements.
     *
     * @param reporters Element containing the Reporter elements to load.
     * @return true if successful
     */
@SuppressWarnings("unchecked")
public boolean loadReporters(Element reporters) {
    boolean result = true;
    List<Element> reporterList = reporters.getChildren("reporter");
    if (log.isDebugEnabled()) {
        log.debug("Found " + reporterList.size() + " reporters");
    }
    ReporterManager tm = InstanceManager.getDefault(jmri.ReporterManager.class);
    for (int i = 0; i < reporterList.size(); i++) {
        String sysName = getSystemName(reporterList.get(i));
        if (sysName == null) {
            log.warn("unexpected null in systemName " + reporterList.get(i) + " " + reporterList.get(i).getAttributes());
            result = false;
            break;
        }
        String userName = getUserName(reporterList.get(i));
        if (log.isDebugEnabled()) {
            log.debug("create Reporter: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
        }
        Reporter r = tm.newReporter(sysName, userName);
        loadCommon(r, reporterList.get(i));
    }
    return result;
}
Also used : ReporterManager(jmri.ReporterManager) Element(org.jdom2.Element) Reporter(jmri.Reporter)

Example 24 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class AbstractSensorManagerConfigXML method loadSensors.

/**
     * Utility method to load the individual Sensor objects. If there's no
     * additional info needed for a specific sensor type, invoke this with the
     * parent of the set of Sensor elements.
     *
     * @param sensors Element containing the Sensor elements to load.
     * @return true if succeeded
     */
@SuppressWarnings("unchecked")
public boolean loadSensors(Element sensors) throws jmri.configurexml.JmriConfigureXmlException {
    boolean result = true;
    List<Element> sensorList = sensors.getChildren("sensor");
    if (log.isDebugEnabled()) {
        log.debug("Found " + sensorList.size() + " sensors");
    }
    SensorManager tm = InstanceManager.sensorManagerInstance();
    long goingActive = 0L;
    long goingInActive = 0L;
    if (sensors.getChild("globalDebounceTimers") != null) {
        Element timer = sensors.getChild("globalDebounceTimers");
        try {
            if (timer.getChild("goingActive") != null) {
                String active = timer.getChild("goingActive").getText();
                goingActive = Long.valueOf(active);
                tm.setDefaultSensorDebounceGoingActive(goingActive);
            }
        } catch (NumberFormatException ex) {
            log.error(ex.toString());
        }
        try {
            if (timer.getChild("goingInActive") != null) {
                String inActive = timer.getChild("goingInActive").getText();
                goingInActive = Long.valueOf(inActive);
                tm.setDefaultSensorDebounceGoingInActive(goingInActive);
            }
        } catch (NumberFormatException ex) {
            log.error(ex.toString());
        }
    }
    for (int i = 0; i < sensorList.size(); i++) {
        String sysName = getSystemName(sensorList.get(i));
        if (sysName == null) {
            handleException("Unexpected missing system name while loading sensors", null, null, null, null);
            result = false;
            break;
        }
        boolean inverted = false;
        String userName = getUserName(sensorList.get(i));
        checkNameNormalization(sysName, userName, tm);
        if (sensorList.get(i).getAttribute("inverted") != null) {
            if (sensorList.get(i).getAttribute("inverted").getValue().equals("true")) {
                inverted = true;
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("create sensor: (" + sysName + ")");
        }
        Sensor s;
        try {
            s = tm.newSensor(sysName, userName);
        } catch (IllegalArgumentException e) {
            handleException("Could not create sensor", null, sysName, userName, null);
            result = false;
            continue;
        }
        // load common parts
        loadCommon(s, sensorList.get(i));
        if (sensorList.get(i).getChild("debounceTimers") != null) {
            Element timer = sensorList.get(i).getChild("debounceTimers");
            try {
                if (timer.getChild("goingActive") != null) {
                    String active = timer.getChild("goingActive").getText();
                    s.setSensorDebounceGoingActiveTimer(Long.valueOf(active));
                }
            } catch (NumberFormatException ex) {
                log.error(ex.toString());
            }
            try {
                if (timer.getChild("goingInActive") != null) {
                    String inActive = timer.getChild("goingInActive").getText();
                    s.setSensorDebounceGoingInActiveTimer(Long.valueOf(inActive));
                }
            } catch (NumberFormatException ex) {
                log.error(ex.toString());
            }
        }
        if (sensorList.get(i).getChild("useGlobalDebounceTimer") != null) {
            if (sensorList.get(i).getChild("useGlobalDebounceTimer").getText().equals("yes")) {
                s.useDefaultTimerSettings(true);
            }
        }
        s.setInverted(inverted);
        if (sensorList.get(i).getChild("pullResistance") != null) {
            String pull = sensorList.get(i).getChild("pullResistance").getText();
            log.debug("setting pull to {} for sensor {}", pull, s);
            s.setPullResistance(jmri.Sensor.PullResistance.getByShortName(pull));
        }
    }
    return result;
}
Also used : SensorManager(jmri.SensorManager) Element(org.jdom2.Element) Sensor(jmri.Sensor)

Example 25 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class AbstractSensorManagerConfigXML method store.

public Element store(Object o, Element sensors) {
    setStoreElementClass(sensors);
    SensorManager tm = (SensorManager) o;
    if (tm.getDefaultSensorDebounceGoingActive() > 0 || tm.getDefaultSensorDebounceGoingInActive() > 0) {
        Element elem = new Element("globalDebounceTimers");
        elem.addContent(new Element("goingActive").addContent(String.valueOf(tm.getDefaultSensorDebounceGoingActive())));
        elem.addContent(new Element("goingInActive").addContent(String.valueOf(tm.getDefaultSensorDebounceGoingInActive())));
        sensors.addContent(elem);
    }
    java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
    // don't return an element if there are not sensors to include
    if (!iter.hasNext()) {
        return null;
    }
    // store the sensors
    while (iter.hasNext()) {
        String sname = iter.next();
        log.debug("system name is " + sname);
        Sensor s = tm.getBySystemName(sname);
        String inverted = s.getInverted() ? "true" : "false";
        Element elem = new Element("sensor").setAttribute("inverted", inverted);
        elem.addContent(new Element("systemName").addContent(sname));
        log.debug("store sensor " + sname);
        if (s.useDefaultTimerSettings()) {
            elem.addContent(new Element("useGlobalDebounceTimer").addContent("yes"));
        } else {
            if (s.getSensorDebounceGoingActiveTimer() > 0 || s.getSensorDebounceGoingInActiveTimer() > 0) {
                Element timer = new Element("debounceTimers");
                timer.addContent(new Element("goingActive").addContent(String.valueOf(s.getSensorDebounceGoingActiveTimer())));
                timer.addContent(new Element("goingInActive").addContent(String.valueOf(s.getSensorDebounceGoingInActiveTimer())));
                elem.addContent(timer);
            }
        }
        if (tm.isPullResistanceConfigurable()) {
            // store the sensor's value for pull resistance.
            elem.addContent(new Element("pullResistance").addContent(s.getPullResistance().getShortName()));
        }
        // store common part
        storeCommon(s, elem);
        sensors.addContent(elem);
    }
    return sensors;
}
Also used : SensorManager(jmri.SensorManager) Element(org.jdom2.Element) Sensor(jmri.Sensor)

Aggregations

Element (org.jdom2.Element)829 Attribute (org.jdom2.Attribute)76 Document (org.jdom2.Document)75 Test (org.junit.Test)70 File (java.io.File)53 ArrayList (java.util.ArrayList)45 JDOMException (org.jdom2.JDOMException)37 IOException (java.io.IOException)34 HashMap (java.util.HashMap)28 NamedIcon (jmri.jmrit.catalog.NamedIcon)27 List (java.util.List)26 XmlFile (jmri.jmrit.XmlFile)24 SAXBuilder (org.jdom2.input.SAXBuilder)21 Turnout (jmri.Turnout)20 DataConversionException (org.jdom2.DataConversionException)20 DocType (org.jdom2.DocType)19 Editor (jmri.jmrit.display.Editor)18 XMLOutputter (org.jdom2.output.XMLOutputter)18 Namespace (org.jdom2.Namespace)17 Point (java.awt.Point)15