Search in sources :

Example 61 with Text

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

the class LinkingLabelXml method store.

/**
     * Default implementation for storing the contents of a LinkingLabel
     *
     * @param o Object to store, of type LinkingLabel
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    LinkingLabel p = (LinkingLabel) o;
    if (!p.isActive()) {
        // if flagged as inactive, don't store
        return null;
    }
    Element element = new Element("linkinglabel");
    storeCommonAttributes(p, element);
    if (p.isText()) {
        if (p.getUnRotatedText() != null) {
            element.setAttribute("text", p.getUnRotatedText());
        }
        storeTextInfo(p, element);
    }
    if (p.isIcon() && p.getIcon() != null) {
        element.setAttribute("icon", "yes");
        element.addContent(storeIcon("icon", (NamedIcon) p.getIcon()));
    }
    element.addContent(new Element("url").addContent(p.getUrl()));
    element.setAttribute("class", "jmri.jmrit.display.configurexml.LinkingLabelXml");
    return element;
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) LinkingLabel(jmri.jmrit.display.LinkingLabel) Element(org.jdom2.Element)

Example 62 with Text

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

the class BlockValueFile method writeBlockValues.

/*
     *  Writes out block values to a file in the user's preferences directory
     *  If there are no defined Blocks, no file is written.
     *  If none of the defined Blocks have values, no file is written.
     */
public void writeBlockValues() throws java.io.IOException {
    log.debug("entered writeBlockValues");
    List<String> blocks = blockManager.getSystemNameList();
    if (blocks.size() > 0) {
        // there are blocks defined, create root element
        root = new Element("block_values");
        doc = newDocument(root, dtdLocation + "block-values.dtd");
        boolean valuesFound = false;
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/block-values.xsl"?>
        java.util.Map<String, String> m = new java.util.HashMap<String, String>();
        m.put("type", "text/xsl");
        m.put("href", xsltLocation + "blockValues.xsl");
        org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
        doc.addContent(0, p);
        // save block values in xml format
        Element values = new Element("blockvalues");
        for (int i = 0; i < blocks.size(); i++) {
            String sname = blocks.get(i);
            Block b = blockManager.getBySystemName(sname);
            if (b != null) {
                Object o = b.getValue();
                if (o != null) {
                    // block has value, save it
                    Element val = new Element("block");
                    val.setAttribute("systemname", sname);
                    if (o instanceof jmri.jmrit.roster.RosterEntry) {
                        val.setAttribute("value", ((jmri.jmrit.roster.RosterEntry) o).getId());
                        val.setAttribute("valueClass", "jmri.jmrit.roster.RosterEntry");
                    } else {
                        val.setAttribute("value", o.toString());
                    }
                    int v = b.getDirection();
                    if (v != jmri.Path.NONE) {
                        val.setAttribute("dir", "" + v);
                    }
                    values.addContent(val);
                    valuesFound = true;
                }
            } else {
                log.error("Block " + sname + " was not found.");
            }
        }
        root.addContent(values);
        // write out the file if values were found
        if (valuesFound) {
            try {
                if (!checkFile(defaultFileName)) {
                    // file does not exist, create it
                    File file = new File(defaultFileName);
                    if (// create and check result
                    !file.createNewFile()) {
                        log.error("createNewFile failed");
                    }
                }
                // write content to file
                writeXML(findFile(defaultFileName), doc);
            } catch (java.io.IOException ioe) {
                log.error("IO Exception " + ioe);
                throw (ioe);
            }
        }
    }
}
Also used : Element(org.jdom2.Element) Block(jmri.Block) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 63 with Text

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

the class PositionableLabelXml method store.

