1 11 12 package org.eclipse.ui.internal.handlers; 13 14 import org.eclipse.core.commands.Command; 15 import org.eclipse.core.commands.CommandEvent; 16 import org.eclipse.core.commands.ExecutionEvent; 17 import org.eclipse.core.commands.ExecutionException; 18 import org.eclipse.core.commands.ICommandListener; 19 import org.eclipse.core.commands.INamedHandleStateIds; 20 import org.eclipse.core.commands.NotHandledException; 21 import org.eclipse.core.commands.ParameterizedCommand; 22 import org.eclipse.core.commands.State; 23 import org.eclipse.core.commands.common.NotDefinedException; 24 import org.eclipse.jface.action.AbstractAction; 25 import org.eclipse.jface.action.IAction; 26 import org.eclipse.jface.action.IMenuCreator; 27 import org.eclipse.jface.bindings.TriggerSequence; 28 import org.eclipse.jface.bindings.keys.KeySequence; 29 import org.eclipse.jface.bindings.keys.KeyStroke; 30 import org.eclipse.jface.commands.RadioState; 31 import org.eclipse.jface.commands.ToggleState; 32 import org.eclipse.jface.menus.IMenuStateIds; 33 import org.eclipse.jface.menus.TextState; 34 import org.eclipse.jface.resource.ImageDescriptor; 35 import org.eclipse.jface.util.Util; 36 import org.eclipse.swt.events.HelpListener; 37 import org.eclipse.swt.widgets.Event; 38 import org.eclipse.ui.commands.ICommandService; 39 import org.eclipse.ui.internal.commands.CommandImageManager; 40 import org.eclipse.ui.internal.commands.ICommandImageService; 41 import org.eclipse.ui.keys.IBindingService; 42 import org.eclipse.ui.services.IServiceLocator; 43 44 65 public final class CommandLegacyActionWrapper extends AbstractAction { 66 67 71 private final class CommandListener implements ICommandListener { 72 public final void commandChanged(final CommandEvent commandEvent) { 73 final Command baseCommand = commandEvent.getCommand(); 74 75 if (commandEvent.isNameChanged()) { 77 String newName = null; 78 if (baseCommand.isDefined()) { 79 try { 80 newName = baseCommand.getName(); 81 } catch (final NotDefinedException e) { 82 } 84 } 85 firePropertyChange(IAction.TEXT, null, newName); 86 } 87 88 if (commandEvent.isDescriptionChanged()) { 90 String newDescription = null; 91 if (baseCommand.isDefined()) { 92 try { 93 newDescription = baseCommand.getDescription(); 94 } catch (final NotDefinedException e) { 95 } 97 } 98 firePropertyChange(IAction.DESCRIPTION, null, newDescription); 99 firePropertyChange(IAction.TOOL_TIP_TEXT, null, newDescription); 100 } 101 102 if (commandEvent.isHandledChanged()) { 104 if (baseCommand.isHandled()) { 105 firePropertyChange(IAction.HANDLED, Boolean.FALSE, 106 Boolean.TRUE); 107 } else { 108 firePropertyChange(IAction.HANDLED, Boolean.TRUE, 109 Boolean.FALSE); 110 } 111 } 112 } 113 114 } 115 116 119 private ParameterizedCommand command; 120 121 125 private final ICommandListener commandListener = new CommandListener(); 126 127 130 private boolean enabled = true; 131 132 135 private String id; 136 137 141 private final IServiceLocator serviceLocator; 142 143 147 private final String style; 148 149 164 public CommandLegacyActionWrapper(final String id, 165 final ParameterizedCommand command, final String style, 166 final IServiceLocator serviceLocator) { 167 if (command == null) { 168 throw new NullPointerException ( 169 "An action proxy can't be created without a command"); } 171 172 if (serviceLocator == null) { 173 throw new NullPointerException ( 174 "An action proxy can't be created without a service locator"); } 176 177 this.command = command; 178 this.id = id; 179 this.style = style; 180 this.serviceLocator = serviceLocator; 181 182 command.getCommand().addCommandListener(commandListener); 184 } 185 186 public final int getAccelerator() { 187 final String commandId = getActionDefinitionId(); 188 final IBindingService bindingService = (IBindingService) serviceLocator 189 .getService(IBindingService.class); 190 final TriggerSequence triggerSequence = bindingService 191 .getBestActiveBindingFor(commandId); 192 if (triggerSequence instanceof KeySequence) { 193 final KeySequence keySequence = (KeySequence) triggerSequence; 194 final KeyStroke[] keyStrokes = keySequence.getKeyStrokes(); 195 if (keyStrokes.length == 1) { 196 final KeyStroke keyStroke = keyStrokes[0]; 197 return keyStroke.getModifierKeys() | keyStroke.getNaturalKey(); 198 } 199 } 200 201 return 0; 202 } 203 204 public final String getActionDefinitionId() { 205 return command.getId(); 206 } 207 208 public final String getDescription() { 209 try { 210 return command.getCommand().getDescription(); 211 } catch (final NotDefinedException e) { 212 return null; 213 } 214 } 215 216 public final ImageDescriptor getDisabledImageDescriptor() { 217 final String commandId = getActionDefinitionId(); 218 final ICommandImageService commandImageService = (ICommandImageService) serviceLocator 219 .getService(ICommandImageService.class); 220 return commandImageService.getImageDescriptor(commandId, 221 CommandImageManager.TYPE_DISABLED, style); 222 } 223 224 public final HelpListener getHelpListener() { 225 return null; 227 } 228 229 public final ImageDescriptor getHoverImageDescriptor() { 230 final String commandId = getActionDefinitionId(); 231 final ICommandImageService commandImageService = (ICommandImageService) serviceLocator 232 .getService(ICommandImageService.class); 233 return commandImageService.getImageDescriptor(commandId, 234 CommandImageManager.TYPE_HOVER, style); 235 } 236 237 public final String getId() { 238 return id; 239 } 240 241 public final ImageDescriptor getImageDescriptor() { 242 final String commandId = getActionDefinitionId(); 243 final ICommandImageService commandImageService = (ICommandImageService) serviceLocator 244 .getService(ICommandImageService.class); 245 return commandImageService.getImageDescriptor(commandId, style); 246 } 247 248 public final IMenuCreator getMenuCreator() { 249 return null; 251 } 252 253 public final int getStyle() { 254 final State state = command.getCommand().getState(IMenuStateIds.STYLE); 256 if (state instanceof RadioState) { 257 return IAction.AS_RADIO_BUTTON; 258 } else if (state instanceof ToggleState) { 259 return IAction.AS_CHECK_BOX; 260 } 261 262 return IAction.AS_PUSH_BUTTON; 263 } 264 265 public final String getText() { 266 try { 267 return command.getName(); 268 } catch (final NotDefinedException e) { 269 return null; 270 } 271 } 272 273 public final String getToolTipText() { 274 return getDescription(); 275 } 276 277 public final boolean isChecked() { 278 final State state = command.getCommand().getState(IMenuStateIds.STYLE); 279 if (state instanceof ToggleState) { 280 final Boolean currentValue = (Boolean ) state.getValue(); 281 return currentValue.booleanValue(); 282 } 283 284 return false; 285 } 286 287 public final boolean isEnabled() { 288 final Command baseCommand = command.getCommand(); 289 return baseCommand.isEnabled() && enabled; 290 } 291 292 304 public final boolean isEnabledDisregardingCommand() { 305 return enabled; 306 } 307 308 public final boolean isHandled() { 309 final Command baseCommand = command.getCommand(); 310 return baseCommand.isHandled(); 311 } 312 313 public final void run() { 314 runWithEvent(null); 315 } 316 317 public final void runWithEvent(final Event event) { 318 final Command baseCommand = command.getCommand(); 319 final ExecutionEvent executionEvent = new ExecutionEvent(command 320 .getCommand(), command.getParameterMap(), event, null); 321 try { 322 baseCommand.execute(executionEvent); 323 firePropertyChange(IAction.RESULT, null, Boolean.TRUE); 324 325 } catch (final NotHandledException e) { 326 firePropertyChange(IAction.RESULT, null, Boolean.FALSE); 327 328 } catch (final ExecutionException e) { 329 firePropertyChange(IAction.RESULT, null, Boolean.FALSE); 330 332 } 333 } 334 335 public final void setAccelerator(final int keycode) { 336 } 338 339 public final void setActionDefinitionId(final String id) { 340 final boolean oldChecked = isChecked(); 342 final String oldDescription = getDescription(); 343 final boolean oldEnabled = isEnabled(); 344 final boolean oldHandled = isHandled(); 345 final ImageDescriptor oldDefaultImage = getImageDescriptor(); 346 final ImageDescriptor oldDisabledImage = getDisabledImageDescriptor(); 347 final ImageDescriptor oldHoverImage = getHoverImageDescriptor(); 348 final String oldText = getText(); 349 350 final Command oldBaseCommand = command.getCommand(); 352 oldBaseCommand.removeCommandListener(commandListener); 353 final ICommandService commandService = (ICommandService) serviceLocator 354 .getService(ICommandService.class); 355 final Command newBaseCommand = commandService.getCommand(id); 356 command = new ParameterizedCommand(newBaseCommand, null); 357 newBaseCommand.addCommandListener(commandListener); 358 359 final boolean newChecked = isChecked(); 361 final String newDescription = getDescription(); 362 final boolean newEnabled = isEnabled(); 363 final boolean newHandled = isHandled(); 364 final ImageDescriptor newDefaultImage = getImageDescriptor(); 365 final ImageDescriptor newDisabledImage = getDisabledImageDescriptor(); 366 final ImageDescriptor newHoverImage = getHoverImageDescriptor(); 367 final String newText = getText(); 368 369 if (newChecked != oldChecked) { 371 if (oldChecked) { 372 firePropertyChange(IAction.CHECKED, Boolean.TRUE, Boolean.FALSE); 373 } else { 374 firePropertyChange(IAction.CHECKED, Boolean.FALSE, Boolean.TRUE); 375 } 376 } 377 378 if (!Util.equals(oldDescription, newDescription)) { 379 firePropertyChange(IAction.DESCRIPTION, oldDescription, 380 newDescription); 381 firePropertyChange(IAction.TOOL_TIP_TEXT, oldDescription, 382 newDescription); 383 } 384 385 if (newEnabled != oldEnabled) { 386 if (oldEnabled) { 387 firePropertyChange(IAction.ENABLED, Boolean.TRUE, Boolean.FALSE); 388 } else { 389 firePropertyChange(IAction.ENABLED, Boolean.FALSE, Boolean.TRUE); 390 } 391 } 392 393 if (newHandled != oldHandled) { 394 if (oldHandled) { 395 firePropertyChange(IAction.HANDLED, Boolean.TRUE, Boolean.FALSE); 396 } else { 397 firePropertyChange(IAction.HANDLED, Boolean.FALSE, Boolean.TRUE); 398 } 399 } 400 401 if (!Util.equals(oldDefaultImage, newDefaultImage)) { 402 firePropertyChange(IAction.IMAGE, oldDefaultImage, newDefaultImage); 403 } 404 405 if (!Util.equals(oldDisabledImage, newDisabledImage)) { 406 firePropertyChange(IAction.IMAGE, oldDisabledImage, 407 newDisabledImage); 408 } 409 410 if (!Util.equals(oldHoverImage, newHoverImage)) { 411 firePropertyChange(IAction.IMAGE, oldHoverImage, newHoverImage); 412 } 413 414 if (!Util.equals(oldText, newText)) { 415 firePropertyChange(IAction.TEXT, oldText, newText); 416 } 417 } 418 419 public final void setChecked(final boolean checked) { 420 final State state = command.getCommand().getState(IMenuStateIds.STYLE); 421 if (state instanceof ToggleState) { 422 final Boolean currentValue = (Boolean ) state.getValue(); 423 if (checked != currentValue.booleanValue()) { 424 if (checked) { 425 state.setValue(Boolean.TRUE); 426 } else { 427 state.setValue(Boolean.FALSE); 428 } 429 } 430 } 431 } 432 433 public final void setDescription(final String text) { 434 final State state = command.getCommand().getState( 435 INamedHandleStateIds.DESCRIPTION); 436 if (state instanceof TextState) { 437 final String currentValue = (String ) state.getValue(); 438 if (!Util.equals(text, currentValue)) { 439 state.setValue(text); 440 } 441 } 442 } 443 444 public final void setDisabledImageDescriptor(final ImageDescriptor newImage) { 445 final String commandId = getActionDefinitionId(); 446 final int type = CommandImageManager.TYPE_DISABLED; 447 final ICommandImageService commandImageService = (ICommandImageService) serviceLocator 448 .getService(ICommandImageService.class); 449 commandImageService.bind(commandId, type, style, newImage); 450 } 451 452 public final void setEnabled(final boolean enabled) { 453 if (enabled != this.enabled) { 454 final Boolean oldValue = this.enabled ? Boolean.TRUE 455 : Boolean.FALSE; 456 final Boolean newValue = enabled ? Boolean.TRUE : Boolean.FALSE; 457 this.enabled = enabled; 458 firePropertyChange(ENABLED, oldValue, newValue); 459 } 460 } 461 462 public final void setHelpListener(final HelpListener listener) { 463 465 } 466 467 public final void setHoverImageDescriptor(final ImageDescriptor newImage) { 468 final String commandId = getActionDefinitionId(); 469 final int type = CommandImageManager.TYPE_HOVER; 470 final ICommandImageService commandImageService = (ICommandImageService) serviceLocator 471 .getService(ICommandImageService.class); 472 commandImageService.bind(commandId, type, style, newImage); 473 } 474 475 public final void setId(final String id) { 476 this.id = id; 477 } 478 479 public final void setImageDescriptor(final ImageDescriptor newImage) { 480 final String commandId = getActionDefinitionId(); 481 final int type = CommandImageManager.TYPE_DEFAULT; 482 final ICommandImageService commandImageService = (ICommandImageService) serviceLocator 483 .getService(ICommandImageService.class); 484 commandImageService.bind(commandId, type, style, newImage); 485 } 486 487 public final void setMenuCreator(final IMenuCreator creator) { 488 } 490 491 public final void setText(final String text) { 492 final State state = command.getCommand().getState( 493 INamedHandleStateIds.NAME); 494 if (state instanceof TextState) { 495 final String currentValue = (String ) state.getValue(); 496 if (!Util.equals(text, currentValue)) { 497 state.setValue(text); 498 } 499 } 500 } 501 502 public final void setToolTipText(final String text) { 503 setDescription(text); 504 } 505 506 } 507 | Popular Tags |