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.3 2005/10/18 13:22:11 mungady 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; 050 051import javax.swing.Action; 052import javax.swing.Icon; 053import javax.swing.JRadioButton; 054import javax.swing.KeyStroke; 055 056import org.jfree.util.Log; 057 058/** 059 * The ActionRadioButton is used to connect an Action and its properties to a JRadioButton. 060 * This functionality is already implemented in JDK 1.3 but needed for JDK 1.2.2 compatibility. 061 * 062 * @author Thomas Morgner 063 */ 064public class ActionRadioButton extends JRadioButton 065{ 066 /** The action. */ 067 private Action action; 068 069 /** The property change handler. */ 070 private ActionEnablePropertyChangeHandler propertyChangeHandler; 071 072 /** 073 * Helperclass to handle the property change event raised by the action. Changed properties in 074 * the action will affect the button. 075 */ 076 private class ActionEnablePropertyChangeHandler implements PropertyChangeListener 077 { 078 /** 079 * Receives notification of a property change event. 080 * 081 * @param event the property change event. 082 */ 083 public void propertyChange(final PropertyChangeEvent event) 084 { 085 try 086 { 087 if (event.getPropertyName().equals("enabled")) 088 { 089 setEnabled(getAction().isEnabled()); 090 } 091 else if (event.getPropertyName().equals(Action.SMALL_ICON)) 092 { 093 setIcon((Icon) getAction().getValue(Action.SMALL_ICON)); 094 } 095 else if (event.getPropertyName().equals(Action.NAME)) 096 { 097 setText((String) getAction().getValue 098 (Action.NAME)); 099 } 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} 300