1 11 package org.eclipse.ui.internal.commands; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.commands.Command; 20 import org.eclipse.core.commands.ExecutionEvent; 21 import org.eclipse.core.commands.ParameterizedCommand; 22 import org.eclipse.jface.bindings.BindingManager; 23 import org.eclipse.jface.bindings.TriggerSequence; 24 import org.eclipse.ui.commands.ExecutionException; 25 import org.eclipse.ui.commands.ICommand; 26 import org.eclipse.ui.commands.ICommandListener; 27 import org.eclipse.ui.commands.NotDefinedException; 28 import org.eclipse.ui.commands.NotHandledException; 29 import org.eclipse.ui.internal.keys.KeySequenceBinding; 30 import org.eclipse.ui.keys.KeySequence; 31 32 38 final class CommandWrapper implements ICommand { 39 40 43 private final BindingManager bindingManager; 44 45 48 private final Command command; 49 50 54 private ParameterizedCommand parameterizedCommand; 55 56 65 CommandWrapper(final Command command, final BindingManager bindingManager) { 66 if (command == null) { 67 throw new NullPointerException ( 68 "The wrapped command cannot be <code>null</code>."); } 70 71 if (bindingManager == null) { 72 throw new NullPointerException ( 73 "A binding manager is required to wrap a command"); } 75 76 this.command = command; 77 this.bindingManager = bindingManager; 78 } 79 80 85 86 public final void addCommandListener(final ICommandListener commandListener) { 87 command.addCommandListener(new CommandListenerWrapper(commandListener, 88 bindingManager)); 89 } 90 91 96 public final Object execute(Map parameterValuesByName) 97 throws ExecutionException, NotHandledException { 98 try { 99 return command.execute(new ExecutionEvent( 100 (parameterValuesByName == null) ? Collections.EMPTY_MAP 101 : parameterValuesByName, null, null)); 102 } catch (final org.eclipse.core.commands.ExecutionException e) { 103 throw new ExecutionException(e); 104 } catch (final org.eclipse.core.commands.NotHandledException e) { 105 throw new NotHandledException(e); 106 } 107 } 108 109 114 public final Map getAttributeValuesByName() { 115 final Map attributeValues = new HashMap (); 116 attributeValues.put(ILegacyAttributeNames.ENABLED, command.isEnabled() ? Boolean.TRUE : Boolean.FALSE); 118 attributeValues.put(ILegacyAttributeNames.HANDLED, command.isHandled() ? Boolean.TRUE : Boolean.FALSE); 119 return attributeValues; 120 } 121 122 127 public final String getCategoryId() throws NotDefinedException { 128 try { 129 return command.getCategory().getId(); 130 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 131 throw new NotDefinedException(e); 132 } 133 } 134 135 140 public final String getDescription() throws NotDefinedException { 141 try { 142 return command.getDescription(); 143 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 144 throw new NotDefinedException(e); 145 } 146 } 147 148 153 public final String getId() { 154 return command.getId(); 155 } 156 157 162 public final List getKeySequenceBindings() { 163 final List legacyBindings = new ArrayList (); 164 if (parameterizedCommand == null) { 165 parameterizedCommand = new ParameterizedCommand(command, null); 166 } 167 final TriggerSequence[] activeBindings = bindingManager 168 .getActiveBindingsFor(parameterizedCommand); 169 final int activeBindingsCount = activeBindings.length; 170 for (int i = 0; i < activeBindingsCount; i++) { 171 final TriggerSequence triggerSequence = activeBindings[i]; 172 if (triggerSequence instanceof org.eclipse.jface.bindings.keys.KeySequence) { 173 legacyBindings 174 .add(new KeySequenceBinding( 175 KeySequence 176 .getInstance((org.eclipse.jface.bindings.keys.KeySequence) triggerSequence), 177 0)); 178 } 179 } 180 181 return legacyBindings; 182 } 183 184 189 public final String getName() throws NotDefinedException { 190 try { 191 return command.getName(); 192 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 193 throw new NotDefinedException(e); 194 } 195 } 196 197 202 public final boolean isDefined() { 203 return command.isDefined(); 204 } 205 206 211 public final boolean isHandled() { 212 return command.isHandled(); 213 } 214 215 220 public final void removeCommandListener( 221 final ICommandListener commandListener) { 222 command.removeCommandListener(new CommandListenerWrapper( 223 commandListener, bindingManager)); 224 } 225 226 231 public final int compareTo(final Object o) { 232 return command.compareTo(o); 233 } 234 235 } 236 | Popular Tags |