Source for org.jfree.ui.action.ActionRadioButton

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * -----------------
  28:  * ActionButton.java
  29:  * -----------------
  30:  * (C)opyright 2002, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: ActionRadioButton.java,v 1.4 2007/11/02 17:50:37 taqua Exp $
  36:  *
  37:  * ChangeLog
  38:  * ---------
  39:  * 30-Aug-2002 : Initial version
  40:  * 01-Sep-2002 : Documentation
  41:  * 10-Dec-2002 : Minor Javadoc updates (DG);
  42:  * 07-Jun-2004 : Corrected source headers (DG);
  43:  *
  44:  */
  45: 
  46: package org.jfree.ui.action;
  47: 
  48: import java.beans.PropertyChangeEvent;
  49: import java.beans.PropertyChangeListener;
  50: import javax.swing.Action;
  51: import javax.swing.Icon;
  52: import javax.swing.JRadioButton;
  53: import javax.swing.KeyStroke;
  54: 
  55: import org.jfree.util.Log;
  56: 
  57: /**
  58:  * The ActionRadioButton is used to connect an Action and its properties to a JRadioButton.
  59:  * This functionality is already implemented in JDK 1.3 but needed for JDK 1.2.2 compatibility.
  60:  *
  61:  * @author Thomas Morgner
  62:  */
  63: public class ActionRadioButton extends JRadioButton
  64: {
  65:   /** The action. */
  66:   private Action action;
  67: 
  68:   /** The property change handler. */
  69:   private ActionEnablePropertyChangeHandler propertyChangeHandler;
  70: 
  71:   /**
  72:    * Helperclass to handle the property change event raised by the action. Changed properties in
  73:    * the action will affect the button.
  74:    */
  75:   private class ActionEnablePropertyChangeHandler implements PropertyChangeListener
  76:   {
  77:     /**
  78:      * Receives notification of a property change event.
  79:      *
  80:      * @param event  the property change event.
  81:      */
  82:     public void propertyChange(final PropertyChangeEvent event)
  83:     {
  84:       try
  85:       {
  86:         if (event.getPropertyName().equals("enabled"))
  87:         {
  88:           setEnabled(getAction().isEnabled());
  89:         }
  90:         else if (event.getPropertyName().equals(Action.SMALL_ICON))
  91:         {
  92:           setIcon((Icon) getAction().getValue(Action.SMALL_ICON));
  93:         }
  94:         else if (event.getPropertyName().equals(Action.NAME))
  95:         {
  96:           setText((String) getAction().getValue
  97:               (Action.NAME));
  98:         }
  99:         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: }