Search in sources :

Example 66 with Element

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

the class ConsistFile method readFile.

/**
     * Read all consists from a file.
     *
     * @param fileName path to file
     * @throws org.jdom2.JDOMException if unable to parse consists
     * @throws java.io.IOException     if unable to read file
     */
@SuppressWarnings("unchecked")
public void readFile(String fileName) throws JDOMException, IOException {
    if (checkFile(fileName)) {
        Element root = rootFromName(fileName);
        Element roster;
        if (root == null) {
            log.warn("consist file could not be read");
            return;
        }
        roster = root.getChild("roster");
        if (roster == null) {
            log.debug("consist file does not contain a roster entry");
            return;
        }
        Iterator<Element> consistIterator = root.getDescendants(new ElementFilter("consist"));
        try {
            Element consist;
            do {
                consist = consistIterator.next();
                consistFromXml(consist);
            } while (consistIterator.hasNext());
        } catch (NoSuchElementException nde) {
            log.debug("end of consist list");
        }
    } else {
        log.info("Consist file does not exist.  One will be created if necessary.");
    }
}
Also used : Element(org.jdom2.Element) ElementFilter(org.jdom2.filter.ElementFilter) NoSuchElementException(java.util.NoSuchElementException)

Example 67 with Element

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

the class OptionsFile method readDispatcherOptions.

/*
     *  Reads Dispatcher Options from a file in the user's preferences directory
     *  If the file containing Dispatcher Options does not exist this routine returns quietly.
     */
public void readDispatcherOptions(DispatcherFrame f) throws org.jdom2.JDOMException, java.io.IOException {
    // check if file exists
    if (checkFile(defaultFileName)) {
        // file is present, 
        log.debug("Reading Dispatcher options from file {}", defaultFileName);
        root = rootFromName(defaultFileName);
        dispatcher = f;
        if (root != null) {
            // there is a file
            Element options = root.getChild("options");
            if (options != null) {
                // there are options defined, read and set Dispatcher options
                if (options.getAttribute("lename") != null) {
                    // there is a layout editor name selected
                    String leName = options.getAttribute("lename").getValue();
                    // get list of Layout Editor panels
                    ArrayList<LayoutEditor> layoutEditorList = jmri.jmrit.display.PanelMenu.instance().getLayoutEditorPanelList();
                    if (layoutEditorList.size() == 0) {
                        log.warn("Dispatcher options specify a Layout Editor panel that is not present.");
                    } else {
                        boolean found = false;
                        for (int i = 0; i < layoutEditorList.size(); i++) {
                            if (leName.equals(layoutEditorList.get(i).getTitle())) {
                                found = true;
                                dispatcher.setLayoutEditor(layoutEditorList.get(i));
                            }
                        }
                        if (!found) {
                            log.warn("Layout Editor panel - " + leName + " - not found.");
                        }
                    }
                }
                if (options.getAttribute("usesignaltype") != null) {
                    dispatcher.setSignalType(0x00);
                    if (options.getAttribute("usesignaltype").getValue().equals("signalmast")) {
                        dispatcher.setSignalType(0x01);
                    }
                }
                if (options.getAttribute("useconnectivity") != null) {
                    dispatcher.setUseConnectivity(true);
                    if (options.getAttribute("useconnectivity").getValue().equals("no")) {
                        dispatcher.setUseConnectivity(false);
                    }
                }
                if (options.getAttribute("trainsfromroster") != null) {
                    dispatcher.setTrainsFromRoster(true);
                    if (options.getAttribute("trainsfromroster").getValue().equals("no")) {
                        dispatcher.setTrainsFromRoster(false);
                    }
                }
                if (options.getAttribute("trainsfromtrains") != null) {
                    dispatcher.setTrainsFromTrains(true);
                    if (options.getAttribute("trainsfromtrains").getValue().equals("no")) {
                        dispatcher.setTrainsFromTrains(false);
                    }
                }
                if (options.getAttribute("trainsfromuser") != null) {
                    dispatcher.setTrainsFromUser(true);
                    if (options.getAttribute("trainsfromuser").getValue().equals("no")) {
                        dispatcher.setTrainsFromUser(false);
                    }
                }
                if (options.getAttribute("autoallocate") != null) {
                    dispatcher.setAutoAllocate(false);
                    if (options.getAttribute("autoallocate").getValue().equals("yes")) {
                        dispatcher.setAutoAllocate(true);
                    }
                }
                if (options.getAttribute("autoturnouts") != null) {
                    dispatcher.setAutoTurnouts(true);
                    if (options.getAttribute("autoturnouts").getValue().equals("no")) {
                        dispatcher.setAutoTurnouts(false);
                    }
                }
                if (options.getAttribute("trustknownturnouts") != null) {
                    dispatcher.setTrustKnownTurnouts(false);
                    if (options.getAttribute("trustknownturnouts").getValue().equals("yes")) {
                        dispatcher.setTrustKnownTurnouts(true);
                    }
                }
                if (options.getAttribute("minthrottleinterval") != null) {
                    String s = (options.getAttribute("minthrottleinterval")).getValue();
                    dispatcher.setMinThrottleInterval(Integer.parseInt(s));
                }
                if (options.getAttribute("fullramptime") != null) {
                    String s = (options.getAttribute("fullramptime")).getValue();
                    dispatcher.setFullRampTime(Integer.parseInt(s));
                }
                if (options.getAttribute("hasoccupancydetection") != null) {
                    dispatcher.setHasOccupancyDetection(true);
                    if (options.getAttribute("hasoccupancydetection").getValue().equals("no")) {
                        dispatcher.setHasOccupancyDetection(false);
                    }
                }
                if (options.getAttribute("shortactivetrainnames") != null) {
                    dispatcher.setShortActiveTrainNames(true);
                    if (options.getAttribute("shortactivetrainnames").getValue().equals("no")) {
                        dispatcher.setShortActiveTrainNames(false);
                    }
                }
                if (options.getAttribute("shortnameinblock") != null) {
                    dispatcher.setShortNameInBlock(true);
                    if (options.getAttribute("shortnameinblock").getValue().equals("no")) {
                        dispatcher.setShortNameInBlock(false);
                    }
                }
                if (options.getAttribute("extracolorforallocated") != null) {
                    dispatcher.setExtraColorForAllocated(true);
                    if (options.getAttribute("extracolorforallocated").getValue().equals("no")) {
                        dispatcher.setExtraColorForAllocated(false);
                    }
                }
                if (options.getAttribute("nameinallocatedblock") != null) {
                    dispatcher.setNameInAllocatedBlock(true);
                    if (options.getAttribute("nameinallocatedblock").getValue().equals("no")) {
                        dispatcher.setNameInAllocatedBlock(false);
                    }
                }
                if (options.getAttribute("supportvsdecoder") != null) {
                    dispatcher.setSupportVSDecoder(true);
                    if (options.getAttribute("supportvsdecoder").getValue().equals("no")) {
                        dispatcher.setSupportVSDecoder(false);
                    }
                }
                if (options.getAttribute("layoutscale") != null) {
                    String s = (options.getAttribute("layoutscale")).getValue();
                    for (int i = 1; i <= Scale.NUM_SCALES; i++) {
                        if (Scale.getShortScaleID(i).equals(s)) {
                            dispatcher.setScale(i);
                        }
                    }
                }
                if (options.getAttribute("usescalemeters") != null) {
                    dispatcher.setUseScaleMeters(true);
                    if (options.getAttribute("usescalemeters").getValue().equals("no")) {
                        dispatcher.setUseScaleMeters(false);
                    }
                }
                if (options.getAttribute("userosterentryinblock") != null) {
                    dispatcher.setRosterEntryInBlock(false);
                    if (options.getAttribute("userosterentryinblock").getValue().equals("yes")) {
                        dispatcher.setRosterEntryInBlock(true);
                    }
                }
            }
        }
    } else {
        log.debug("No Dispatcher options file found at {}, using defaults", defaultFileName);
    }
}
Also used : LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) Element(org.jdom2.Element)

