Search in sources :

Example 26 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class AbstractNamedBeanManagerConfigXML method loadProperties.

/**
     * Load all key/value properties
     *
     * @param t    The NamedBean being loaded
     * @param elem The existing Element
     */
void loadProperties(NamedBean t, Element elem) {
    Element p = elem.getChild("properties");
    if (p == null) {
        return;
    }
    for (Object next : p.getChildren("property")) {
        Element e = (Element) next;
        try {
            Class<?> cl;
            Constructor<?> ctor;
            // create key string
            String key = e.getChild("key").getText();
            // constructed from Strings, similar to the value code below.
            if (!(e.getChild("key").getAttributeValue("class") == null || e.getChild("key").getAttributeValue("class").equals("") || e.getChild("key").getAttributeValue("class").equals("java.lang.String"))) {
                log.warn("NamedBean {} property key of invalid non-String type {} not supported", t.getSystemName(), e.getChild("key").getAttributeValue("class"));
            }
            // create value object
            Object value = null;
            if (e.getChild("value") != null) {
                cl = Class.forName(e.getChild("value").getAttributeValue("class"));
                ctor = cl.getConstructor(new Class<?>[] { String.class });
                value = ctor.newInstance(new Object[] { e.getChild("value").getText() });
            }
            // store
            t.setProperty(key, value);
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | java.lang.reflect.InvocationTargetException ex) {
            log.error("Error loading properties", ex);
        }
    }
}
Also used : Element(org.jdom2.Element)

Example 27 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class JmriJTablePersistenceManager method savePreferences.

@Override
public synchronized void savePreferences(Profile profile) {
    log.debug("Saving preferences (dirty={})...", this.dirty);
    Element element = new Element(TABLES_ELEMENT, TABLES_NAMESPACE);
    if (!this.columns.isEmpty()) {
        this.columns.entrySet().stream().map((entry) -> {
            Element table = new Element("table").setAttribute("name", entry.getKey());
            Element columnsElement = new Element("columns");
            entry.getValue().entrySet().stream().map((column) -> {
                Element columnElement = new Element("column").setAttribute("name", column.getKey());
                if (column.getValue().getOrder() != -1) {
                    columnElement.setAttribute("order", Integer.toString(column.getValue().getOrder()));
                }
                if (column.getValue().getWidth() != -1) {
                    columnElement.setAttribute("width", Integer.toString(column.getValue().getWidth()));
                }
                columnElement.setAttribute("hidden", Boolean.toString(column.getValue().getHidden()));
                return columnElement;
            }).forEach((columnElement) -> {
                columnsElement.addContent(columnElement);
            });
            table.addContent(columnsElement);
            List<SortKey> keys = this.sortKeys.get(entry.getKey());
            if (keys != null) {
                Element sorter = new Element(SORT_ORDER);
                keys.stream().forEach((key) -> {
                    sorter.addContent(new Element("sortKey").setAttribute("column", Integer.toString(key.getColumn())).setAttribute(SORT_ORDER, key.getSortOrder().name()));
                });
                table.addContent(sorter);
            }
            return table;
        }).forEach((table) -> {
            element.addContent(table);
        });
    }
    try {
        ProfileUtils.getUserInterfaceConfiguration(ProfileManager.getDefault().getActiveProfile()).putConfigurationFragment(JDOMUtil.toW3CElement(element), false);
    } catch (JDOMException ex) {
        log.error("Unable to save user preferences", ex);
    }
    this.dirty = false;
}
Also used : Arrays(java.util.Arrays) TableColumnModel(javax.swing.table.TableColumnModel) Enumeration(java.util.Enumeration) SortKey(javax.swing.RowSorter.SortKey) XTableColumnModel(jmri.util.swing.XTableColumnModel) AbstractPreferencesManager(jmri.util.prefs.AbstractPreferencesManager) LoggerFactory(org.slf4j.LoggerFactory) ProfileManager(jmri.profile.ProfileManager) HashMap(java.util.HashMap) Timer(java.util.Timer) InitializationException(jmri.util.prefs.InitializationException) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) JDOMUtil(jmri.util.jdom.JDOMUtil) ProfileUtils(jmri.profile.ProfileUtils) Map(java.util.Map) Profile(jmri.profile.Profile) TableColumnModelEvent(javax.swing.event.TableColumnModelEvent) RowSorter(javax.swing.RowSorter) TimerTask(java.util.TimerTask) ListSelectionEvent(javax.swing.event.ListSelectionEvent) Nonnull(javax.annotation.Nonnull) PropertyChangeEvent(java.beans.PropertyChangeEvent) ChangeEvent(javax.swing.event.ChangeEvent) Logger(org.slf4j.Logger) TableColumn(javax.swing.table.TableColumn) RowSorterListener(javax.swing.event.RowSorterListener) Set(java.util.Set) Objects(java.util.Objects) SortOrder(javax.swing.SortOrder) List(java.util.List) TableColumnModelListener(javax.swing.event.TableColumnModelListener) PropertyChangeListener(java.beans.PropertyChangeListener) JTable(javax.swing.JTable) DataConversionException(org.jdom2.DataConversionException) CheckForNull(javax.annotation.CheckForNull) RowSorterEvent(javax.swing.event.RowSorterEvent) Element(org.jdom2.Element) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) List(java.util.List) JDOMException(org.jdom2.JDOMException)

Example 28 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class JMenuUtil method jMenuFromElement.

