Search in sources :

Example 6 with Content

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

the class LocoLabelXml method load.

/**
     * Create a PositionableShape, 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
    Editor ed = (Editor) o;
    LocoLabel ll = new LocoLabel(ed);
    Element elem = element.getChild("size");
    ll.setWidth(getInt(elem, "width"));
    ll.setHeight(getInt(elem, "height"));
    if (elem != null && elem.getAttribute("systemName") != null) {
        String name = elem.getAttribute("systemName").getValue();
        OBlockManager manager = InstanceManager.getDefault(jmri.jmrit.logix.OBlockManager.class);
        OBlock block = manager.getBySystemName(name);
        ll.setBlock(block);
        if (elem.getAttribute("trainName") != null && block != null) {
            block.setValue(elem.getAttribute("trainName").getValue());
        }
    } else {
        // don't put into editor's content list without           
        return;
    }
    ed.putItem(ll);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(ll, Editor.MARKERS, element);
}
Also used : LocoLabel(jmri.jmrit.display.controlPanelEditor.shape.LocoLabel) Element(org.jdom2.Element) OBlockManager(jmri.jmrit.logix.OBlockManager) Editor(jmri.jmrit.display.Editor) OBlock(jmri.jmrit.logix.OBlock)

Example 7 with Content

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

the class SwitchboardEditorXml method load.

/**
     * Create a SwitchboardEditor object, then register and fill it, then pop it in a
     * JFrame
     *
     * @param shared Top level Element to unpack.
     * @return true if successful
     */
