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.3 2005/10/18 13:22:11 mungady 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: 
  51: import javax.swing.Action;
  52: import javax.swing.Icon;
  53: import javax.swing.JRadioButton;
  54: import javax.swing.KeyStroke;
  55: 
  56: import org.jfree.util.Log;
  57: 
  58: /**
  59:  * The ActionRadioButton is used to connect an Action and its properties to a JRadioButton.
  60:  * This functionality is already implemented in JDK 1.3 but needed for JDK 1.2.2 compatibility.
  61:  *
  62:  * @author Thomas Morgner
  63:  */
  64: public class ActionRadioButton extends JRadioButton
  65: {
  66:   /** The action. */
  67:   private Action action;
  68: 
  69:   /** The property change handler. */
  70:   private ActionEnablePropertyChangeHandler propertyChangeHandler;
  71: 
  72:   /**
  73:    * Helperclass to handle the property change event raised by the action. Changed properties in
  74:    * the action will affect the button.
  75:    */
  76:   private class ActionEnablePropertyChangeHandler implements PropertyChangeListener
  77:   {
  78:     /**
  79:      * Receives notification of a property change event.
  80:      *
  81:      * @param event  the property change event.
  82:      */
  83:     public void propertyChange(final PropertyChangeEvent event)
  84:     {
  85:       try
  86:       {
  87:         if (event.getPropertyName().equals("enabled"))
  88:         {
  89:           setEnabled(getAction().isEnabled());
  90:         }
  91:         else if (event.getPropertyName().equals(Action.SMALL_ICON))
  92:         {
  93:           setIcon((Icon) getAction().getValue(Action.SMALL_ICON));
  94:         }
  95:         else if (event.getPropertyName().equals(Action.NAME))
  96:         {
  97:           setText((String) getAction().getValue
  98:               (Action.NAME));
  99:         }
 100:         else if (event.getPropertyName().equals(Action.SHORT_DESCRIPTION))
 101:         {
 102:           ActionRadioButton.this.setToolTipText((String)
 103:               getAction().getValue(Action.SHORT_DESCRIPTION));
 104:         }
 105: 
 106:         final Action ac = getAction();
 107:         if (event.getPropertyName().equals(ActionDowngrade.ACCELERATOR_KEY))
 108:         {
 109:           final KeyStroke oldVal = (KeyStroke) event.getOldValue();
 110:           if (oldVal != null)
 111:           {
 112:             unregisterKeyboardAction
 113:                 (oldVal);
 114:           }
 115:           final Object o = ac.getValue(ActionDowngrade.ACCELERATOR_KEY);
 116:           if (o instanceof KeyStroke && o != null)
 117:           {
 118:             final KeyStroke k = (KeyStroke) o;
 119:             registerKeyboardAction(ac, k, WHEN_IN_FOCUSED_WINDOW);
 120:           }
 121:         }
 122:         else if (event.getPropertyName().equals(ActionDowngrade.MNEMONIC_KEY))
 123:         {
 124:           final Object o = ac.getValue(ActionDowngrade.MNEMONIC_KEY);
 125:           if (o != null)
 126:           {
 127:             if (o instanceof Character)
 128:             {
 129:               final Character c = (Character) o;
 130:               setMnemonic(c.charValue());
 131:             }
 132:             else if (o instanceof Integer)
 133:             {
 134:               final Integer c = (Integer) o;
 135:               setMnemonic(c.intValue());
 136:             }
 137:           }
 138:         }
 139:       }
 140:       catch (Exception e)
 141:       {
 142:         Log.warn("Error on PropertyChange in ActionButton: ", e);
 143:       }
 144:     }
 145:   }
 146: 
 147:   /**
 148:    * Creates a Button without any text and without an assigned Action.
 149:    */
 150:   public ActionRadioButton()
 151:   {
 152:     super();
 153:   }
 154: 
 155:   /**
 156:    * Creates a Button and set the given text as label.
 157:    *
 158:    * @param text  the label for the new button.
 159:    */
 160:   public ActionRadioButton(final String text)
 161:   {
 162:     super(text);
 163:   }
 164: 
 165:   /**
 166:    * Creates an ActionButton and sets the given text and icon on the button.
 167:    *
 168:    * @param text  the label for the new button.
 169:    * @param icon  the icon for the button.
 170:    */
 171:   public ActionRadioButton(final String text, final Icon icon)
 172:   {
 173:     super(text, icon);
 174:   }
 175: 
 176: 
 177:   /**
 178:    * Creates an ActionButton and sets the given icon on the button.
 179:    *
 180:    * @param icon  the icon for the button.
 181:    */
 182:   public ActionRadioButton(final Icon icon)
 183:   {
 184:     super(icon);
 185:   }
 186: 
 187:   /**
 188:    * Nreates an ActionButton and assigns the given action with the button.
 189:    *
 190:    * @param action  the action.
 191:    */
 192:   public ActionRadioButton(final Action action)
 193:   {
 194:     setAction(action);
 195:   }
 196: 
 197:   /**
 198:    * Returns the assigned action or null if no action has been assigned.
 199:    *
 200:    * @return the action (possibly null).
 201:    */
 202:   public Action getAction()
 203:   {
 204:     return this.action;
 205:   }
 206: 
 207: 
 208:   /**
 209:    * Returns and initializes the PropertyChangehandler for this ActionButton.
 210:    * The PropertyChangeHandler monitors the action and updates the button if necessary.
 211:    *
 212:    * @return the property change handler.
 213:    */
 214:   private ActionEnablePropertyChangeHandler getPropertyChangeHandler()
 215:   {
 216:     if (this.propertyChangeHandler == null)
 217:     {
 218:         this.propertyChangeHandler = new ActionEnablePropertyChangeHandler();
 219:     }
 220:     return this.propertyChangeHandler;
 221:   }
 222: 
 223:   /**
 224:    * Enables and disables this button and if an action is assigned to this button the
 225:    * propertychange is forwarded to the assigned action.
 226:    *
 227:    * @param b the new enable-state of this button
 228:    */
 229:   public void setEnabled(final boolean b)
 230:   {
 231:     super.setEnabled(b);
 232:     if (getAction() != null)
 233:     {
 234:       getAction().setEnabled(b);
 235:     }
 236:   }
 237: 
 238:   /**
 239:    * Assigns the given action to this button. The properties of the action will be assigned to
 240:    * the button. If an previous action was set, the old action is unregistered.
 241:    * <p>
 242:    * <ul>
 243:    * <li>NAME - specifies the button text
 244:    * <li>SMALL_ICON - specifies the buttons icon
 245:    * <li>MNEMONIC_KEY - specifies the buttons mnemonic key
 246:    * <li>ACCELERATOR_KEY - specifies the buttons accelerator
 247:    * </ul>
 248:    *
 249:    * @param newAction the new action
 250:    */
 251:   public void setAction(final Action newAction)
 252:   {
 253:     final Action oldAction = getAction();
 254:     if (oldAction != null)
 255:     {
 256:       removeActionListener(oldAction);
 257:       oldAction.removePropertyChangeListener(getPropertyChangeHandler());
 258: 
 259:       final Object o = oldAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
 260:       if (o instanceof KeyStroke && o != null)
 261:       {
 262:         final KeyStroke k = (KeyStroke) o;
 263:         unregisterKeyboardAction(k);
 264:       }
 265:     }
 266:     this.action = newAction;
 267:     if (this.action != null)
 268:     {
 269:       addActionListener(newAction);
 270:       newAction.addPropertyChangeListener(getPropertyChangeHandler());
 271: 
 272:       setText((String) (newAction.getValue(Action.NAME)));
 273:       setToolTipText((String) (newAction.getValue(Action.SHORT_DESCRIPTION)));
 274:       setIcon((Icon) newAction.getValue(Action.SMALL_ICON));
 275:       setEnabled(this.action.isEnabled());
 276: 
 277:       Object o = newAction.getValue(ActionDowngrade.MNEMONIC_KEY);
 278:       if (o != null)
 279:       {
 280:         if (o instanceof Character)
 281:         {
 282:           final Character c = (Character) o;
 283:           setMnemonic(c.charValue());
 284:         }
 285:         else if (o instanceof Integer)
 286:         {
 287:           final Integer c = (Integer) o;
 288:           setMnemonic(c.intValue());
 289:         }
 290:       }
 291:       o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
 292:       if (o instanceof KeyStroke && o != null)
 293:       {
 294:         final KeyStroke k = (KeyStroke) o;
 295:         registerKeyboardAction(newAction, k, WHEN_IN_FOCUSED_WINDOW);
 296:       }
 297:     }
 298:   }
 299: }