Example 68 with Element

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

the class PositionableLabelXml method loadCommonAttributes.

public void loadCommonAttributes(Positionable l, int defaultLevel, Element element) {
    Attribute a = element.getAttribute("forcecontroloff");
    if ((a != null) && a.getValue().equals("true")) {
        l.setControlling(false);
    } else {
        l.setControlling(true);
    }
    // find coordinates
    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.setLocation(x, y);
    // find display level
    int level = defaultLevel;
    try {
        level = element.getAttribute("level").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.warn("Could not parse level attribute!");
    } catch (NullPointerException e) {
    // considered normal if the attribute not present
    }
    l.setDisplayLevel(level);
    a = element.getAttribute("hidden");
    if ((a != null) && a.getValue().equals("yes")) {
        l.setHidden(true);
        l.setVisible(false);
    }
    a = element.getAttribute("positionable");
    if ((a != null) && a.getValue().equals("true")) {
        l.setPositionable(true);
    } else {
        l.setPositionable(false);
    }
    a = element.getAttribute("showtooltip");
    if ((a != null) && a.getValue().equals("true")) {
        l.setShowTooltip(true);
    } else {
        l.setShowTooltip(false);
    }
    a = element.getAttribute("editable");
    if ((a != null) && a.getValue().equals("true")) {
        l.setEditable(true);
    } else {
        l.setEditable(false);
    }
    a = element.getAttribute("degrees");
    if (a != null && l instanceof PositionableLabel) {
        try {
            int deg = a.getIntValue();
            ((PositionableLabel) l).setDegrees(deg);
        } catch (org.jdom2.DataConversionException dce) {
        }
    }
    Element elem = element.getChild("tooltip");
    if (elem == null) {
        // pre JMRI 3.5.2
        elem = element.getChild("toolTip");
    }
    if (elem != null) {
        ToolTip tip = l.getTooltip();
        if (tip != null) {
            tip.setText(elem.getText());
        }
    }
}
Also used : ToolTip(jmri.jmrit.display.ToolTip) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) PositionableLabel(jmri.jmrit.display.PositionableLabel) DataConversionException(org.jdom2.DataConversionException)

Example 69 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element 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 70 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element 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)

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