Search in sources :

Example 61 with Attribute

use of org.omegat.filters3.Attribute in project JMRI by JMRI.

the class PaneProgPane method newGridItem.

/**
     * Create a grid item from the JDOM Element
     *
     * @param element     element containing grid item contents
     * @param showStdName show the name following the rules for the
     * <em>nameFmt</em> element
     * @param modelElem   element containing the decoder model
     * @param globs       properties to configure the layout
     * @return a panel containing the group
     */
public JPanel newGridItem(Element element, boolean showStdName, Element modelElem, GridGlobals globs) {
    // get item-level attributes
    List<Attribute> itemAttList = element.getAttributes();
    List<Attribute> attList = new ArrayList<>(globs.gridAttList);
    // merge grid and item-level attributes
    attList.addAll(itemAttList);
    //                log.info("New gridtiem -----------------------------------------------");
    //                log.info("Attribute list:"+attList);
    attList.add(new Attribute(LAST_GRIDX, ""));
    attList.add(new Attribute(LAST_GRIDY, ""));
    //                log.info("Previous gridxCurrent="+globs.gridxCurrent+";gridyCurrent="+globs.gridyCurrent);
    for (int j = 0; j < attList.size(); j++) {
        Attribute attrib = attList.get(j);
        String attribName = attrib.getName();
        String attribRawValue = attrib.getValue();
        Field constraint = null;
        String constraintType = null;
        // make sure we only process the last gridx or gridy attribute in the list
        if (attribName.equals("gridx")) {
            Attribute a = new Attribute(LAST_GRIDX, attribRawValue);
            attList.set(attList.size() - 2, a);
            //. don't process now
            continue;
        }
        if (attribName.equals("gridy")) {
            Attribute a = new Attribute(LAST_GRIDY, attribRawValue);
            attList.set(attList.size() - 1, a);
            //. don't process now
            continue;
        }
        if (attribName.equals(LAST_GRIDX)) {
            // we must be at end of original list, restore last gridx
            attribName = "gridx";
            if (attribRawValue.equals("")) {
                // don't process blank (unused)
                continue;
            }
        }
        if (attribName.equals(LAST_GRIDY)) {
            // we must be at end of original list, restore last gridy
            attribName = "gridy";
            if (attribRawValue.equals("")) {
                // don't process blank (unused)
                continue;
            }
        }
        if ((attribName.equals("gridx") || attribName.equals("gridy")) && attribRawValue.equals("RELATIVE")) {
            // NEXT is a synonym for RELATIVE
            attribRawValue = "NEXT";
        }
        if (attribName.equals("gridx") && attribRawValue.equals("CURRENT")) {
            attribRawValue = String.valueOf(Math.max(0, globs.gridxCurrent));
        }
        if (attribName.equals("gridy") && attribRawValue.equals("CURRENT")) {
            attribRawValue = String.valueOf(Math.max(0, globs.gridyCurrent));
        }
        if (attribName.equals("gridx") && attribRawValue.equals("NEXT")) {
            attribRawValue = String.valueOf(++globs.gridxCurrent);
        }
        if (attribName.equals("gridy") && attribRawValue.equals("NEXT")) {
            attribRawValue = String.valueOf(++globs.gridyCurrent);
        }
        //                    log.info("attribName="+attribName+";attribRawValue="+attribRawValue);
        try {
            constraint = globs.gridConstraints.getClass().getDeclaredField(attribName);
            constraintType = constraint.getType().toString();
            constraint.setAccessible(true);
        } catch (NoSuchFieldException ex) {
            log.error("Unrecognised attribute \"" + attribName + "\", skipping");
            continue;
        }
        switch(constraintType) {
            case "int":
                {
                    int attribValue;
                    try {
                        attribValue = Integer.valueOf(attribRawValue);
                        constraint.set(globs.gridConstraints, attribValue);
                    } catch (IllegalAccessException ey) {
                        log.error("Unable to set constraint \"" + attribName + ". IllegalAccessException error thrown.");
                    } catch (NumberFormatException ex) {
                        try {
                            Field constant = globs.gridConstraints.getClass().getDeclaredField(attribRawValue);
                            constant.setAccessible(true);
                            attribValue = (Integer) GridBagConstraints.class.getField(attribRawValue).get(constant);
                            constraint.set(globs.gridConstraints, attribValue);
                        } catch (NoSuchFieldException ey) {
                            log.error("Invalid value \"" + attribRawValue + "\" for attribute \"" + attribName + "\"");
                        } catch (IllegalAccessException ey) {
                            log.error("Unable to set constraint \"" + attribName + ". IllegalAccessException error thrown.");
                        }
                    }
                    break;
                }
            case "double":
                {
                    double attribValue;
                    try {
                        attribValue = Double.valueOf(attribRawValue);
                        constraint.set(globs.gridConstraints, attribValue);
                    } catch (IllegalAccessException ey) {
                        log.error("Unable to set constraint \"" + attribName + ". IllegalAccessException error thrown.");
                    } catch (NumberFormatException ex) {
                        log.error("Invalid value \"" + attribRawValue + "\" for attribute \"" + attribName + "\"");
                    }
                    break;
                }
            case "class java.awt.Insets":
                try {
                    String[] insetStrings = attribRawValue.split(",");
                    if (insetStrings.length == 4) {
                        Insets attribValue = new Insets(Integer.valueOf(insetStrings[0]), Integer.valueOf(insetStrings[1]), Integer.valueOf(insetStrings[2]), Integer.valueOf(insetStrings[3]));
                        constraint.set(globs.gridConstraints, attribValue);
                    } else {
                        log.error("Invalid value \"" + attribRawValue + "\" for attribute \"" + attribName + "\"");
                        log.error("Value should be four integers of the form \"top,left,bottom,right\"");
                    }
                } catch (IllegalAccessException ey) {
                    log.error("Unable to set constraint \"" + attribName + ". IllegalAccessException error thrown.");
                } catch (NumberFormatException ex) {
                    log.error("Invalid value \"" + attribRawValue + "\" for attribute \"" + attribName + "\"");
                    log.error("Value should be four integers of the form \"top,left,bottom,right\"");
                }
                break;
            default:
                log.error("Required \"" + constraintType + "\" handler for attribute \"" + attribName + "\" not defined in JMRI code");
                log.error("Please file a JMRI bug report at https://sourceforge.net/p/jmri/bugs/new/");
                break;
        }
    }
    //                log.info("Updated globs.GridBagConstraints.gridx="+globs.gridConstraints.gridx+";globs.GridBagConstraints.gridy="+globs.gridConstraints.gridy);
    // create a panel to add as a new grid item
    final JPanel c = new JPanel();
    panelList.add(c);
    GridBagLayout g = new GridBagLayout();
    GridBagConstraints cs = new GridBagConstraints();
    c.setLayout(g);
    // handle the xml definition
    // for all elements in the grid item
    List<Element> elemList = element.getChildren();
    log.trace("newGridItem starting with {} elements", elemList.size());
    for (int i = 0; i < elemList.size(); i++) {
        // update the grid position
        cs.gridy = 0;
        cs.gridx++;
        Element e = elemList.get(i);
        String name = e.getName();
        log.trace("newGridItem processing {} element", name);
        // decode the type
        if (name.equals("display")) {
            // its a variable
            // load the variable
            newVariable(e, c, g, cs, showStdName);
        } else if (name.equals("separator")) {
            // its a separator
            JSeparator j = new JSeparator(javax.swing.SwingConstants.VERTICAL);
            cs.fill = GridBagConstraints.BOTH;
            cs.gridheight = GridBagConstraints.REMAINDER;
            g.setConstraints(j, cs);
            c.add(j);
            cs.fill = GridBagConstraints.NONE;
            cs.gridheight = 1;
        } else if (name.equals("label")) {
            cs.gridheight = GridBagConstraints.REMAINDER;
            makeLabel(e, c, g, cs);
        } else if (name.equals("soundlabel")) {
            cs.gridheight = GridBagConstraints.REMAINDER;
            makeSoundLabel(e, c, g, cs);
        } else if (name.equals("cvtable")) {
            makeCvTable(cs, g, c);
        } else if (name.equals("indxcvtable")) {
            log.debug("starting to build IndexedCvTable pane");
            JTable indxcvTable = new JTable(_indexedCvModel);
            JScrollPane cvScroll = new JScrollPane(indxcvTable);
            indxcvTable.setDefaultRenderer(JTextField.class, new ValueRenderer());
            indxcvTable.setDefaultRenderer(JButton.class, new ValueRenderer());
            indxcvTable.setDefaultEditor(JTextField.class, new ValueEditor());
            indxcvTable.setDefaultEditor(JButton.class, new ValueEditor());
            indxcvTable.setRowHeight(new JButton("X").getPreferredSize().height);
            indxcvTable.setPreferredScrollableViewportSize(new Dimension(700, indxcvTable.getRowHeight() * 14));
            cvScroll.setColumnHeaderView(indxcvTable.getTableHeader());
            // don't want a horizontal scroll bar
            // Need to see the whole row at one time
            //                indxcvTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            cs.gridwidth = GridBagConstraints.REMAINDER;
            g.setConstraints(cvScroll, cs);
            c.add(cvScroll);
            cs.gridwidth = 1;
            // remember which indexed CVs to read/write
            for (int j = 0; j < _indexedCvModel.getRowCount(); j++) {
                String sz = "CV" + _indexedCvModel.getName(j);
                int in = _varModel.findVarIndex(sz);
                indexedCvList.add(in);
            }
            _cvTable = true;
            log.debug("end of building IndexedCvTable pane");
        } else if (name.equals("fnmapping")) {
            pickFnMapPanel(c, g, cs, modelElem);
        } else if (name.equals("dccaddress")) {
            JPanel l = addDccAddressPanel(e);
            if (l.getComponentCount() > 0) {
                cs.gridheight = GridBagConstraints.REMAINDER;
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridheight = 1;
            }
        } else if (name.equals("column")) {
            // nested "column" elements ...
            cs.gridheight = GridBagConstraints.REMAINDER;
            JPanel l = newColumn(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridheight = 1;
            }
        } else if (name.equals("row")) {
            // nested "row" elements ...
            cs.gridwidth = GridBagConstraints.REMAINDER;
            JPanel l = newRow(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridwidth = 1;
            }
        } else if (name.equals("grid")) {
            // nested "grid" elements ...
            cs.gridwidth = GridBagConstraints.REMAINDER;
            JPanel l = newGrid(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridwidth = 1;
            }
        } else if (name.equals("group")) {
            // nested "group" elements ...
            JPanel l = newGroup(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
            }
        } else if (!name.equals("qualifier")) {
            // its a mistake
            log.error("No code to handle element of type " + e.getName() + " in newGridItem");
        }
    }
    globs.gridxCurrent = globs.gridConstraints.gridx;
    globs.gridyCurrent = globs.gridConstraints.gridy;
    // add glue to the bottom to allow resize
    if (c.getComponentCount() > 0) {
        c.add(Box.createVerticalGlue());
    }
    // handle qualification if any
    QualifierAdder qa = new QualifierAdder() {

        @Override
        protected Qualifier createQualifier(VariableValue var, String relation, String value) {
            return new JComponentQualifier(c, var, Integer.parseInt(value), relation);
        }

        @Override
        protected void addListener(java.beans.PropertyChangeListener qc) {
            c.addPropertyChangeListener(qc);
        }
    };
    qa.processModifierElements(element, _varModel);
    return c;
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) JSeparator(javax.swing.JSeparator) ValueRenderer(jmri.jmrit.symbolicprog.ValueRenderer) JTextField(javax.swing.JTextField) Field(java.lang.reflect.Field) QualifierAdder(jmri.jmrit.symbolicprog.QualifierAdder) JScrollPane(javax.swing.JScrollPane) VariableValue(jmri.jmrit.symbolicprog.VariableValue) ValueEditor(jmri.jmrit.symbolicprog.ValueEditor) Dimension(java.awt.Dimension) JTable(javax.swing.JTable)

