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 CommandLegacyWrapper 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 CommandLegacyWrapper(final Command command, 66 final BindingManager bindingManager) { 67 if (command == null) { 68 throw new NullPointerException ( 69 "The wrapped command cannot be <code>null</code>."); } 71 72 if (bindingManager == null) { 73 throw new NullPointerException ( 74 "A binding manager is required to wrap a command"); } 76 77 this.command = command; 78 this.bindingManager = bindingManager; 79 } 80 81 86 87 public final void addCommandListener(final ICommandListener commandListener) { 88 command.addCommandListener(new LegacyCommandListenerWrapper( 89 commandListener, bindingManager)); 90 } 91 92 97 public final Object execute(Map parameterValuesByName) 98 throws ExecutionException, NotHandledException { 99 try { 100 return command.execute(new ExecutionEvent(command, 101 (parameterValuesByName == null) ? Collections.EMPTY_MAP 102 : parameterValuesByName, null, null)); 103 } catch (final org.eclipse.core.commands.ExecutionException e) { 104 throw new ExecutionException(e); 105 } catch (final org.eclipse.core.commands.NotHandledException e) { 106 throw new NotHandledException(e); 107 } 108 } 109 110 115 public final Map getAttributeValuesByName() { 116 final Map attributeValues = new HashMap (); 117 attributeValues.put(ILegacyAttributeNames.ENABLED, 120 command.isEnabled() ? Boolean.TRUE : Boolean.FALSE); 121 attributeValues.put(ILegacyAttributeNames.HANDLED, 122 command.isHandled() ? Boolean.TRUE : Boolean.FALSE); 123 return attributeValues; 124 } 125 126 131 public final String getCategoryId() throws NotDefinedException { 132 try { 133 return command.getCategory().getId(); 134 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 135 throw new NotDefinedException(e); 136 } 137 } 138 139 144 public final String getDescription() throws NotDefinedException { 145 try { 146 return command.getDescription(); 147 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 148 throw new NotDefinedException(e); 149 } 150 } 151 152 157 public final String getId() { 158 return command.getId(); 159 } 160 161 166 public final List getKeySequenceBindings() { 167 final List legacyBindings = new ArrayList (); 168 if (parameterizedCommand == null) { 169 parameterizedCommand = new ParameterizedCommand(command, null); 170 } 171 final TriggerSequence[] activeBindings = bindingManager 172 .getActiveBindingsFor(parameterizedCommand); 173 final int activeBindingsCount = activeBindings.length; 174 for (int i = 0; i < activeBindingsCount; i++) { 175 final TriggerSequence triggerSequence = activeBindings[i]; 176 if (triggerSequence instanceof org.eclipse.jface.bindings.keys.KeySequence) { 177 legacyBindings 178 .add(new KeySequenceBinding( 179 KeySequence 180 .getInstance((org.eclipse.jface.bindings.keys.KeySequence) triggerSequence), 181 0)); 182 } 183 } 184 185 return legacyBindings; 186 } 187 188 193 public final String getName() throws NotDefinedException { 194 try { 195 return command.getName(); 196 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 197 throw new NotDefinedException(e); 198 } 199 } 200 201 206 public final boolean isDefined() { 207 return command.isDefined(); 208 } 209 210 215 public final boolean isHandled() { 216 return command.isHandled(); 217 } 218 219 224 public final void removeCommandListener( 225 final ICommandListener commandListener) { 226 command.removeCommandListener(new LegacyCommandListenerWrapper( 227 commandListener, bindingManager)); 228 } 229 230 235 public final int compareTo(final Object o) { 236 return command.compareTo(o); 237 } 238 239 } 240 | Popular Tags |