1 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 64 public class ActionRadioButton extends JRadioButton 65 { 66 67 private Action action; 68 69 70 private ActionEnablePropertyChangeHandler propertyChangeHandler; 71 72 76 private class ActionEnablePropertyChangeHandler implements PropertyChangeListener 77 { 78 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 150 public ActionRadioButton() 151 { 152 super(); 153 } 154 155 160 public ActionRadioButton(final String text) 161 { 162 super(text); 163 } 164 165 171 public ActionRadioButton(final String text, final Icon icon) 172 { 173 super(text, icon); 174 } 175 176 177 182 public ActionRadioButton(final Icon icon) 183 { 184 super(icon); 185 } 186 187 192 public ActionRadioButton(final Action action) 193 { 194 setAction(action); 195 } 196 197 202 public Action getAction() 203 { 204 return this.action; 205 } 206 207 208 214 private ActionEnablePropertyChangeHandler getPropertyChangeHandler() 215 { 216 if (this.propertyChangeHandler == null) 217 { 218 this.propertyChangeHandler = new ActionEnablePropertyChangeHandler(); 219 } 220 return this.propertyChangeHandler; 221 } 222 223 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 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 301 | Popular Tags |