/**
     * Default implementation for storing the contents of a PositionableLabel
     *
     * @param o Object to store, of type PositionableLabel
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    PositionableLabel p = (PositionableLabel) o;
    if (!p.isActive()) {
        // if flagged as inactive, don't store
        return null;
    }
    Element element = new Element("positionablelabel");
    storeCommonAttributes(p, element);
    if (p.isText()) {
        if (p.getUnRotatedText() != null) {
            element.setAttribute("text", p.getUnRotatedText());
        }
        storeTextInfo(p, element);
    }
    if (p.isIcon() && p.getIcon() != null) {
        element.setAttribute("icon", "yes");
        element.addContent(storeIcon("icon", (NamedIcon) p.getIcon()));
    }
    element.setAttribute("class", "jmri.jmrit.display.configurexml.PositionableLabelXml");
    return element;
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Element(org.jdom2.Element) PositionableLabel(jmri.jmrit.display.PositionableLabel)

Example 64 with Text

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

the class PositionableLabelXml method load.

/**
     * Create a PositionableLabel, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    // create the objects
    PositionableLabel l = null;
    // get object class and determine editor being used
    Editor editor = (Editor) o;
    if (element.getAttribute("icon") != null) {
        NamedIcon icon;
        String name = element.getAttribute("icon").getValue();
        //            if (log.isDebugEnabled()) log.debug("icon attribute= "+name);
        if (name.equals("yes")) {
            icon = getNamedIcon("icon", element, "PositionableLabel ", editor);
        } else {
            icon = NamedIcon.getIconByName(name);
            if (icon == null) {
                icon = editor.loadFailed("PositionableLabel", name);
                if (icon == null) {
                    log.info("PositionableLabel icon removed for url= " + name);
                    return;
                }
            }
        }
        // abort if name != yes and have null icon
        if (icon == null && !name.equals("yes")) {
            log.info("PositionableLabel icon removed for url= " + name);
            return;
        }
        l = new PositionableLabel(icon, editor);
        try {
            Attribute a = element.getAttribute("rotate");
            if (a != null && icon != null) {
                int rotation = element.getAttribute("rotate").getIntValue();
                icon.setRotation(rotation, l);
            }
        } catch (org.jdom2.DataConversionException e) {
        }
        if (name.equals("yes")) {
            NamedIcon nIcon = loadIcon(l, "icon", element, "PositionableLabel ", editor);
            if (nIcon != null) {
                l.updateIcon(nIcon);
            } else {
                log.info("PositionableLabel icon removed for url= " + name);
                return;
            }
        } else {
            l.updateIcon(icon);
        }
    }
    if (element.getAttribute("text") != null) {
        if (l == null) {
            l = new PositionableLabel(element.getAttribute("text").getValue(), editor);
        }
        loadTextInfo(l, element);
    } else if (l == null) {
        log.error("PositionableLabel is null!");
        if (log.isDebugEnabled()) {
            java.util.List<Attribute> attrs = element.getAttributes();
            log.debug("\tElement Has " + attrs.size() + " Attributes:");
            for (int i = 0; i < attrs.size(); i++) {
                Attribute a = attrs.get(i);
                log.debug("\t\t" + a.getName() + " = " + a.getValue());
            }
            java.util.List<Element> kids = element.getChildren();
            log.debug("\tElementHas " + kids.size() + " children:");
            for (int i = 0; i < kids.size(); i++) {
                Element e = kids.get(i);
                log.debug("\t\t" + e.getName() + " = \"" + e.getValue() + "\"");
            }
        }
        editor.loadFailed();
        return;
    }
    editor.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.LABELS, element);
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) PositionableLabel(jmri.jmrit.display.PositionableLabel) DataConversionException(org.jdom2.DataConversionException) Editor(jmri.jmrit.display.Editor)

Example 65 with Text

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

the class SwitchboardEditor$BeanSwitchXml method store.

/**
     * Default implementation for storing the contents of a BeanSwitch.
     * Used the display Switchboard switches in JMRI web server.
     *
     * @param o Object to store, of type BeanSwitch
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    SwitchboardEditor.BeanSwitch bs = (SwitchboardEditor.BeanSwitch) o;
    Element element = new Element("beanswitch");
    // include attributes
    element.setAttribute("label", bs.getNameString());
    if (bs.getNamedBean() != null) {
        element.setAttribute("connected", "true");
    } else {
        element.setAttribute("connected", "false");
    }
    // get state textual info (only used for shape 'button')
    // includes beanswitch label to operate like SensorIcon
    // never null
    Element textElement = new Element("activeText");
    textElement.setAttribute("text", bs.getActiveText());
    element.addContent(textElement);
    textElement = new Element("inactiveText");
    textElement.setAttribute("text", bs.getInactiveText());
    element.addContent(textElement);
    textElement = new Element("unknownText");
    textElement.setAttribute("text", bs.getUnknownText());
    element.addContent(textElement);
    textElement = new Element("inconsistentText");
    textElement.setAttribute("text", bs.getInconsistentText());
    element.addContent(textElement);
    String txt = bs.getTooltip();
    if (txt != null) {
        Element elem = new Element("tooltip").addContent(txt);
        element.addContent(elem);
    }
    element.setAttribute("class", "jmri.jmrit.display.switchboardEditor.configurexml.SwitchboardEditor$BeanSwitchXml");
    return element;
}
Also used : SwitchboardEditor(jmri.jmrit.display.switchboardEditor.SwitchboardEditor) BeanSwitch(jmri.jmrit.display.switchboardEditor.SwitchboardEditor.BeanSwitch) Element(org.jdom2.Element) BeanSwitch(jmri.jmrit.display.switchboardEditor.SwitchboardEditor.BeanSwitch)

Aggregations

Element (org.jdom2.Element)83 Document (org.jdom2.Document)36 File (java.io.File)21 ProcessingInstruction (org.jdom2.ProcessingInstruction)17 IOException (java.io.IOException)14 Text (org.jdom2.Text)12 Attribute (org.jdom2.Attribute)11 XMLOutputter (org.jdom2.output.XMLOutputter)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)9 XmlFile (jmri.jmrit.XmlFile)9 ArrayList (java.util.ArrayList)6 JDOMException (org.jdom2.JDOMException)6 StringWriter (java.io.StringWriter)5 NamedIcon (jmri.jmrit.catalog.NamedIcon)5 FileOutputStream (java.io.FileOutputStream)4 LinkedHashMap (java.util.LinkedHashMap)4 FileNotFoundException (java.io.FileNotFoundException)3 URL (java.net.URL)3 Locale (java.util.Locale)3