@Override
public boolean load(Element shared, Element perNode) {
    boolean result = true;
    // find coordinates
    int x = 0;
    int y = 0;
    int height = 400;
    int width = 300;
    int rangemin = 1;
    int rangemax = 32;
    int columns = 4;
    String type = "T";
    String connection = "I";
    String shape = "key";
    String name;
    try {
        x = shared.getAttribute("x").getIntValue();
        y = shared.getAttribute("y").getIntValue();
        height = shared.getAttribute("height").getIntValue();
        width = shared.getAttribute("width").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert Switchboard's attribute");
        result = false;
    }
    // find the name
    // this will be replaced by the name as stored NOI18N
    name = "Switchboard";
    if (shared.getAttribute("name") != null) {
        name = shared.getAttribute("name").getValue();
    }
    // confirm that panel hasn't already been loaded
    if (jmri.jmrit.display.PanelMenu.instance().isPanelNameUsed(name)) {
        log.warn("File contains a panel with the same name (" + name + ") as an existing panel");
        result = false;
    }
    SwitchboardEditor panel = new SwitchboardEditor(name);
    //panel.makeFrame(name);
    jmri.jmrit.display.PanelMenu.instance().addEditorPanel(panel);
    panel.getTargetFrame().setLocation(x, y);
    panel.getTargetFrame().setSize(width, height);
    panel.setTitle();
    // Load editor option flags. This has to be done before the content
    // items are loaded, to preserve the individual item settings.
    Attribute a;
    boolean value = true;
    if ((a = shared.getAttribute("editable")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setAllEditable(value);
    value = true;
    if ((a = shared.getAttribute("showtooltips")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setAllShowTooltip(value);
    value = true;
    if ((a = shared.getAttribute("controlling")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setAllControlling(value);
    value = false;
    if ((a = shared.getAttribute("hide")) != null && a.getValue().equals("yes")) {
        value = true;
    }
    panel.setShowHidden(value);
    value = true;
    if ((a = shared.getAttribute("panelmenu")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setPanelMenuVisible(value);
    String state = "both";
    if ((a = shared.getAttribute("scrollable")) != null) {
        state = a.getValue();
    }
    panel.setScroll(state);
    value = false;
    if ((a = shared.getAttribute("hideunconnected")) != null && a.getValue().equals("yes")) {
        value = true;
    }
    panel.setHideUnconnected(value);
    try {
        rangemin = shared.getAttribute("rangemin").getIntValue();
        rangemax = shared.getAttribute("rangemax").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert Switchboard's range");
        result = false;
    }
    panel.setPanelMenuRangeMin(rangemin);
    panel.setPanelMenuRangeMax(rangemax);
    type = shared.getAttribute("type").getValue();
    panel.setSwitchType(type);
    connection = shared.getAttribute("connection").getValue();
    panel.setSwitchManu(connection);
    shape = shared.getAttribute("shape").getValue();
    panel.setSwitchShape(shape);
    try {
        columns = shared.getAttribute("columns").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert Switchboard's column count");
        result = false;
    }
    panel.setColumns(columns);
    String defaultTextColor = "black";
    if (shared.getAttribute("defaulttextcolor") != null) {
        defaultTextColor = shared.getAttribute("defaulttextcolor").getValue();
    }
    panel.setDefaultTextColor(defaultTextColor);
    // set color if needed
    try {
        int red = shared.getAttribute("redBackground").getIntValue();
        int blue = shared.getAttribute("blueBackground").getIntValue();
        int green = shared.getAttribute("greenBackground").getIntValue();
        //panel.setBackground(new Color(red, green, blue));
        panel.setDefaultBackgroundColor(new Color(red, green, blue));
    } catch (org.jdom2.DataConversionException e) {
        log.warn("Could not parse color attributes!");
    } catch (NullPointerException e) {
    // considered normal if the attributes are not present
    }
    //set the (global) editor display widgets to their flag settings
    panel.initView();
    // load the contents with their individual option settings
    List<Element> items = shared.getChildren();
    for (int i = 0; i < items.size(); i++) {
        // get the class, hence the adapter object to do loading
        Element item = items.get(i);
        String adapterName = item.getAttribute("class").getValue();
        log.debug("load via " + adapterName);
        try {
            XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
            // and do it
            adapter.load(item, panel);
            if (!panel.loadOK()) {
                result = false;
            }
        } catch (Exception e) {
            log.error("Exception while loading " + item.getName() + ":" + e);
            result = false;
            e.printStackTrace();
        }
    }
    // dispose of url correction data
    panel.disposeLoadData();
    // display the results, with the editor in back
    panel.pack();
    panel.setAllEditable(panel.isEditable());
    // we don't pack the target frame here, because size was specified
    // TODO: Work out why, when calling this method, panel size is increased
    // vertically (at least on MS Windows)
    // always show the panel
    panel.getTargetFrame().setVisible(true);
    // register the resulting panel for later configuration
    ConfigureManager cm = InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
    if (cm != null) {
        cm.registerUser(panel);
    }
    // reset the size and position, in case the display caused it to change
    panel.getTargetFrame().setLocation(x, y);
    panel.getTargetFrame().setSize(width, height);
    panel.updatePressed();
    return result;
}
Also used : Attribute(org.jdom2.Attribute) Color(java.awt.Color) Element(org.jdom2.Element) Point(java.awt.Point) SwitchboardEditor(jmri.jmrit.display.switchboardEditor.SwitchboardEditor) ConfigureManager(jmri.ConfigureManager) AbstractXmlAdapter(jmri.configurexml.AbstractXmlAdapter) XmlAdapter(jmri.configurexml.XmlAdapter)

Example 8 with Content

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

the class WarrantPreferences method loadSpeedMap.

// Avoid firePropertyChange until SignalSpeedMap is completely loaded
private boolean loadSpeedMap(Element child) {
    if (child == null) {
        return false;
    }
    Element rampParms = child.getChild(STEP_INCREMENTS);
    if (rampParms == null) {
        return false;
    }
    Attribute a;
    if ((a = rampParms.getAttribute(TIME_INCREMENT)) != null) {
        try {
            this._msIncrTime = a.getIntValue();
        } catch (DataConversionException ex) {
            this._msIncrTime = 500;
            log.error("Unable to read ramp time increment. Setting to default value (500ms).", ex);
        }
    }
    if ((a = rampParms.getAttribute(RAMP_INCREMENT)) != null) {
        try {
            this._throttleIncr = a.getFloatValue();
        } catch (DataConversionException ex) {
            this._throttleIncr = 0.03f;
            log.error("Unable to read ramp throttle increment. Setting to default value (0.03).", ex);
        }
    }
    if ((a = rampParms.getAttribute(THROTTLE_SCALE)) != null) {
        try {
            _throttleScale = a.getFloatValue();
        } catch (DataConversionException ex) {
            _throttleScale = .90f;
            log.error("Unable to read throttle scale. Setting to default value (0.90f).", ex);
        }
    }
    rampParms = child.getChild(SPEED_NAME_PREFS);
    if (rampParms == null) {
        return false;
    }
    if ((a = rampParms.getAttribute("percentNormal")) != null) {
        if (a.getValue().equals("yes")) {
            _interpretation = 1;
        } else {
            _interpretation = 2;
        }
    }
    if ((a = rampParms.getAttribute(INTERPRETATION)) != null) {
        try {
            _interpretation = a.getIntValue();
        } catch (DataConversionException ex) {
            _interpretation = 1;
            log.error("Unable to read interpetation of Speed Map. Setting to default value % normal.", ex);
        }
    }
    HashMap<String, Float> map = new LinkedHashMap<>();
    List<Element> list = rampParms.getChildren();
    for (int i = 0; i < list.size(); i++) {
        String name = list.get(i).getName();
        Float speed = 0f;
        try {
            speed = Float.valueOf(list.get(i).getText());
        } catch (NumberFormatException nfe) {
            log.error("Speed names has invalid content for {} = ", name, list.get(i).getText());
        }
        log.debug("Add {}, {} to AspectSpeed Table", name, speed);
        map.put(name, speed);
    }
    // no firePropertyChange
    this.setSpeedNames(map);
    rampParms = child.getChild(APPEARANCE_PREFS);
    if (rampParms == null) {
        return false;
    }
    LinkedHashMap<String, String> heads = new LinkedHashMap<>();
    list = rampParms.getChildren();
    for (int i = 0; i < list.size(); i++) {
        String name = Bundle.getMessage(list.get(i).getName());
        String speed = list.get(i).getText();
        heads.put(name, speed);
    }
    // no firePropertyChange
    this.setAppearances(heads);
    // Now set SignalSpeedMap members.
    SignalSpeedMap speedMap = jmri.InstanceManager.getDefault(SignalSpeedMap.class);
    speedMap.setRampParams(_msIncrTime, _msIncrTime);
    speedMap.setDefaultThrottleFactor(_throttleScale);
    speedMap.setLayoutScale(_scale);
    speedMap.setAspects(new HashMap<>(this._speedNames), _interpretation);
    speedMap.setAppearances(new HashMap<>(this._headAppearances));
    return true;
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) LinkedHashMap(java.util.LinkedHashMap) SignalSpeedMap(jmri.implementation.SignalSpeedMap) DataConversionException(org.jdom2.DataConversionException)

Example 9 with Content

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

the class CopyRosterItemAction method doTransfer.

@Override
boolean doTransfer() {
    // read the from file, change the ID, and write it out
    log.debug("doTransfer starts");
    // ensure preferences will be found
    FileUtil.createDirectory(LocoFile.getFileLocation());
    // locate the file
    //File f = new File(mFullFromFilename);
    // read it
    // used as a temporary
    LocoFile lf = new LocoFile();
    Element lroot;
    try {
        lroot = lf.rootFromName(mFullFromFilename);
    } catch (Exception e) {
        log.error("Exception while loading loco XML file: " + mFullFromFilename + " exception: " + e);
        return false;
    }
    // create a new entry
    mToEntry = new RosterEntry(mFromEntry, mToID);
    // set the filename from the ID
    mToEntry.ensureFilenameExists();
    // detach the content element from it's existing file so 
    // it can be reused
    lroot.detach();
    // transfer the contents to a new file
    LocoFile newLocoFile = new LocoFile();
    File fout = new File(LocoFile.getFileLocation() + mToEntry.getFileName());
    newLocoFile.writeFile(fout, lroot, mToEntry);
    return true;
}
Also used : Element(org.jdom2.Element) File(java.io.File)

Example 10 with Content

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

the class VariableTableModel method setIndxRow.

/**
     * Load one row in the IndexedVariableTableModel, by reading in the Element
     * containing its definition.
     * <p>
     * Invoked from DecoderFile
     *
     * @param row       number of row to fill
     * @param e         Element of type "variable"
     * @param productID product ID of decoder, passed in so that subparts of the
     *                  variable can use it for selection
     */
public int setIndxRow(int row, Element e, String productID, String modelID, String familyID) {
    // get the values for the VariableValue ctor
    // Note the name variable is actually the label attribute
    String name = LocaleSelector.getAttribute(e, "label");
    if (log.isDebugEnabled()) {
        log.debug("Starting to setIndexedRow \"" + name + "\" row " + row);
    }
    String cvName = e.getAttributeValue("CVname");
    String item = (e.getAttribute("item") != null ? e.getAttribute("item").getValue() : null);
    String comment = LocaleSelector.getAttribute(e, "comment");
    int piVal = Integer.valueOf(e.getAttribute("PI").getValue()).intValue();
    int siVal = (e.getAttribute("SI") != null ? Integer.valueOf(e.getAttribute("SI").getValue()).intValue() : -1);
    String cv = e.getAttribute("CV").getValue();
    String mask = null;
    if (e.getAttribute("mask") != null) {
        mask = e.getAttribute("mask").getValue();
    } else {
        mask = "VVVVVVVV";
    }
    boolean readOnly = e.getAttribute("readOnly") != null ? e.getAttribute("readOnly").getValue().equals("yes") : false;
    boolean infoOnly = e.getAttribute("infoOnly") != null ? e.getAttribute("infoOnly").getValue().equals("yes") : false;
    boolean writeOnly = e.getAttribute("writeOnly") != null ? e.getAttribute("writeOnly").getValue().equals("yes") : false;
    boolean opsOnly = e.getAttribute("opsOnly") != null ? e.getAttribute("opsOnly").getValue().equals("yes") : false;
    JButton br = new JButton("Read");
    _readButtons.addElement(br);
    JButton bw = new JButton("Write");
    _writeButtons.addElement(bw);
    setButtonsReadWrite(readOnly, infoOnly, writeOnly, bw, br, row);
    if (_indxCvModel == null) {
        log.error("IndexedCvModel reference is null; can not add variables");
        return -1;
    }
    // add the information to the indexed CV model
    int _newRow = _indxCvModel.addIndxCV(cvName, _piCv, piVal, _siCv, siVal, cv, readOnly, infoOnly, writeOnly);
    if (_newRow != row) {
        row = _newRow;
        if (log.isDebugEnabled()) {
            log.debug("new row is " + _newRow + ", row was " + row);
        }
    }
    // Find and process the specific content types
    VariableValue iv;
    iv = createIndexedVariableFromElement(e, name, comment, cvName, readOnly, infoOnly, writeOnly, opsOnly, cv, mask, item, productID, modelID, familyID);
    if (iv == null) {
        // trouble reporting
        reportBogus();
        return -1;
    }
    processModifierElements(e, iv);
    setToolTip(e, iv);
    // record new variable, update state, hook up listeners
    rowVector.addElement(iv);
    iv.setState(VariableValue.FROMFILE);
    iv.addPropertyChangeListener(this);
    // set to default value if specified (CV load may later override this)
    Attribute a;
    if ((a = e.getAttribute("default")) != null) {
        String val = a.getValue();
        if (log.isDebugEnabled()) {
            log.debug("Found default value: " + val + " for " + name);
        }
        iv.setIntValue(Integer.valueOf(val).intValue());
        if (_indxCvModel.getCvByRow(row).getInfoOnly()) {
            _indxCvModel.getCvByRow(row).setState(VariableValue.READ);
        } else {
            // correct for transition to "edited"
            _indxCvModel.getCvByRow(row).setState(VariableValue.FROMFILE);
        }
    } else {
        _indxCvModel.getCvByRow(row).setState(VariableValue.UNKNOWN);
    }
    return row;
}
Also used : Attribute(org.jdom2.Attribute) JButton(javax.swing.JButton)

Aggregations

Element (org.jdom2.Element)77 Document (org.jdom2.Document)27 IOException (java.io.IOException)18 JDOMException (org.jdom2.JDOMException)16 File (java.io.File)11 MCRException (org.mycore.common.MCRException)11 Content (org.jdom2.Content)10 MCRContent (org.mycore.common.content.MCRContent)10 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)10 XMLOutputter (org.jdom2.output.XMLOutputter)9 SAXBuilder (org.jdom2.input.SAXBuilder)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Attribute (org.jdom2.Attribute)6 MCRPath (org.mycore.datamodel.niofs.MCRPath)6 SAXException (org.xml.sax.SAXException)6 JsonElement (com.google.gson.JsonElement)5 Color (java.awt.Color)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 MCRObject (org.mycore.datamodel.metadata.MCRObject)4