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.JButton ; 54 import javax.swing.KeyStroke ; 55 56 import org.jfree.util.Log; 57 58 64 public class ActionButton extends JButton { 65 66 69 private Action action; 70 71 74 private ActionEnablePropertyChangeHandler propertyChangeHandler; 75 76 80 private class ActionEnablePropertyChangeHandler implements PropertyChangeListener { 81 82 85 public ActionEnablePropertyChangeHandler() { 86 } 87 88 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(oldVal); 115 } 116 final Object o = ac.getValue(ActionDowngrade.ACCELERATOR_KEY); 117 if (o instanceof KeyStroke ) { 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 final Object o = ac.getValue(ActionDowngrade.MNEMONIC_KEY); 124 if (o != null) { 125 if (o instanceof Character ) { 126 final Character c = (Character ) o; 127 setMnemonic(c.charValue()); 128 } 129 else if (o instanceof Integer ) { 130 final Integer c = (Integer ) o; 131 setMnemonic(c.intValue()); 132 } 133 } 134 } 135 } 136 catch (Exception e) { 137 Log.warn("Error on PropertyChange in ActionButton: ", e); 138 } 139 } 140 } 141 142 145 public ActionButton() { 146 super(); 147 } 148 149 154 public ActionButton(final String text) { 155 super(text); 156 } 157 158 164 public ActionButton(final String text, final Icon icon) { 165 super(text, icon); 166 } 167 168 169 174 public ActionButton(final Icon icon) { 175 super(icon); 176 } 177 178 183 public ActionButton(final Action action) { 184 setAction(action); 185 } 186 187 192 public Action getAction() { 193 return this.action; 194 } 195 196 197 203 private ActionEnablePropertyChangeHandler getPropertyChangeHandler() { 204 if (this.propertyChangeHandler == null) { 205 this.propertyChangeHandler = new ActionEnablePropertyChangeHandler(); 206 } 207 return this.propertyChangeHandler; 208 } 209 210 216 public void setEnabled(final boolean b) { 217 super.setEnabled(b); 218 if (getAction() != null) { 219 getAction().setEnabled(b); 220 } 221 } 222 223 236 public void setAction(final Action newAction) { 237 final Action oldAction = getAction(); 238 if (oldAction != null) { 239 removeActionListener(oldAction); 240 oldAction.removePropertyChangeListener(getPropertyChangeHandler()); 241 242 final Object o = oldAction.getValue(ActionDowngrade.ACCELERATOR_KEY); 243 if (o instanceof KeyStroke ) { 244 final KeyStroke k = (KeyStroke ) o; 245 unregisterKeyboardAction(k); 246 } 247 } 248 this.action = newAction; 249 if (this.action != null) { 250 addActionListener(newAction); 251 newAction.addPropertyChangeListener(getPropertyChangeHandler()); 252 253 setText((String ) (newAction.getValue(Action.NAME))); 254 setToolTipText((String ) (newAction.getValue(Action.SHORT_DESCRIPTION))); 255 setIcon((Icon ) newAction.getValue(Action.SMALL_ICON)); 256 setEnabled(this.action.isEnabled()); 257 258 Object o = newAction.getValue(ActionDowngrade.MNEMONIC_KEY); 259 if (o != null) { 260 if (o instanceof Character ) { 261 final Character c = (Character ) o; 262 setMnemonic(c.charValue()); 263 } 264 else if (o instanceof Integer ) { 265 final Integer c = (Integer ) o; 266 setMnemonic(c.intValue()); 267 } 268 } 269 o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY); 270 if (o instanceof KeyStroke ) { 271 final KeyStroke k = (KeyStroke ) o; 272 registerKeyboardAction(newAction, k, WHEN_IN_FOCUSED_WINDOW); 273 } 274 } 275 } 276 } 277 278 | Popular Tags |