1 44 45 package org.jfree.ui.action; 46 47 import java.beans.PropertyChangeEvent ; 48 import java.beans.PropertyChangeListener ; 49 import java.awt.event.KeyEvent ; 50 import javax.swing.Action ; 51 import javax.swing.Icon ; 52 import javax.swing.JMenuItem ; 53 import javax.swing.KeyStroke ; 54 55 import org.jfree.util.Log; 56 57 66 public class ActionMenuItem extends JMenuItem 67 { 68 69 70 private Action action; 71 72 73 private ActionEnablePropertyChangeHandler propertyChangeHandler; 74 75 79 private class ActionEnablePropertyChangeHandler 80 implements PropertyChangeListener 81 { 82 public ActionEnablePropertyChangeHandler() 83 { 84 } 85 86 91 public void propertyChange(final PropertyChangeEvent event) 92 { 93 try 94 { 95 if (event.getPropertyName().equals("enabled")) 96 { 97 setEnabled(getAction().isEnabled()); 98 } 99 else if (event.getPropertyName().equals(Action.SMALL_ICON)) 100 { 101 setIcon((Icon ) getAction().getValue(Action.SMALL_ICON)); 102 } 103 else if (event.getPropertyName().equals(Action.NAME)) 104 { 105 setText((String ) getAction().getValue(Action.NAME)); 106 } 107 else if (event.getPropertyName().equals(Action.SHORT_DESCRIPTION)) 108 { 109 ActionMenuItem.this.setToolTipText((String ) 110 getAction().getValue(Action.SHORT_DESCRIPTION)); 111 } 112 113 final Action ac = getAction(); 114 if (event.getPropertyName().equals(ActionDowngrade.ACCELERATOR_KEY)) 115 { 116 setAccelerator((KeyStroke ) ac.getValue(ActionDowngrade.ACCELERATOR_KEY)); 117 } 118 else if (event.getPropertyName().equals(ActionDowngrade.MNEMONIC_KEY)) 119 { 120 final Object o = ac.getValue(ActionDowngrade.MNEMONIC_KEY); 121 if (o != null) 122 { 123 if (o instanceof Character ) 124 { 125 final Character c = (Character ) o; 126 setMnemonic(c.charValue()); 127 } 128 else if (o instanceof Integer ) 129 { 130 final Integer c = (Integer ) o; 131 setMnemonic(c.intValue()); 132 } 133 } 134 else 135 { 136 setMnemonic(KeyEvent.VK_UNDEFINED); 137 } 138 } 139 } 140 catch (Exception e) 141 { 142 Log.warn("Error on PropertyChange in ActionButton: ", e); 143 } 144 } 145 } 146 147 148 public ActionMenuItem() 149 { 150 } 152 153 158 public ActionMenuItem(final Icon icon) 159 { 160 super(icon); 161 } 162 163 168 public ActionMenuItem(final String text) 169 { 170 super(text); 171 } 172 173 179 public ActionMenuItem(final String text, final Icon icon) 180 { 181 super(text, icon); 182 } 183 184 190 public ActionMenuItem(final String text, final int i) 191 { 192 super(text, i); 193 } 194 195 200 public ActionMenuItem(final Action action) 201 { 202 setAction(action); 203 } 204 205 210 public Action getAction() 211 { 212 return this.action; 213 } 214 215 222 private ActionEnablePropertyChangeHandler getPropertyChangeHandler() 223 { 224 if (this.propertyChangeHandler == null) 225 { 226 this.propertyChangeHandler = new ActionEnablePropertyChangeHandler(); 227 } 228 return this.propertyChangeHandler; 229 } 230 231 237 public void setEnabled(final boolean b) 238 { 239 super.setEnabled(b); 240 if (getAction() != null) 241 { 242 getAction().setEnabled(b); 243 } 244 } 245 246 257 public void setAction(final Action newAction) 258 { 259 final Action oldAction = getAction(); 260 if (oldAction != null) 261 { 262 removeActionListener(oldAction); 263 oldAction.removePropertyChangeListener(getPropertyChangeHandler()); 264 setAccelerator(null); 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 else 292 { 293 setMnemonic(KeyEvent.VK_UNDEFINED); 294 } 295 296 297 o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY); 298 if (o instanceof KeyStroke ) 299 { 300 setAccelerator((KeyStroke ) o); 301 } 302 } 303 } 304 } 305 | Popular Tags |