Example 62 with Attribute

use of org.omegat.filters3.Attribute in project JMRI by JMRI.

the class PaneProgPane method newVariable.

/**
     * Add the representation of a single variable. The variable is defined by a
     * JDOM variable Element from the XML file.
     *
     * @param var         element containing variable
     * @param col         column to insert label into
     * @param g           panel layout manager
     * @param cs          constraints on layout manager
     * @param showStdName show the name following the rules for the
     * <em>nameFmt</em> element
     */
public void newVariable(Element var, JComponent col, GridBagLayout g, GridBagConstraints cs, boolean showStdName) {
    // get the name
    String name = var.getAttribute("item").getValue();
    // if it doesn't exist, do nothing
    int i = _varModel.findVarIndex(name);
    if (i < 0) {
        log.trace("Variable \"{}\" not found, omitted", name);
        return;
    }
    //        Leave here for now. Need to track pre-existing corner-case issue
    //        log.info("Entry item="+name+";cs.gridx="+cs.gridx+";cs.gridy="+cs.gridy+";cs.anchor="+cs.anchor+";cs.ipadx="+cs.ipadx);
    // check label orientation
    Attribute attr;
    // this default is also set in the DTD
    String layout = "left";
    if ((attr = var.getAttribute("layout")) != null && attr.getValue() != null) {
        layout = attr.getValue();
    }
    // load label if specified, else use name
    String label = name;
    if (!showStdName) {
        // get name attribute from variable, as that's the mfg name
        label = _varModel.getLabel(i);
    }
    String temp = LocaleSelector.getAttribute(var, "label");
    if (temp != null) {
        label = temp;
    }
    // get representation; store into the list to be programmed
    JComponent rep = getRepresentation(name, var);
    varList.add(i);
    // create the paired label
    JLabel l = new WatchingLabel(label, rep);
    int spaceWidth = getFontMetrics(l.getFont()).stringWidth(" ");
    // assemble v from label, rep
    switch(layout) {
        case "left":
            cs.anchor = GridBagConstraints.EAST;
            cs.ipadx = spaceWidth;
            g.setConstraints(l, cs);
            col.add(l);
            cs.ipadx = 0;
            cs.gridx++;
            cs.anchor = GridBagConstraints.WEST;
            g.setConstraints(rep, cs);
            col.add(rep);
            break;
        //        log.info("Exit item="+name+";cs.gridx="+cs.gridx+";cs.gridy="+cs.gridy+";cs.anchor="+cs.anchor+";cs.ipadx="+cs.ipadx);
        case "right":
            cs.anchor = GridBagConstraints.EAST;
            g.setConstraints(rep, cs);
            col.add(rep);
            cs.gridx++;
            cs.anchor = GridBagConstraints.WEST;
            cs.ipadx = spaceWidth;
            g.setConstraints(l, cs);
            col.add(l);
            cs.ipadx = 0;
            break;
        case "below":
            // variable in center of upper line
            cs.anchor = GridBagConstraints.CENTER;
            g.setConstraints(rep, cs);
            col.add(rep);
            // label aligned like others
            cs.gridy++;
            cs.anchor = GridBagConstraints.WEST;
            cs.ipadx = spaceWidth;
            g.setConstraints(l, cs);
            col.add(l);
            cs.ipadx = 0;
            break;
        case "above":
            // label aligned like others
            cs.anchor = GridBagConstraints.WEST;
            cs.ipadx = spaceWidth;
            g.setConstraints(l, cs);
            col.add(l);
            cs.ipadx = 0;
            // variable in center of lower line
            cs.gridy++;
            cs.anchor = GridBagConstraints.CENTER;
            g.setConstraints(rep, cs);
            col.add(rep);
            break;
        default:
            log.error("layout internally inconsistent: " + layout);
    }
}
Also used : Attribute(org.jdom2.Attribute) JComponent(javax.swing.JComponent) JLabel(javax.swing.JLabel)

