    001/* ========================================================================
    002 * JCommon : a free general purpose class library for the Java(tm) platform
    003 * ========================================================================
    004 *
    005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006 *
    007 * Project Info:  http://www.jfree.org/jcommon/index.html
    008 *
    009 * This library is free software; you can redistribute it and/or modify it
    010 * under the terms of the GNU Lesser General Public License as published by
    011 * the Free Software Foundation; either version 2.1 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This library is distributed in the hope that it will be useful, but
    015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017 * License for more details.
    018 *
    019 * You should have received a copy of the GNU Lesser General Public
    020 * License along with this library; if not, write to the Free Software
    021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022 * USA.
    023 *
    024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025 * in the United States and other countries.]
    026 *
    027 * -----------------
    028 * ActionButton.java
    029 * -----------------
    030 * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
    031 *
    032 * Original Author:  Thomas Morgner;
    033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034 *
    035 * $Id: ActionButton.java,v 1.7 2007/11/02 17:50:36 taqua Exp $
    036 *
    037 * ChangeLog
    038 * ---------
    039 * 30-Aug-2002 : Initial version
    040 * 01-Sep-2002 : Documentation
    041 * 10-Dec-2002 : Minor Javadoc updates (DG);
    042 * 07-Jun-2004 : Corrected source headers (DG);
    043 *
    044 */
    045
    046package org.jfree.ui.action;
    047
    048import java.beans.PropertyChangeEvent;
    049import java.beans.PropertyChangeListener;
    050import javax.swing.Action;
    051import javax.swing.Icon;
    052import javax.swing.JButton;
    053import javax.swing.KeyStroke;
    054
    055import org.jfree.util.Log;
    056
    057/**
    058 * The ActionButton is used to connect an Action and its properties to a Button. This functionality
    059 * is already implemented in JDK 1.3 but needed for JDK 1.2.2 compatibility.
    060 *
    061 * @author Thomas Morgner
    062 */
    063public class ActionButton extends JButton {
    064
    065    /**
    066     * The action.
    067     */
    068    private Action action;
    069
    070    /**
    071     * The property change handler.
    072     */
    073    private ActionEnablePropertyChangeHandler propertyChangeHandler;
    074
    075    /**
    076     * Helperclass to handle the property change event raised by the action. Changed properties in
    077     * the action will affect the button.
    078     */
    079    private class ActionEnablePropertyChangeHandler implements PropertyChangeListener {
    080
    081        /**
    082         * Default constructor.
    083         */
    084        public ActionEnablePropertyChangeHandler() {
    085        }
    086
    087        /**
    088         * Receives notification of a property change event.
    089         *
    090         * @param event the property change event.
    091         */
    092        public void propertyChange(final PropertyChangeEvent event) {
    093            try {
    094                if (event.getPropertyName().equals("enabled")) {
    095                    setEnabled(getAction().isEnabled());
    096                }
    097                else if (event.getPropertyName().equals(Action.SMALL_ICON)) {
    098                    setIcon((Icon) getAction().getValue(Action.SMALL_ICON));
    099                }
    100                else if (event.getPropertyName().equals(Action.NAME)) {
    101                    setText((String) getAction().getValue
    102                        (Action.NAME));
    103                }
    104                else if (event.getPropertyName().equals(Action.SHORT_DESCRIPTION)) {
    105                    ActionButton.this.setToolTipText((String)
    106                        getAction().getValue(Action.SHORT_DESCRIPTION));
    107                }
    108
    109                final Action ac = getAction();
    110                if (event.getPropertyName().equals(ActionDowngrade.ACCELERATOR_KEY)) {
    111                    final KeyStroke oldVal = (KeyStroke) event.getOldValue();
    112                    if (oldVal != null) {
    113                        unregisterKeyboardAction(oldVal);
    114                    }
    115                    final Object o = ac.getValue(ActionDowngrade.ACCELERATOR_KEY);
    116                    if (o instanceof KeyStroke) {
    117                        final KeyStroke k = (KeyStroke) o;
    118                        registerKeyboardAction(ac, k, WHEN_IN_FOCUSED_WINDOW);
    119                    }
    120                }
    121                else if (event.getPropertyName().equals(ActionDowngrade.MNEMONIC_KEY)) {
    122                    final Object o = ac.getValue(ActionDowngrade.MNEMONIC_KEY);
    123                    if (o != null) {
    124                        if (o instanceof Character) {
    125                            final Character c = (Character) o;
    126                            setMnemonic(c.charValue());
    127                        }
    128                        else if (o instanceof Integer) {
    129                            final Integer c = (Integer) o;
    130                            setMnemonic(c.intValue());
    131                        }
    132                    }
    133                }
    134            }
    135            catch (Exception e) {
    136                Log.warn("Error on PropertyChange in ActionButton: ", e);
    137            }
    138        }
    139    }
    140
    141    /**
    142     * Creates a Button without any text and without an assigned Action.
    143     */
    144    public ActionButton() {
    145        super();
    146    }
    147
    148    /**
    149     * Creates a Button and set the given text as label.
    150     *
    151     * @param text the label for the new button.
    152     */
    153    public ActionButton(final String text) {
    154        super(text);
    155    }
    156
    157    /**
    158     * Creates an ActionButton and sets the given text and icon on the button.
    159     *
    160     * @param text the label for the new button.
    161     * @param icon the icon for the button.
    162     */
    163    public ActionButton(final String text, final Icon icon) {
    164        super(text, icon);
    165    }
    166
    167
    168    /**
    169     * Creates an ActionButton and sets the given icon on the button.
    170     *
    171     * @param icon the icon for the button.
    172     */
    173    public ActionButton(final Icon icon) {
    174        super(icon);
    175    }
    176
    177    /**
    178     * Nreates an ActionButton and assigns the given action with the button.
    179     *
    180     * @param action the action.
    181     */
    182    public ActionButton(final Action action) {
    183        setAction(action);
    184    }
    185
    186    /**
    187     * Returns the assigned action or null if no action has been assigned.
    188     *
    189     * @return the action (possibly null).
    190     */
    191    public Action getAction() {
    192        return this.action;
    193    }
    194
    195
    196    /**
    197     * Returns and initializes the PropertyChangehandler for this ActionButton.
    198     * The PropertyChangeHandler monitors the action and updates the button if necessary.
    199     *
    200     * @return the property change handler.
    201     */
    202    private ActionEnablePropertyChangeHandler getPropertyChangeHandler() {
    203        if (this.propertyChangeHandler == null) {
    204            this.propertyChangeHandler = new ActionEnablePropertyChangeHandler();
    205        }
    206        return this.propertyChangeHandler;
    207    }
    208
    209    /**
    210     * Enables and disables this button and if an action is assigned to this button the
    211     * propertychange is forwarded to the assigned action.
    212     *
    213     * @param b the new enable-state of this button
    214     */
    215    public void setEnabled(final boolean b) {
    216        super.setEnabled(b);
    217        if (getAction() != null) {
    218            getAction().setEnabled(b);
    219        }
    220    }
    221
    222    /**
    223     * Assigns the given action to this button. The properties of the action will be assigned to
    224     * the button. If an previous action was set, the old action is unregistered.
    225     * <p/>
    226     * <ul>
    227     * <li>NAME - specifies the button text
    228     * <li>SMALL_ICON - specifies the buttons icon
    229     * <li>MNEMONIC_KEY - specifies the buttons mnemonic key
    230     * <li>ACCELERATOR_KEY - specifies the buttons accelerator
    231     * </ul>
    232     *
    233     * @param newAction the new action
    234     */
    235    public void setAction(final Action newAction) {
    236        final Action oldAction = getAction();
    237        if (oldAction != null) {
    238            removeActionListener(oldAction);
    239            oldAction.removePropertyChangeListener(getPropertyChangeHandler());
    240
    241            final Object o = oldAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
    242            if (o instanceof KeyStroke) {
    243                final KeyStroke k = (KeyStroke) o;
    244                unregisterKeyboardAction(k);
    245            }
    246        }
    247        this.action = newAction;
    248        if (this.action != null) {
    249            addActionListener(newAction);
    250            newAction.addPropertyChangeListener(getPropertyChangeHandler());
    251
    252            setText((String) (newAction.getValue(Action.NAME)));
    253            setToolTipText((String) (newAction.getValue(Action.SHORT_DESCRIPTION)));
    254            setIcon((Icon) newAction.getValue(Action.SMALL_ICON));
    255            setEnabled(this.action.isEnabled());
    256
    257            Object o = newAction.getValue(ActionDowngrade.MNEMONIC_KEY);
    258            if (o != null) {
    259                if (o instanceof Character) {
    260                    final Character c = (Character) o;
    261                    setMnemonic(c.charValue());
    262                }
    263                else if (o instanceof Integer) {
    264                    final Integer c = (Integer) o;
    265                    setMnemonic(c.intValue());
    266                }
    267            }
    268            o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
    269            if (o instanceof KeyStroke) {
    270                final KeyStroke k = (KeyStroke) o;
    271                registerKeyboardAction(newAction, k, WHEN_IN_FOCUSED_WINDOW);
    272            }
    273        }
    274    }
    275}
    276