Search in sources :

Example 51 with Element

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

the class TrainInfoFile method writeTrainInfo.

/*
     *  Writes out Dispatcher options to a file in the user's preferences directory
     */
public void writeTrainInfo(TrainInfo tf, String name) throws java.io.IOException {
    log.debug("entered writeTrainInfo");
    root = new Element("traininfofile");
    doc = newDocument(root, dtdLocation + "dispatcher-traininfo.dtd");
    // add XSLT processing instruction
    // <?xml-stylesheet type="text/xsl" href="XSLT/block-values.xsl"?>
    java.util.Map<String, String> m = new java.util.HashMap<>();
    m.put("type", "text/xsl");
    m.put("href", xsltLocation + "dispatcher-traininfo.xsl");
    org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    // save Dispatcher TrainInfo in xml format
    Element traininfo = new Element("traininfo");
    traininfo.setAttribute("transitname", tf.getTransitName());
    traininfo.setAttribute("trainname", tf.getTrainName());
    traininfo.setAttribute("dccaddress", tf.getDCCAddress());
    traininfo.setAttribute("trainintransit", "" + (tf.getTrainInTransit() ? "yes" : "no"));
    traininfo.setAttribute("startblockname", tf.getStartBlockName());
    traininfo.setAttribute("endblockname", tf.getDestinationBlockName());
    traininfo.setAttribute("trainfromroster", "" + (tf.getTrainFromRoster() ? "yes" : "no"));
    traininfo.setAttribute("trainfromtrains", "" + (tf.getTrainFromTrains() ? "yes" : "no"));
    traininfo.setAttribute("trainfromuser", "" + (tf.getTrainFromUser() ? "yes" : "no"));
    traininfo.setAttribute("priority", Integer.toString(tf.getPriority()));
    traininfo.setAttribute("resetwhendone", "" + (tf.getResetWhenDone() ? "yes" : "no"));
    switch(tf.getDelayedRestart()) {
        case ActiveTrain.SENSORDELAY:
            traininfo.setAttribute("delayedrestart", "sensor");
            traininfo.setAttribute("delayedrestartsensor", tf.getRestartSensorName());
            break;
        case ActiveTrain.TIMEDDELAY:
            traininfo.setAttribute("delayedrestart", "timed");
            traininfo.setAttribute("delayedrestarttime", Integer.toString(tf.getRestartDelayMin()));
            break;
        default:
            traininfo.setAttribute("delayedrestart", "no");
            break;
    }
    traininfo.setAttribute("reverseatend", "" + (tf.getReverseAtEnd() ? "yes" : "no"));
    if (tf.getDelayedStart() == ActiveTrain.TIMEDDELAY) {
        traininfo.setAttribute("delayedstart", "timed");
    } else if (tf.getDelayedStart() == ActiveTrain.SENSORDELAY) {
        traininfo.setAttribute("delayedstart", "sensor");
        if (tf.getDelaySensorName() != null) {
            traininfo.setAttribute("delayedSensor", tf.getDelaySensorName());
        }
    }
    traininfo.setAttribute("terminatewhendone", (tf.getTerminateWhenDone() ? "yes" : "no"));
    traininfo.setAttribute("departuretimehr", Integer.toString(tf.getDepartureTimeHr()));
    traininfo.setAttribute("departuretimemin", Integer.toString(tf.getDepartureTimeMin()));
    traininfo.setAttribute("traintype", tf.getTrainType());
    traininfo.setAttribute("autorun", "" + (tf.getAutoRun() ? "yes" : "no"));
    traininfo.setAttribute("loadatstartup", "" + (tf.getLoadAtStartup() ? "yes" : "no"));
    traininfo.setAttribute("allocatealltheway", "" + (tf.getAllocateAllTheWay() ? "yes" : "no"));
    // here save items related to automatically running active trains
    traininfo.setAttribute("speedfactor", Float.toString(tf.getSpeedFactor()));
    traininfo.setAttribute("maxspeed", Float.toString(tf.getMaxSpeed()));
    traininfo.setAttribute("ramprate", tf.getRampRate());
    traininfo.setAttribute("resistancewheels", "" + (tf.getResistanceWheels() ? "yes" : "no"));
    traininfo.setAttribute("runinreverse", "" + (tf.getRunInReverse() ? "yes" : "no"));
    traininfo.setAttribute("sounddecoder", "" + (tf.getSoundDecoder() ? "yes" : "no"));
    traininfo.setAttribute("maxtrainlength", Float.toString(tf.getMaxTrainLength()));
    root.addContent(traininfo);
    // write out the file
    try {
        if (!checkFile(fileLocation + name)) {
            // file does not exist, create it
            File file = new File(fileLocation + name);
            if (// create file and check result
            !file.createNewFile()) {
                log.error("createNewFile failed");
            }
        }
        // write content to file
        writeXML(findFile(fileLocation + name), doc);
    } catch (java.io.IOException ioe) {
        log.error("IO Exception " + ioe);
        throw (ioe);
    }
}
Also used : Element(org.jdom2.Element) File(java.io.File)

