create Swing Tool Bar from Action - Java Swing

Java examples for Swing:Action

Description

create Swing Tool Bar from Action

Demo Code


import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

public class Main{
    public static JToolBar createToolBar(String label, Action[] actions) {
        if (actions == null) {
            return null;
        }//from  www.  j  a va 2  s.c  om
        if (label == null || label.length() == 0) {
            return createToolBar(actions);
        }
        JLabel labelLabel = new JLabel(label);
        JToolBar toolBar = createToolBar(actions);
        toolBar.add(labelLabel, 0);
        return toolBar;
    }
    public static JToolBar createToolBar(String label, Object usingObject) {
        return usingObject != null ? createToolBar(label,
                ActionIntrospector.getActions(usingObject)) : null;
    }
    public static JToolBar createToolBar(Object usingObject,
            String[] justThese) {
        Action[] actions = ActionIntrospector.getActions(usingObject,
                justThese);
        return createToolBar(actions);
    }
    public static JToolBar createToolBar(Action[] actions) {
        if (actions == null) {
            return null;
        }
        JToolBar toolBar = new JToolBar();
        JButton[] buttons = createButtons(actions);
        for (int i = 0; i < actions.length; i++) {
            if (actions[i].getValue(Action.SMALL_ICON) != null) {
                buttons[i].setText(null);
            }
            toolBar.add(buttons[i]);
        }
        return toolBar;
    }
    public static JToolBar createToolBar(String label, Object[] usingObject) {
        if (usingObject == null) {
            return null;
        }
        JToolBar toolBar = new JToolBar();
        if (label != null && label.length() > 0) {
            toolBar.add(new JLabel(label));
        }
        for (int i = 0; i < usingObject.length; i++) {
            if (usingObject[i] == null) {
                continue;
            }
            Action[] actions = ActionIntrospector
                    .getActions(usingObject[i]);
            for (int j = 0; j < actions.length; j++) {
                toolBar.add(createButton(actions[j]));
            }
            if (i < usingObject.length - 1) {
                toolBar.addSeparator();
            }
        }
        return toolBar;
    }
    public static JToolBar createToolBar(Object[] usingObject) {
        return createToolBar(null, usingObject);
    }
    public static JButton[] createButtons(Action[] actions) {
        if (actions == null) {
            return null;
        }
        JButton[] button = new JButton[actions.length];
        for (int i = 0; i < button.length; i++) {
            button[i] = createButton(actions[i]);
        }
        return button;
    }
    public static JButton createButton(Action action) {
        JButton button = new JButton(action);
        JButtonListener listener = new JButtonListener(button);
        action.addPropertyChangeListener(listener);
        configureButton(action, listener);
        return button;
    }
    public static JButton createButton(Object source, String method) {
        Action action = ActionIntrospector.getAction(source, method);
        return createButton(action);
    }
    public static void configureButton(Action action,
            PropertyChangeListener listener) {
        if (action == null || listener == null) {
            return;
        }
        action.addPropertyChangeListener(listener);
        action.putValue("listener", listener);
        if (action instanceof AbstractAction) {
            AbstractAction abstractAction = (AbstractAction) action;
            Object[] key = abstractAction.getKeys();
            for (int i = 0; i < key.length; i++) {
                listener.propertyChange(new PropertyChangeEvent(action,
                        key[i].toString(), null, abstractAction
                                .getValue(key[i].toString())));
            }
        }
    }
}

Related Tutorials