Search in sources :

Example 86 with Element

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

the class WindowPreferences method getPreferences.

/**
     * Collect JInternalFrame preferences.
     *
     * @param c The JInternalFrame being XMLed.
     * @return An Element containing the following prefs:
     * <ul>
     * <li> x location
     * <li> y location
     * <li> width
     * <li> height
     * <li> isIcon
     * </ul>
     */
public static Element getPreferences(JInternalFrame c) {
    Element window = getPreferences((Container) c);
    window.setAttribute("isIconified", String.valueOf(c.isIcon()));
    return window;
}
Also used : Element(org.jdom2.Element)

Example 87 with Element

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

the class Diesel3Sound method getXml.

@Override
public Element getXml() {
    Element me = new Element("sound");
    me.setAttribute("name", this.getName());
    me.setAttribute("type", "engine");
    // Do something, eventually...
    return (me);
}
Also used : Element(org.jdom2.Element)

Example 88 with Element

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

the class VSDecoder method setXml.

/*
     * @Deprecated public void setXml(Element e) { this.setXml(e, null); }
     * 
     * @Deprecated public void setXml(Element e, VSDFile vf) { this.setXml(vf); }
     * 
     * @Deprecated public void setXml(VSDFile vf) { }
     */
/**
     * Build this VSDecoder from an XML representation
     *
     * @param vf (VSDFile) : VSD File to pull the XML from
     * @param pn (String) : Parameter Name to find within the VSD File.
     */
@SuppressWarnings({ "cast" })
public void setXml(VSDFile vf, String pn) {
    Iterator<Element> itr;
    Element e = null;
    Element el = null;
    SoundEvent se;
    if (vf == null) {
        log.debug("Null VSD File Name");
        return;
    }
    log.debug("VSD File Name = " + vf.getName());
    // need to choose one.
    this.setVSDFilePath(vf.getName());
    // Find the <profile/> element that matches the name pn
    // List<Element> profiles = vf.getRoot().getChildren("profile");
    // java.util.Iterator i = profiles.iterator();
    java.util.Iterator<Element> i = vf.getRoot().getChildren("profile").iterator();
    while (i.hasNext()) {
        e = i.next();
        if (e.getAttributeValue("name").equals(pn)) {
            break;
        }
    }
    if (e == null) {
        // No matching profile name found.
        return;
    }
    // Set this decoder's name.
    this.setProfileName(e.getAttributeValue("name"));
    log.debug("Decoder Name = " + e.getAttributeValue("name"));
    // Check for default element.
    if (e.getChild("default") != null) {
        log.debug("" + getProfileName() + "is default.");
        is_default = true;
    } else {
        is_default = false;
    }
    // +++ DEBUG
    // Log and print all of the child elements.
    itr = (e.getChildren()).iterator();
    while (itr.hasNext()) {
        // Pull each element from the XML file.
        el = itr.next();
        log.debug("Element: " + el.toString());
        if (el.getAttribute("name") != null) {
            log.debug("  Name: " + el.getAttributeValue("name"));
            log.debug("   type: " + el.getAttributeValue("type"));
        }
    }
    // --- DEBUG
    // First, the sounds.
    String prefix = "" + this.getID() + ":";
    log.debug("VSDecoder " + this.getID() + " prefix = " + prefix);
    itr = (e.getChildren("sound")).iterator();
    while (itr.hasNext()) {
        el = (Element) itr.next();
        if (el.getAttributeValue("type") == null) {
            // Empty sound. Skip.
            log.debug("Skipping empty Sound.");
            continue;
        } else if (el.getAttributeValue("type").equals("configurable")) {
            // Handle configurable sounds.
            ConfigurableSound cs = new ConfigurableSound(prefix + el.getAttributeValue("name"));
            cs.setXml(el, vf);
            sound_list.put(el.getAttributeValue("name"), cs);
        } else if (el.getAttributeValue("type").equals("diesel")) {
            // Handle a Diesel Engine sound
            DieselSound es = new DieselSound(prefix + el.getAttributeValue("name"));
            es.setXml(el, vf);
            sound_list.put(el.getAttributeValue("name"), es);
        } else if (el.getAttributeValue("type").equals("diesel3")) {
            // Handle a Diesel Engine sound
            Diesel3Sound es = new Diesel3Sound(prefix + el.getAttributeValue("name"));
            es.setXml(el, vf);
            sound_list.put(el.getAttributeValue("name"), es);
        } else if (el.getAttributeValue("type").equals("steam")) {
            // Handle a Diesel Engine sound
            SteamSound es = new SteamSound(prefix + el.getAttributeValue("name"));
            es.setXml(el, vf);
            sound_list.put(el.getAttributeValue("name"), es);
        } else if (el.getAttributeValue("type").equals("steam1")) {
            // Handle a Steam Engine sound
            Steam1Sound es = new Steam1Sound(prefix + el.getAttributeValue("name"));
            es.setXml(el, vf);
            sound_list.put(el.getAttributeValue("name"), es);
        } else {
        // TODO: Some type other than configurable sound. Handle appropriately
        }
    }
    // Next, grab all of the SoundEvents
    // Have to do the sounds first because the SoundEvent's setXml() will
    // expect to be able to look it up.
    itr = (e.getChildren("sound-event")).iterator();
    while (itr.hasNext()) {
        el = (Element) itr.next();
        switch(SoundEvent.ButtonType.valueOf(el.getAttributeValue("buttontype").toUpperCase())) {
            case MOMENTARY:
                se = new MomentarySoundEvent(el.getAttributeValue("name"));
                break;
            case TOGGLE:
                se = new ToggleSoundEvent(el.getAttributeValue("name"));
                break;
            case ENGINE:
                se = new EngineSoundEvent(el.getAttributeValue("name"));
                break;
            case NONE:
            default:
                se = new SoundEvent(el.getAttributeValue("name"));
        }
        se.setParent(this);
        se.setXml(el, vf);
        event_list.put(se.getName(), se);
    }
    // Handle other types of children similarly here.
    // Check for an existing throttle and update speed if it exists.
    Float s = (Float) InstanceManager.throttleManagerInstance().getThrottleInfo(config.getDccAddress(), "SpeedSetting");
    if (s != null) {
        // Mimic a throttlePropertyChange to propagate the current (init) speed setting of the throttle.
        log.debug("Existing Throttle found.  Speed = " + s);
        this.throttlePropertyChange(new PropertyChangeEvent(this, "SpeedSetting", null, s));
    } else {
        log.debug("No existing throttle found.");
    }
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) Element(org.jdom2.Element)

