1 11 package org.eclipse.ui.commands; 12 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.core.commands.IHandlerAttributes; 18 import org.eclipse.jface.action.IAction; 19 import org.eclipse.jface.util.IPropertyChangeListener; 20 import org.eclipse.jface.util.PropertyChangeEvent; 21 import org.eclipse.swt.widgets.Event; 22 import org.eclipse.ui.actions.RetargetAction; 23 24 32 public final class ActionHandler extends AbstractHandler { 33 34 39 private final static String ATTRIBUTE_CHECKED = "checked"; 41 44 private final static String ATTRIBUTE_ENABLED = "enabled"; 46 52 private final static String ATTRIBUTE_HANDLED = IHandlerAttributes.ATTRIBUTE_HANDLED; 53 54 58 private final static String ATTRIBUTE_ID = "id"; 60 65 private final static String ATTRIBUTE_STYLE = "style"; 67 70 private final IAction action; 71 72 80 private Map attributeValuesByName; 81 82 87 private IPropertyChangeListener propertyChangeListener; 88 89 96 public ActionHandler(IAction action) { 97 if (action == null) { 98 throw new NullPointerException (); 99 } 100 101 this.action = action; 102 } 103 104 108 public void addHandlerListener(IHandlerListener handlerListener) { 109 if (!hasListeners()) { 110 attachListener(); 111 } 112 113 super.addHandlerListener(handlerListener); 114 } 115 116 122 private final void attachListener() { 123 if (propertyChangeListener == null) { 124 attributeValuesByName = getAttributeValuesByNameFromAction(); 125 126 propertyChangeListener = new IPropertyChangeListener() { 127 public void propertyChange( 128 PropertyChangeEvent propertyChangeEvent) { 129 String property = propertyChangeEvent.getProperty(); 130 if (IAction.ENABLED.equals(property) 131 || IAction.CHECKED.equals(property) 132 || IHandlerAttributes.ATTRIBUTE_HANDLED 133 .equals(property)) { 134 135 Map previousAttributeValuesByName = attributeValuesByName; 136 attributeValuesByName = getAttributeValuesByNameFromAction(); 137 if (!attributeValuesByName 138 .equals(previousAttributeValuesByName)) { 139 fireHandlerChanged(new HandlerEvent( 140 ActionHandler.this, true, 141 previousAttributeValuesByName)); 142 } 143 } 144 } 145 }; 146 } 147 148 this.action.addPropertyChangeListener(propertyChangeListener); 149 } 150 151 158 private final void detachListener() { 159 this.action.removePropertyChangeListener(propertyChangeListener); 160 propertyChangeListener = null; 161 attributeValuesByName = null; 162 } 163 164 169 public void dispose() { 170 if (hasListeners()) { 171 action.removePropertyChangeListener(propertyChangeListener); 172 } 173 } 174 175 176 179 public Object execute(Map parameterValuesByName) throws ExecutionException { 180 if ((action.getStyle() == IAction.AS_CHECK_BOX) 181 || (action.getStyle() == IAction.AS_RADIO_BUTTON)) { 182 action.setChecked(!action.isChecked()); 183 } 184 try { 185 action.runWithEvent(new Event()); 186 } catch (Exception e) { 187 throw new ExecutionException( 188 "While executing the action, an exception occurred", e); } 190 return null; 191 } 192 193 199 public IAction getAction() { 200 return action; 201 } 202 203 206 public Map getAttributeValuesByName() { 207 if (attributeValuesByName == null) { 208 return getAttributeValuesByNameFromAction(); 209 } 210 211 return attributeValuesByName; 212 } 213 214 222 private Map getAttributeValuesByNameFromAction() { 223 Map map = new HashMap (); 224 map.put(ATTRIBUTE_CHECKED, action.isChecked() ? Boolean.TRUE 225 : Boolean.FALSE); 226 map.put(ATTRIBUTE_ENABLED, action.isEnabled() ? Boolean.TRUE 227 : Boolean.FALSE); 228 boolean handled = true; 229 if (action instanceof RetargetAction) { 230 RetargetAction retargetAction = (RetargetAction) action; 231 handled = retargetAction.getActionHandler() != null; 232 } 233 map.put(ATTRIBUTE_HANDLED, handled ? Boolean.TRUE : Boolean.FALSE); 234 map.put(ATTRIBUTE_ID, action.getId()); 235 map.put(ATTRIBUTE_STYLE, new Integer (action.getStyle())); 236 return Collections.unmodifiableMap(map); 237 } 238 239 243 public void removeHandlerListener(IHandlerListener handlerListener) { 244 super.removeHandlerListener(handlerListener); 245 246 if (!hasListeners()) { 247 detachListener(); 248 } 249 } 250 251 254 public final String toString() { 255 final StringBuffer buffer = new StringBuffer (); 256 257 buffer.append("ActionHandler(action="); buffer.append(action); 259 buffer.append(')'); 260 261 return buffer.toString(); 262 } 263 } 264 | Popular Tags |