1 26 package org.objectweb.util.explorer.swing.menu; 27 28 import java.awt.event.ActionEvent ; 29 30 import javax.swing.AbstractAction ; 31 import javax.swing.Icon ; 32 import javax.swing.JOptionPane ; 33 import javax.swing.KeyStroke ; 34 35 import org.objectweb.util.explorer.api.MenuItem; 36 import org.objectweb.util.explorer.api.MenuItemTreeView; 37 import org.objectweb.util.explorer.core.menu.api.AcceleratorDescription; 38 import org.objectweb.util.explorer.core.menu.api.ItemDescription; 39 import org.objectweb.util.explorer.core.menu.api.MenuItemTreeViewConfiguration; 40 import org.objectweb.util.explorer.core.menu.api.MnemonicDescription; 41 import org.objectweb.util.explorer.swing.icon.EmptyIconProvider; 42 import org.objectweb.util.trace.TraceSystem; 43 44 52 public class GenericAction 53 extends AbstractAction 54 { 55 56 62 protected MenuItem delegate_; 63 protected MenuItemTreeView treeView_; 64 protected boolean isUserIcon_ = true; 65 66 72 75 public GenericAction(ItemDescription itemDesc, MenuItem action, MenuItemTreeView treeView, Icon icon, boolean enabled){ 76 super(itemDesc.getLabel(),icon); 77 78 delegate_ = action; 79 treeView_ = treeView; 80 81 if(icon==null){ 82 putValue(SMALL_ICON, new EmptyIconProvider().newIcon(null)); 83 isUserIcon_ = false; 84 } 85 86 putValue(SHORT_DESCRIPTION, itemDesc.getLabel()); 88 MnemonicDescription md = itemDesc.getMnemonicDescription(); 89 if(md!=null && !md.isEmpty()){ 90 putValue(MNEMONIC_KEY,new Integer (Character.toUpperCase(md.getMnemonicCharacter()))); 91 } 92 KeyStroke key = getKeyStroke(itemDesc.getAcceleratorDescription()); 93 if(key!=null){ 94 putValue(ACCELERATOR_KEY,key); 95 } 96 setEnabled(enabled); 97 } 98 99 105 110 protected KeyStroke getKeyStroke(AcceleratorDescription acceleratorDesc){ 111 if(acceleratorDesc!=null && !acceleratorDesc.isEmpty()){ 112 int modifier = 0; 113 modifier = (acceleratorDesc.getAltKey()?java.awt.Event.ALT_MASK:0) 114 |(acceleratorDesc.getCtrlKey()?java.awt.Event.CTRL_MASK:0) 115 |(acceleratorDesc.getShiftKey()?java.awt.Event.SHIFT_MASK:0) 116 |(acceleratorDesc.getMetaKey()?java.awt.Event.META_MASK:0); 117 if(modifier==0) 118 return KeyStroke.getKeyStroke(Character.toUpperCase(acceleratorDesc.getAcceleratorCharacter())); 119 else 120 return KeyStroke.getKeyStroke(Character.toUpperCase(acceleratorDesc.getAcceleratorCharacter()), modifier); 121 } 122 return null; 123 } 124 125 131 134 public void actionPerformed(ActionEvent actionEvent) { 135 try{ 136 ((MenuItemTreeViewConfiguration)treeView_).setEvent(actionEvent); 137 delegate_.actionPerformed(treeView_); 138 } catch (Exception e) { 139 TraceSystem.get("explorer").warn(getClass().getName() + " exception: " + e.getMessage()); 140 JOptionPane.showMessageDialog(null, e.getClass().getName() + ":\n" + e.getMessage(), "Exception (" + actionEvent.getActionCommand() + ")", JOptionPane.ERROR_MESSAGE); 141 } 142 treeView_.getTree().refreshAll(); 143 } 144 145 public boolean isUserIcon(){ 146 return isUserIcon_; 147 } 148 } 149 | Popular Tags |