@Nonnull
static JMenu jMenuFromElement(@CheckForNull Element main, WindowInterface wi, Object context) {
    boolean addSep = false;
    String name = "<none>";
    if (main == null) {
        log.warn("Menu from element called without an element");
        return new JMenu(name);
    }
    name = LocaleSelector.getAttribute(main, "name");
    //Next statement left in if the xml file hasn't been converted
    if ((name == null) || (name.equals(""))) {
        if (main.getChild("name") != null) {
            name = main.getChild("name").getText();
        }
    }
    JMenu menu = new JMenu(name);
    ArrayList<Integer> mnemonicList = new ArrayList<Integer>();
    for (Object item : main.getChildren("node")) {
        JMenuItem menuItem = null;
        Element child = (Element) item;
        if (child.getChildren("node").size() == 0) {
            // leaf
            if ((child.getText().trim()).equals("separator")) {
                addSep = true;
            } else {
                if (!(SystemType.isMacOSX() && UIManager.getLookAndFeel().isNativeLookAndFeel() && ((child.getChild("adapter") != null && child.getChild("adapter").getText().equals("apps.gui3.TabbedPreferencesAction")) || (child.getChild("current") != null && child.getChild("current").getText().equals("quit"))))) {
                    if (addSep) {
                        menu.addSeparator();
                        addSep = false;
                    }
                    Action act = actionFromNode(child, wi, context);
                    menu.add(menuItem = new JMenuItem(act));
                }
            }
        } else {
            if (addSep) {
                menu.addSeparator();
                addSep = false;
            }
            if (child.getChild("group") != null && child.getChild("group").getText().equals("yes")) {
                //A seperate method is required for creating radio button groups
                menu.add(createMenuGroupFromElement(child, wi, context));
            } else {
                // not leaf
                menu.add(menuItem = jMenuFromElement(child, wi, context));
            }
        }
        if (menuItem != null && child.getChild("current") != null) {
            setMenuItemInterAction(context, child.getChild("current").getText(), menuItem);
        }
        if (menuItem != null && child.getChild("mnemonic") != null) {
            int mnemonic = convertStringToKeyEvent(child.getChild("mnemonic").getText());
            if (mnemonicList.contains(mnemonic)) {
                log.error("Menu Item '" + menuItem.getText() + "' Mnemonic '" + child.getChild("mnemonic").getText() + "' has already been assigned");
            } else {
                menuItem.setMnemonic(mnemonic);
                mnemonicList.add(mnemonic);
            }
        }
    }
    return menu;
}
Also used : Action(javax.swing.Action) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu) Nonnull(javax.annotation.Nonnull)

Example 29 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class JMenuUtil method loadMenu.

public static JMenu[] loadMenu(String path, WindowInterface wi, Object context) {
    Element root = rootFromName(path);
    int n = root.getChildren("node").size();
    JMenu[] retval = new JMenu[n];
    int i = 0;
    ArrayList<Integer> mnemonicList = new ArrayList<Integer>();
    for (Object child : root.getChildren("node")) {
        JMenu menuItem = jMenuFromElement((Element) child, wi, context);
        retval[i++] = menuItem;
        if (((Element) child).getChild("mnemonic") != null) {
            int mnemonic = convertStringToKeyEvent(((Element) child).getChild("mnemonic").getText());
            if (mnemonicList.contains(mnemonic)) {
                log.error("Menu item '" + menuItem.getText() + "' Mnemonic '" + ((Element) child).getChild("mnemonic").getText() + "' has already been assigned");
            } else {
                menuItem.setMnemonic(mnemonic);
                mnemonicList.add(mnemonic);
            }
        }
    }
    return retval;
}
Also used : Element(org.jdom2.Element) ArrayList(java.util.ArrayList) JMenu(javax.swing.JMenu)

Example 30 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class JMenuUtil method createMenuGroupFromElement.

@Nonnull
static JMenu createMenuGroupFromElement(@CheckForNull Element main, WindowInterface wi, Object context) {
    String name = "<none>";
    if (main == null) {
        log.warn("Menu from element called without an element");
        return new JMenu(name);
    }
    name = LocaleSelector.getAttribute(main, "name");
    //Next statement left in if the xml file hasn't been converted
    if ((name == null) || (name.equals(""))) {
        if (main.getChild("name") != null) {
            name = main.getChild("name").getText();
        }
    }
    JMenu menu = new JMenu(name);
    ButtonGroup group = new ButtonGroup();
    for (Object item : main.getChildren("node")) {
        Element elem = (Element) item;
        Action act = actionFromNode(elem, wi, context);
        JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(act);
        group.add(menuItem);
        menu.add(menuItem);
        if (elem.getChild("current") != null) {
            setMenuItemInterAction(context, elem.getChild("current").getText(), menuItem);
        }
    }
    return menu;
}
Also used : Action(javax.swing.Action) ButtonGroup(javax.swing.ButtonGroup) Element(org.jdom2.Element) JRadioButtonMenuItem(javax.swing.JRadioButtonMenuItem) JMenu(javax.swing.JMenu) Nonnull(javax.annotation.Nonnull)

Aggregations

Element (org.jdom2.Element)829 Attribute (org.jdom2.Attribute)76 Document (org.jdom2.Document)75 Test (org.junit.Test)70 File (java.io.File)53 ArrayList (java.util.ArrayList)45 JDOMException (org.jdom2.JDOMException)37 IOException (java.io.IOException)34 HashMap (java.util.HashMap)28 NamedIcon (jmri.jmrit.catalog.NamedIcon)27 List (java.util.List)26 XmlFile (jmri.jmrit.XmlFile)24 SAXBuilder (org.jdom2.input.SAXBuilder)21 Turnout (jmri.Turnout)20 DataConversionException (org.jdom2.DataConversionException)20 DocType (org.jdom2.DocType)19 Editor (jmri.jmrit.display.Editor)18 XMLOutputter (org.jdom2.output.XMLOutputter)18 Namespace (org.jdom2.Namespace)17 Point (java.awt.Point)15