Search in sources :

Example 81 with Element

use of com.zhan_dui.utils.m3u8.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 82 with Element

use of com.zhan_dui.utils.m3u8.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 83 with Element

use of com.zhan_dui.utils.m3u8.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 84 with Element

use of com.zhan_dui.utils.m3u8.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)

Example 85 with Element

use of com.zhan_dui.utils.m3u8.Element in project JMRI by JMRI.

the class VSDecoder method getXml.

/**
     * Get an XML representation of this VSDecoder Includes a subtree of
     * Elements for all of the associated SoundEvents, Triggers, VSDSounds, etc.
     *
     * @return Element XML Element for this VSDecoder
     */
public Element getXml() {
    Element me = new Element("vsdecoder");
    ArrayList<Element> le = new ArrayList<Element>();
    me.setAttribute("name", this.config.getProfileName());
    // If this decoder is marked as default, add the default Element.
    if (is_default) {
        me.addContent(new Element("default"));
    }
    for (SoundEvent se : event_list.values()) {
        le.add(se.getXml());
    }
    for (VSDSound vs : sound_list.values()) {
        le.add(vs.getXml());
    }
    for (Trigger t : trigger_list.values()) {
        le.add(t.getXml());
    }
    me.addContent(le);
    // Need to add whatever else here.
    return (me);
}
Also used : Element(org.jdom2.Element) ArrayList(java.util.ArrayList)

Aggregations

Element (org.jdom2.Element)673 Attribute (org.jdom2.Attribute)73 Document (org.jdom2.Document)58 File (java.io.File)49 ArrayList (java.util.ArrayList)36 NamedIcon (jmri.jmrit.catalog.NamedIcon)27 IOException (java.io.IOException)26 JDOMException (org.jdom2.JDOMException)26 XmlFile (jmri.jmrit.XmlFile)24 Test (org.junit.Test)24 Turnout (jmri.Turnout)20 DataConversionException (org.jdom2.DataConversionException)20 DocType (org.jdom2.DocType)19 Editor (jmri.jmrit.display.Editor)18 Point (java.awt.Point)15 HashMap (java.util.HashMap)15 Dimension (java.awt.Dimension)14 FileNotFoundException (java.io.FileNotFoundException)13 SignalHead (jmri.SignalHead)13 List (java.util.List)12