Example 63 with Attribute

use of org.omegat.filters3.Attribute in project JMRI by JMRI.

the class WiThrottlePreferences method load.

@Override
public void load(Element child) {
    Attribute a;
    if ((a = child.getAttribute("isUseEStop")) != null) {
        setUseEStop(a.getValue().equalsIgnoreCase("true"));
        this.asLoadedUseEStop = this.isUseEStop();
    }
    if ((a = child.getAttribute("getEStopDelay")) != null) {
        try {
            setEStopDelay(Integer.valueOf(a.getValue()));
            this.asLoadedEStopDelay = this.getEStopDelay();
        } catch (NumberFormatException e) {
            log.debug(e.getLocalizedMessage(), e);
        }
    }
    if ((a = child.getAttribute("isUseMomF2")) != null) {
        setUseMomF2(a.getValue().equalsIgnoreCase("true"));
        this.asLoadedUseMomF2 = this.isUseMomF2();
    }
    if ((a = child.getAttribute("getPort")) != null) {
        try {
            setPort(a.getIntValue());
        } catch (DataConversionException ex) {
            log.error("Port {} is invalid.", a.getValue());
        }
        this.asLoadedPort = this.getPort();
    }
    if ((a = child.getAttribute("isAllowTrackPower")) != null) {
        setAllowTrackPower(a.getValue().equalsIgnoreCase("true"));
        this.asLoadedAllowTrackPower = this.isAllowTrackPower();
    }
    if ((a = child.getAttribute("isAllowTurnout")) != null) {
        setAllowTurnout(a.getValue().equalsIgnoreCase("true"));
        this.asLoadedAllowTurnout = this.isAllowTurnout();
    }
    if ((a = child.getAttribute("isAllowRoute")) != null) {
        setAllowRoute(a.getValue().equalsIgnoreCase("true"));
        this.asLoadedAllowRoute = this.isAllowRoute();
    }
    if ((a = child.getAttribute("isAllowConsist")) != null) {
        setAllowConsist(a.getValue().equalsIgnoreCase("true"));
        this.asLoadedAllowConsist = this.isAllowConsist();
    }
    if ((a = child.getAttribute("isUseWiFiConsist")) != null) {
        setUseWiFiConsist(a.getValue().equalsIgnoreCase("true"));
        this.asLoadedUseWiFiConsist = this.isUseWiFiConsist();
    }
}
Also used : Attribute(org.jdom2.Attribute) DataConversionException(org.jdom2.DataConversionException)

