001package jmri.util.swing;
002
003import javax.swing.Action;
004import javax.swing.JButton;
005import javax.swing.JToolBar;
006import org.jdom2.Element;
007
008/**
009 * Common utility methods for working with JToolBars.
010 * <p>
011 * Chief among these is the loadToolBar method, for creating a JToolBar from an
012 * XML definition
013 * <p>
014 * Only parses top level of XML file, since ToolBars have only level.
015 *
016 * @author Bob Jacobsen Copyright 2003, 2010
017 * @since 2.9.4
018 */
019public class JToolBarUtil extends GuiUtilBase {
020
021    static public JToolBar loadToolBar(String name) {
022        return loadToolBar(name, null, null);  // tool bar without window or context
023    }
024
025    static public JToolBar loadToolBar(String name, WindowInterface wi, Object context) {
026        Element root = rootFromName(name);
027
028        JToolBar retval = new JToolBar(root.getChild("name").getText());
029
030        for (Object item : root.getChildren("node")) {
031            Action act = actionFromNode((Element) item, wi, context);
032            if (act == null) {
033                continue;
034            }
035            if (act.getValue(javax.swing.Action.SMALL_ICON) != null) {
036                // icon present, add explicitly
037                JButton b = new JButton((javax.swing.Icon) act.getValue(javax.swing.Action.SMALL_ICON));
038                b.setAction(act);
039                retval.add(b);
040            } else {
041                retval.add(new JButton(act));
042            }
043        }
044        return retval;
045
046    }
047}