Example 52 with Element

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

the class TurnoutIconXml method load.

/**
     * Create a PositionableLabel, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       Editor as an Object
     */
@SuppressWarnings("null")
@Override
public void load(Element element, Object o) {
    // create the objects
    Editor p = (Editor) o;
    TurnoutIcon l = new TurnoutIcon(p);
    String name;
    try {
        name = element.getAttribute("turnout").getValue();
    } catch (NullPointerException e) {
        log.error("incorrect information for turnout; must use turnout name");
        p.loadFailed();
        return;
    }
    l.setTurnout(name);
    Attribute a = element.getAttribute("tristate");
    if ((a == null) || a.getValue().equals("true")) {
        l.setTristate(true);
    } else {
        l.setTristate(false);
    }
    a = element.getAttribute("momentary");
    if ((a != null) && a.getValue().equals("true")) {
        l.setMomentary(true);
    } else {
        l.setMomentary(false);
    }
    a = element.getAttribute("directControl");
    if ((a != null) && a.getValue().equals("true")) {
        l.setDirectControl(true);
    } else {
        l.setDirectControl(false);
    }
    List<Element> states = element.getChildren();
    if (states.size() > 0) {
        if (log.isDebugEnabled()) {
            log.debug("Main element has" + states.size() + " items");
        }
        // the element containing the icons
        Element elem = element;
        Element icons = element.getChild("icons");
        if (icons != null) {
            List<Element> s = icons.getChildren();
            states = s;
            // the element containing the icons
            elem = icons;
            if (log.isDebugEnabled()) {
                log.debug("icons element has" + states.size() + " items");
            }
        }
        for (int i = 0; i < states.size(); i++) {
            String state = states.get(i).getName();
            if (log.isDebugEnabled()) {
                log.debug("setIcon for state \"" + state + "\" and " + _nameMap.get(state));
            }
            NamedIcon icon = loadIcon(l, state, elem, "TurnoutIcon \"" + name + "\": icon \"" + state + "\" ", p);
            if (icon != null) {
                l.setIcon(_nameMap.get(state), icon);
            } else {
                log.info("TurnoutIcon \"" + name + "\": icon \"" + state + "\" removed");
                return;
            }
        }
        log.debug(states.size() + " icons loaded for " + l.getNameString());
    } else {
        // case when everything was attributes
        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
        }
        if (loadTurnoutIcon("thrown", rotation, l, element, name, p) == null) {
            return;
        }
        if (loadTurnoutIcon("closed", rotation, l, element, name, p) == null) {
            return;
        }
        if (loadTurnoutIcon("unknown", rotation, l, element, name, p) == null) {
            return;
        }
        if (loadTurnoutIcon("inconsistent", rotation, l, element, name, p) == null) {
            return;
        }
    }
    Element elem = element.getChild("iconmaps");
    if (elem != null) {
        Attribute attr = elem.getAttribute("family");
        if (attr != null) {
            l.setFamily(attr.getValue());
        }
    }
    p.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.TURNOUTS, element);
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Editor(jmri.jmrit.display.Editor) TurnoutIcon(jmri.jmrit.display.TurnoutIcon)