Example 64 with Attribute

use of org.omegat.filters3.Attribute in project JMRI by JMRI.

the class GuiLafConfigPaneXml method load.

@Override
public boolean load(Element shared, Element perNode) {
    boolean result = true;
    UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
    java.util.Hashtable<String, String> installedLAFs = new java.util.Hashtable<String, String>(plafs.length);
    for (int i = 0; i < plafs.length; i++) {
        installedLAFs.put(plafs[i].getName(), plafs[i].getClassName());
    }
    String name = shared.getAttribute("LAFclass").getValue();
    String className = installedLAFs.get(name);
    log.debug("GUI selection: " + name + " class name: " + className);
    // set the GUI
    if (className != null) {
        InstanceManager.getDefault(GuiLafPreferencesManager.class).setLookAndFeel(name);
        try {
            if (!className.equals(UIManager.getLookAndFeel().getClass().getName())) {
                log.debug("set GUI to " + name + "," + className);
                updateLookAndFeel(name, className);
            } else {
                log.debug("skip updateLAF as already has className==" + className);
            }
        } catch (Exception ex) {
            log.error("Exception while setting GUI look & feel: " + ex);
            result = false;
        }
    }
    Attribute langAttr = shared.getAttribute("LocaleLanguage");
    Attribute countryAttr = shared.getAttribute("LocaleCountry");
    Attribute varAttr = shared.getAttribute("LocaleVariant");
    if (countryAttr != null && langAttr != null && varAttr != null) {
        Locale locale = new Locale(langAttr.getValue(), countryAttr.getValue(), varAttr.getValue());
        Locale.setDefault(locale);
        InstanceManager.getDefault(GuiLafPreferencesManager.class).setLocale(locale);
    }
    Attribute clickAttr = shared.getAttribute("nonStandardMouseEvent");
    if (clickAttr != null) {
        boolean nonStandardMouseEvent = clickAttr.getValue().equals("yes");
        jmri.util.swing.SwingSettings.setNonStandardMouseEvent(nonStandardMouseEvent);
        InstanceManager.getDefault(GuiLafPreferencesManager.class).setNonStandardMouseEvent(nonStandardMouseEvent);
    }
    Attribute graphicAttr = shared.getAttribute("graphicTableState");
    if (graphicAttr != null) {
        boolean graphicTableState = graphicAttr.getValue().equals("yes");
        InstanceManager.getDefault(GuiLafPreferencesManager.class).setGraphicTableState(graphicTableState);
    }
    GuiLafConfigPane g = new GuiLafConfigPane();
    ConfigureManager cm = jmri.InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
    if (cm != null) {
        cm.registerPref(g);
    }
    Attribute fontsize = shared.getAttribute("fontsize");
    if (fontsize != null) {
        int size = Integer.parseInt(fontsize.getValue());
        InstanceManager.getDefault(GuiLafPreferencesManager.class).setFontSize(size);
        this.setUIFontSize(size);
    }
    return result;
}
Also used : Locale(java.util.Locale) GuiLafPreferencesManager(apps.gui.GuiLafPreferencesManager) Attribute(org.jdom2.Attribute) GuiLafConfigPane(apps.GuiLafConfigPane) UnsupportedLookAndFeelException(javax.swing.UnsupportedLookAndFeelException) ConfigureManager(jmri.ConfigureManager)

