1 7 8 package org.jdesktop.swing.actions; 9 10 import java.awt.event.ItemListener ; 11 12 import java.beans.PropertyChangeListener ; 13 14 import javax.swing.AbstractAction ; 15 import javax.swing.Action ; 16 import javax.swing.Icon ; 17 import javax.swing.KeyStroke ; 18 import javax.swing.JFrame ; 19 import javax.swing.JOptionPane ; 20 21 25 public abstract class AbstractActionExt extends AbstractAction 26 implements ItemListener { 27 28 31 public static final String LARGE_ICON = "__LargeIcon__"; 32 33 36 public static final String GROUP = "__Group__"; 37 38 41 public static final String IS_STATE = "__State__"; 42 43 46 private boolean selected = false; 47 48 51 public AbstractActionExt(AbstractActionExt action) { 52 Object [] keys = action.getKeys(); 53 for (int i = 0; i < keys.length; i++) { 54 putValue((String )keys[i], action.getValue((String )keys[i])); 55 } 56 this.selected = action.selected; 57 this.enabled = action.enabled; 58 59 PropertyChangeListener [] listeners = action.getPropertyChangeListeners(); 61 for (int i = 0; i < listeners.length; i++) { 62 addPropertyChangeListener(listeners[i]); 63 } 64 } 65 66 public AbstractActionExt(String name) { 67 super(name); 68 } 69 70 public AbstractActionExt(String name, Icon icon) { 71 super(name, icon); 72 } 73 74 80 public AbstractActionExt(String name, String command) { 81 this(name); 82 setActionCommand(command); 83 } 84 85 90 public AbstractActionExt(String name, String command, Icon icon) { 91 super(name, icon); 92 setActionCommand(command); 93 } 94 99 public String getShortDescription() { 100 return (String )getValue(Action.SHORT_DESCRIPTION); 101 } 102 103 114 public void setShortDescription(String desc) { 115 putValue(Action.SHORT_DESCRIPTION, desc); 116 if (desc != null && getLongDescription() == null) { 117 setLongDescription(desc); 118 } 119 } 120 121 126 public String getLongDescription() { 127 return (String )getValue(Action.LONG_DESCRIPTION); 128 } 129 130 141 public void setLongDescription(String desc) { 142 putValue(Action.LONG_DESCRIPTION, desc); 143 if (desc != null && getShortDescription() == null) { 144 setLongDescription(desc); 145 } 146 } 147 148 153 public Icon getSmallIcon() { 154 return (Icon )getValue(SMALL_ICON); 155 } 156 157 167 public void setSmallIcon(Icon icon) { 168 putValue(SMALL_ICON, icon); 169 } 170 171 176 public Icon getLargeIcon() { 177 return (Icon )getValue(LARGE_ICON); 178 } 179 180 190 public void setLargeIcon(Icon icon) { 191 putValue(LARGE_ICON, icon); 192 } 193 194 204 public void setName(String name) { 205 putValue(Action.NAME, name); 206 } 207 208 213 public String getName() { 214 return (String )getValue(Action.NAME); 215 } 216 217 public void setMnemonic(String mnemonic) { 218 if (mnemonic != null && mnemonic.length() > 0) { 219 putValue(Action.MNEMONIC_KEY, new Integer (mnemonic.charAt(0))); 220 } 221 } 222 223 238 public void setMnemonic(int mnemonic) { 239 putValue(Action.MNEMONIC_KEY, new Integer (mnemonic)); 240 } 241 242 247 public int getMnemonic() { 248 Integer value = (Integer )getValue(Action.MNEMONIC_KEY); 249 if (value != null) { 250 return value.intValue(); 251 } 252 return '\0'; 253 } 254 255 266 public void setActionCommand(Object key) { 267 putValue(Action.ACTION_COMMAND_KEY, key); 268 } 269 270 275 public Object getActionCommand() { 276 return getValue(Action.ACTION_COMMAND_KEY); 277 } 278 279 285 public KeyStroke getAccelerator() { 286 return (KeyStroke )getValue(Action.ACCELERATOR_KEY); 287 } 288 289 300 public void setAccelerator(KeyStroke key) { 301 putValue(Action.ACCELERATOR_KEY, key); 302 } 303 304 308 public void setGroup(Object group) { 309 putValue(GROUP, group); 310 } 311 312 public Object getGroup() { 313 return getValue(GROUP); 314 } 315 316 322 public void dispose() { 323 PropertyChangeListener [] listeners = getPropertyChangeListeners(); 324 for (int i = 0; i < listeners.length; i++) { 325 removePropertyChangeListener(listeners[i]); 326 } 327 } 328 329 331 338 public boolean isStateAction() { 339 Boolean state = (Boolean )getValue(IS_STATE); 340 if (state != null) { 341 return state.booleanValue(); 342 } 343 return false; 344 } 345 346 349 public void setStateAction() { 350 setStateAction(true); 351 } 352 353 358 public void setStateAction(boolean state) { 359 putValue(IS_STATE, Boolean.valueOf(state)); 360 } 361 362 365 public boolean isSelected() { 366 return selected; 367 } 368 369 373 public synchronized void setSelected(boolean newValue) { 374 boolean oldValue = this.selected; 375 if (oldValue != newValue) { 376 this.selected = newValue; 377 firePropertyChange("selected", Boolean.valueOf(oldValue), 378 Boolean.valueOf(newValue)); 379 } 380 } 381 382 public String toString() { 383 StringBuffer buffer = new StringBuffer ("["); 384 buffer.append(this.getClass().toString()); 387 buffer.append(":"); 388 try { 389 Object [] keys = getKeys(); 390 for (int i = 0; i < keys.length; i++) { 391 buffer.append(keys[i]); 392 buffer.append('='); 393 buffer.append(getValue( (String ) keys[i]).toString()); 394 if (i < keys.length - 1) { 395 buffer.append(','); 396 } 397 } 398 buffer.append(']'); 399 } 400 catch (Exception ex) { 402 } 403 return buffer.toString(); 404 } 405 } 406 | Popular Tags |