Eclipse RCP: how to observe the status of cut / copy / paste commands?

I am currently struggling with the following RCP Eclipse commands:

  • org.eclipse.ui.edit.cut
  • org.eclipse.ui.edit.copy
  • org.eclipse.ui.edit.paste

I use them as a command input to a toolbar, but UIElements (toolbar items) are not updated when the status of these commands <changes handled.

For testing, I used the polling mechanism to verify that the state of these commands really changes depending on the current focused element, and I found that the handler remains the same, but the state of the handler “processed” changes correctly, invoking the “processed” state also correctly is changing.

The only problem is that none of these state changes cause a notification (neither in the command ICommandListener, nor in the handler IHandlerListener), so UIElements will not get updated.

Here is some test code to monitor the status of the command:

ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);

final String commandId="org.eclipse.ui.edit.copy";
Command command = commandService.getCommand(commandId);
command.addCommandListener(new ICommandListener() {

    public void commandChanged (CommandEvent commandEvent) {
        System.out.println(">> Command changed: " + commandId);
    }
});

Am I missing something, or is this an error in the implementations of the cut / copy / paste handler? Any ideas?

EDIT: Commands are activated all the time, and the handler never exchanges, only the state of the handler " handled" (and therefore also the state " handled") changes depending on which ui element has focus. However, when this state changes, notifications are not received. This leads to the fact that the toolbar buttons always turn on, and clicking on them calls up org.eclipse.core.commands.NotHandledException: There is no handler to execute for command.

+3
3

, cut/copy/paste, org.eclipse.ui.internal.handlers.WidgetMethodHandler. , . .

WidgetMethodHandler:

 public final boolean isHandled() {
     return getMethodToExecute() != null;
 }

getMethodToExecute() Display.getCurrent().getFocusControl(), , .

, org.eclipse.swt.widgets.Text, cut(), copy() paste(), , , 'true' isHandled().

, , , ( , ), isHandled.

, // , , , .

, mechansim ui ( ).: - (

+1

, , , Eclipse , "-" . , . Eclipse , .

-1

, , .

. > > > > .

<activeWhen/> plugin.xml . , , Shell, .

   <extension
         point="org.eclipse.ui.handlers">
      ...
      <handler
            class="org.eclipse.ui.examples.contributions.view.SwapInfoHandler"
            commandId="org.eclipse.ui.examples.contributions.view.swap">
         <activeWhen>
            <reference
                  definitionId="org.eclipse.ui.examples.contributions.view.inView">
            </reference>
         </activeWhen>
         <enabledWhen>
            <count
                  value="2">
            </count>
         </enabledWhen>
      </handler>
      ...
-1
source

Source: https://habr.com/ru/post/1709077/


All Articles