    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, by Thomas Morgner and Contributors.
    031 *
    032 * Original Author:  Thomas Morgner;
    033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034 *
    035 * $Id: ActionRadioButton.java,v 1.4 2007/11/02 17:50:37 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.JRadioButton;
    053import javax.swing.KeyStroke;
    054
    055import org.jfree.util.Log;
    056
    057/**
    058 * The ActionRadioButton is used to connect an Action and its properties to a JRadioButton.
    059 * This functionality is already implemented in JDK 1.3 but needed for JDK 1.2.2 compatibility.
    060 *
    061 * @author Thomas Morgner
    062 */
    063public class ActionRadioButton extends JRadioButton
    064{
    065  /** The action. */
    066  private Action action;
    067
    068  /** The property change handler. */
    069  private ActionEnablePropertyChangeHandler propertyChangeHandler;
    070
    071  /**
    072   * Helperclass to handle the property change event raised by the action. Changed properties in
    073   * the action will affect the button.
    074   */
    075  private class ActionEnablePropertyChangeHandler implements PropertyChangeListener
    076  {
    077    /**
    078     * Receives notification of a property change event.
    079     *
    080     * @param event  the property change event.
    081     */
    082    public void propertyChange(final PropertyChangeEvent event)
    083    {
    084      try
    085      {
    086        if (event.getPropertyName().equals("enabled"))
    087        {
    088          setEnabled(getAction().isEnabled());
    089        }
    090        else if (event.getPropertyName().equals(Action.SMALL_ICON))
    091        {
    092          setIcon((Icon) getAction().getValue(Action.SMALL_ICON));
    093        }
    094        else if (event.getPropertyName().equals(Action.NAME))
    095        {
    096          setText((String) getAction().getValue
    097              (Action.NAME));
    098        }
    099        else if (event.getPropertyName().equals(Action.SHORT_DESCRIPTION))
    100        {
    101          ActionRadioButton.this.setToolTipText((String)
    102              getAction().getValue(Action.SHORT_DESCRIPTION));
    103        }
    104
    105        final Action ac = getAction();
    106        if (event.getPropertyName().equals(ActionDowngrade.ACCELERATOR_KEY))
    107        {
    108          final KeyStroke oldVal = (KeyStroke) event.getOldValue();
    109          if (oldVal != null)
    110          {
    111            unregisterKeyboardAction
    112                (oldVal);
    113          }
    114          final Object o = ac.getValue(ActionDowngrade.ACCELERATOR_KEY);
    115          if (o instanceof KeyStroke && o != null)
    116          {
    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        {
    123          final Object o = ac.getValue(ActionDowngrade.MNEMONIC_KEY);
    124          if (o != null)
    125          {
    126            if (o instanceof Character)
    127            {
    128              final Character c = (Character) o;
    129              setMnemonic(c.charValue());
    130            }
    131            else if (o instanceof Integer)
    132            {
    133              final Integer c = (Integer) o;
    134              setMnemonic(c.intValue());
    135            }
    136          }
    137        }
    138      }
    139      catch (Exception e)
    140      {
    141        Log.warn("Error on PropertyChange in ActionButton: ", e);
    142      }
    143    }
    144  }
    145
    146  /**
    147   * Creates a Button without any text and without an assigned Action.
    148   */
    149  public ActionRadioButton()
    150  {
    151    super();
    152  }
    153
    154  /**
    155   * Creates a Button and set the given text as label.
    156   *
    157   * @param text  the label for the new button.
    158   */
    159  public ActionRadioButton(final String text)
    160  {
    161    super(text);
    162  }
    163
    164  /**
    165   * Creates an ActionButton and sets the given text and icon on the button.
    166   *
    167   * @param text  the label for the new button.
    168   * @param icon  the icon for the button.
    169   */
    170  public ActionRadioButton(final String text, final Icon icon)
    171  {
    172    super(text, icon);
    173  }
    174
    175
    176  /**
    177   * Creates an ActionButton and sets the given icon on the button.
    178   *
    179   * @param icon  the icon for the button.
    180   */
    181  public ActionRadioButton(final Icon icon)
    182  {
    183    super(icon);
    184  }
    185
    186  /**
    187   * Nreates an ActionButton and assigns the given action with the button.
    188   *
    189   * @param action  the action.
    190   */
    191  public ActionRadioButton(final Action action)
    192  {
    193    setAction(action);
    194  }
    195
    196  /**
    197   * Returns the assigned action or null if no action has been assigned.
    198   *
    199   * @return the action (possibly null).
    200   */
    201  public Action getAction()
    202  {
    203    return this.action;
    204  }
    205
    206
    207  /**
    208   * Returns and initializes the PropertyChangehandler for this ActionButton.
    209   * The PropertyChangeHandler monitors the action and updates the button if necessary.
    210   *
    211   * @return the property change handler.
    212   */
    213  private ActionEnablePropertyChangeHandler getPropertyChangeHandler()
    214  {
    215    if (this.propertyChangeHandler == null)
    216    {
    217        this.propertyChangeHandler = new ActionEnablePropertyChangeHandler();
    218    }
    219    return this.propertyChangeHandler;
    220  }
    221
    222  /**
    223   * Enables and disables this button and if an action is assigned to this button the
    224   * propertychange is forwarded to the assigned action.
    225   *
    226   * @param b the new enable-state of this button
    227   */
    228  public void setEnabled(final boolean b)
    229  {
    230    super.setEnabled(b);
    231    if (getAction() != null)
    232    {
    233      getAction().setEnabled(b);
    234    }
    235  }
    236
    237  /**
    238   * Assigns the given action to this button. The properties of the action will be assigned to
    239   * the button. If an previous action was set, the old action is unregistered.
    240   * <p>
    241   * <ul>
    242   * <li>NAME - specifies the button text
    243   * <li>SMALL_ICON - specifies the buttons icon
    244   * <li>MNEMONIC_KEY - specifies the buttons mnemonic key
    245   * <li>ACCELERATOR_KEY - specifies the buttons accelerator
    246   * </ul>
    247   *
    248   * @param newAction the new action
    249   */
    250  public void setAction(final Action newAction)
    251  {
    252    final Action oldAction = getAction();
    253    if (oldAction != null)
    254    {
    255      removeActionListener(oldAction);
    256      oldAction.removePropertyChangeListener(getPropertyChangeHandler());
    257
    258      final Object o = oldAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
    259      if (o instanceof KeyStroke && o != null)
    260      {
    261        final KeyStroke k = (KeyStroke) o;
    262        unregisterKeyboardAction(k);
    263      }
    264    }
    265    this.action = newAction;
    266    if (this.action != null)
    267    {
    268      addActionListener(newAction);
    269      newAction.addPropertyChangeListener(getPropertyChangeHandler());
    270
    271      setText((String) (newAction.getValue(Action.NAME)));
    272      setToolTipText((String) (newAction.getValue(Action.SHORT_DESCRIPTION)));
    273      setIcon((Icon) newAction.getValue(Action.SMALL_ICON));
    274      setEnabled(this.action.isEnabled());
    275
    276      Object o = newAction.getValue(ActionDowngrade.MNEMONIC_KEY);
    277      if (o != null)
    278      {
    279        if (o instanceof Character)
    280        {
    281          final Character c = (Character) o;
    282          setMnemonic(c.charValue());
    283        }
    284        else if (o instanceof Integer)
    285        {
    286          final Integer c = (Integer) o;
    287          setMnemonic(c.intValue());
    288        }
    289      }
    290      o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
    291      if (o instanceof KeyStroke && o != null)
    292      {
    293        final KeyStroke k = (KeyStroke) o;
    294        registerKeyboardAction(newAction, k, WHEN_IN_FOCUSED_WINDOW);
    295      }
    296    }
    297  }
    298}
    299