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