Source for org.jfree.ui.action.ActionButton

   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-2004, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: ActionButton.java,v 1.7 2007/11/02 17:50:36 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.JButton;
  53: import javax.swing.KeyStroke;
  54: 
  55: import org.jfree.util.Log;
  56: 
  57: /**
  58:  * The ActionButton is used to connect an Action and its properties to a Button. This functionality
  59:  * is already implemented in JDK 1.3 but needed for JDK 1.2.2 compatibility.
  60:  *
  61:  * @author Thomas Morgner
  62:  */
  63: public class ActionButton extends JButton {
  64: 
  65:     /**
  66:      * The action.
  67:      */
  68:     private Action action;
  69: 
  70:     /**
  71:      * The property change handler.
  72:      */
  73:     private ActionEnablePropertyChangeHandler propertyChangeHandler;
  74: 
  75:     /**
  76:      * Helperclass to handle the property change event raised by the action. Changed properties in
  77:      * the action will affect the button.
  78:      */
  79:     private class ActionEnablePropertyChangeHandler implements PropertyChangeListener {
  80: 
  81:         /**
  82:          * Default constructor.
  83:          */
  84:         public ActionEnablePropertyChangeHandler() {
  85:         }
  86: 
  87:         /**
  88:          * Receives notification of a property change event.
  89:          *
  90:          * @param event the property change event.
  91:          */
  92:         public void propertyChange(final PropertyChangeEvent event) {
  93:             try {
  94:                 if (event.getPropertyName().equals("enabled")) {
  95:                     setEnabled(getAction().isEnabled());
  96:                 }
  97:                 else if (event.getPropertyName().equals(Action.SMALL_ICON)) {
  98:                     setIcon((Icon) getAction().getValue(Action.SMALL_ICON));
  99:                 }
 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: }