Example 53 with Element

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

the class SignalMastIconXml method store.

/**
     * Default implementation for storing the contents of a SignalMastIcon
     *
     * @param o Object to store, of type SignalMastIcon
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    SignalMastIcon p = (SignalMastIcon) o;
    if (!p.isActive()) {
        // if flagged as inactive, don't store
        return null;
    }
    Element element = new Element("signalmasticon");
    element.setAttribute("signalmast", "" + p.getNamedSignalMast().getName());
    storeCommonAttributes(p, element);
    element.setAttribute("clickmode", "" + p.getClickMode());
    element.setAttribute("litmode", "" + p.getLitMode());
    element.setAttribute("degrees", String.valueOf(p.getDegrees()));
    element.setAttribute("scale", String.valueOf(p.getScale()));
    element.setAttribute("imageset", p.useIconSet());
    element.setAttribute("class", "jmri.jmrit.display.configurexml.SignalMastIconXml");
    //storeIconInfo(p, element);
    return element;
}
Also used : SignalMastIcon(jmri.jmrit.display.SignalMastIcon) Element(org.jdom2.Element)

Example 54 with Element

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

the class SlipTurnoutIconXml method storeIcon.

Element storeIcon(String elemName, NamedIcon icon, String text) {
    Element element = super.storeIcon(elemName, icon);
    element.addContent(new Element("text").addContent(text));
    return element;
}
Also used : Element(org.jdom2.Element)

Example 55 with Element

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

the class AutomationItem method store.

/**
     * Create an XML element to represent this Entry. This member has to remain
     * synchronized with the detailed DTD in operations-trains.dtd.
     *
     * @return Contents in a JDOM Element
     */
public Element store() {
    Element e = new Element(Xml.ITEM);
    e.setAttribute(Xml.ID, getId());
    e.setAttribute(Xml.SEQUENCE_ID, Integer.toString(getSequenceId()));
    e.setAttribute(Xml.NAME, getActionName());
    // NOI18N
    e.setAttribute(Xml.ACTION_CODE, "0x" + Integer.toHexString(getActionCode()));
    e.setAttribute(Xml.HALT_FAIL, isHaltFailureEnabled() ? Xml.TRUE : Xml.FALSE);
    e.setAttribute(Xml.ACTION_RAN, isActionRan() ? Xml.TRUE : Xml.FALSE);
    e.setAttribute(Xml.ACTION_SUCCESSFUL, isActionSuccessful() ? Xml.TRUE : Xml.FALSE);
    if (getTrain() != null) {
        e.setAttribute(Xml.TRAIN_ID, getTrain().getId());
        if (getRouteLocation() != null) {
            e.setAttribute(Xml.ROUTE_LOCATION_ID, getRouteLocation().getId());
        }
    }
    if (getAutomationToRun() != null) {
        e.setAttribute(Xml.AUTOMATION_ID, getAutomationToRun().getId());
    }
    if (getGotoAutomationItem() != null) {
        e.setAttribute(Xml.GOTO_AUTOMATION_ID, getGotoAutomationItem().getId());
    }
    if (getTrainSchedule() != null) {
        e.setAttribute(Xml.TRAIN_SCHEDULE_ID, getTrainSchedule().getId());
    }
    if (!getMessage().equals(NONE) || !getMessageFail().equals(NONE)) {
        Element eMessages = new Element(Xml.MESSAGES);
        e.addContent(eMessages);
        Element eMessageOk = new Element(Xml.MESSAGE_OK);
        eMessageOk.setAttribute(Xml.MESSAGE, getMessage());
        Element eMessageFail = new Element(Xml.MESSAGE_FAIL);
        eMessageFail.setAttribute(Xml.MESSAGE, getMessageFail());
        eMessages.addContent(eMessageOk);
        eMessages.addContent(eMessageFail);
    }
    return e;
}
Also used : Element(org.jdom2.Element)

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