Example 89 with Element

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

the class VSDecoderPreferences method store.

private org.jdom2.Element store() {
    org.jdom2.Element ec;
    org.jdom2.Element e = new org.jdom2.Element("VSDecoderPreferences");
    e.setAttribute("isAutoStartingEngine", "" + isAutoStartingEngine());
    e.setAttribute("isAutoLoadingDefaultVSDFile", "" + isAutoLoadingDefaultVSDFile());
    ec = new Element("DefaultVSDFilePath");
    ec.setText("" + getDefaultVSDFilePath());
    e.addContent(ec);
    ec = new Element("DefaultVSDFileName");
    ec.setText("" + getDefaultVSDFileName());
    e.addContent(ec);
    // ListenerPosition generates its own XML
    e.addContent(_listenerPosition.getXml("ListenerPosition"));
    ec = new Element("AudioMode");
    ec.setText("" + AudioModeMap.get(_audioMode));
    e.addContent(ec);
    return e;
}
Also used : Element(org.jdom2.Element) Element(org.jdom2.Element)

Example 90 with Element

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

the class VSDecoderPreferences method save.

public void save() {
    if (prefFile == null) {
        return;
    }
    XmlFile xf = new XmlFile() {
    };
    // odd syntax is due to XmlFile being abstract
    xf.makeBackupFile(prefFile);
    File file = new File(prefFile);
    try {
        //The file does not exist, create it before writing
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            if (// make directory, check result
            !parentDir.mkdir()) {
                log.error("failed to make parent directory");
            }
        }
        if (// create file, check result
        !file.createNewFile()) {
            log.error("createNewFile failed");
        }
    } catch (Exception exp) {
        log.error("Exception while writing the new VSDecoder preferences file, may not be complete: " + exp);
    }
    try {
        Element root = new Element("vsdecoder-preferences");
        //Document doc = XmlFile.newDocument(root, XmlFile.dtdLocation+"vsdecoder-preferences.dtd");
        Document doc = XmlFile.newDocument(root);
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/throttle.xsl"?>
        /*TODO      java.util.Map<String,String> m = new java.util.HashMap<String,String>();
             m.put("type", "text/xsl");
             m.put("href", jmri.jmrit.XmlFile.xsltLocation+"throttles-preferences.xsl");
             ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
             doc.addContent(0,p);*/
        root.setContent(store());
        xf.writeXML(file, doc);
    } catch (Exception ex) {
        // TODO fix null value for Attribute
        log.warn("Exception in storing vsdecoder preferences xml: " + ex);
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Aggregations

Element (org.jdom2.Element)673 Attribute (org.jdom2.Attribute)103 Document (org.jdom2.Document)64 File (java.io.File)49 ArrayList (java.util.ArrayList)36 NamedIcon (jmri.jmrit.catalog.NamedIcon)28 IOException (java.io.IOException)27 DataConversionException (org.jdom2.DataConversionException)26 JDOMException (org.jdom2.JDOMException)26 XmlFile (jmri.jmrit.XmlFile)24 Test (org.junit.Test)24 Editor (jmri.jmrit.display.Editor)22 DocType (org.jdom2.DocType)21 Turnout (jmri.Turnout)20 ProcessingInstruction (org.jdom2.ProcessingInstruction)16 Point (java.awt.Point)15 HashMap (java.util.HashMap)15 SignalHead (jmri.SignalHead)15 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)15 Dimension (java.awt.Dimension)14