Example 65 with Attribute

use of org.omegat.filters3.Attribute in project sis by apache.

the class DecoderWrapper method numericValue.

/**
 * Returns the value of the attribute of the given name as a number, or {@code null} if none.
 *
 * @param  name  the name of the attribute to search, or {@code null}.
 * @return the attribute value, or {@code null} if none or unparsable or if the given name was null.
 */
@Override
public Number numericValue(final String name) {
    if (name != null) {
        for (final Group group : groups) {
            final Attribute attribute = findAttribute(group, name);
            if (attribute != null) {
                final Number value = attribute.getNumericValue();
                if (value != null) {
                    return value;
                }
                String asString = attribute.getStringValue();
                if (asString != null && !(asString = asString.trim()).isEmpty()) {
                    return parseNumber(asString);
                }
            }
        }
    }
    return null;
}
Also used : Group(ucar.nc2.Group) Attribute(ucar.nc2.Attribute)

Aggregations

Attribute (org.jdom2.Attribute)148 Element (org.jdom2.Element)104 Document (org.jdom2.Document)18 ArrayList (java.util.ArrayList)17 DataConversionException (org.jdom2.DataConversionException)16 Editor (jmri.jmrit.display.Editor)15 Test (org.junit.Test)15 IOException (java.io.IOException)14 NamedIcon (jmri.jmrit.catalog.NamedIcon)13 Attribute (org.bouncycastle.asn1.x509.Attribute)11 HashMap (java.util.HashMap)10 List (java.util.List)9 HashSet (java.util.HashSet)7 Map (java.util.Map)7 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)7 Attribute (ucar.nc2.Attribute)7 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)5 Asn1Object (com.android.hotspot2.asn1.Asn1Object)5 Asn1Oid (com.android.hotspot2.asn1.Asn1Oid)5 OidMappings (com.android.hotspot2.asn1.OidMappings)5