2
votes

As the title says I'm wondering whether Eclipse RCP 4 provides any built in cut/copy/paste handlers which can be linked to the org.eclipse.ui.edit.cut, org.eclipse.ui.edit.copy and org.eclipse.ui.edit.paste commands?

I appreciate that that a custom handler may be needed for some SWT widgets or more complex use cases with cut/copy/paste operations, but I can't help but feel I'm trying to re-invent the wheel to copy some text from one component and paste in into another.

If there aren't any built in cut/copy/paste handlers, are there any well documented examples of how to do this? I understand how to use the clipboard.getContents and clipboard.setContents methods, but have found this starts to become non-trivial when trying to find out what text was selected when the copy command is invoked and which component has focus and whether its read only when the paste command is invoked.

I've looked at this StackOverflow question but it doesn't explain whether there any built in handlers or offer any guidance on writing my own handlers.

1

1 Answers

2
votes

For a 3.x compatibilty mode Eclipse 4 application these commands are defined as:

 <command
        name="%command.cut.name"
        description="%command.cut.description"
        categoryId="org.eclipse.ui.category.edit"
        id="org.eclipse.ui.edit.cut"
        defaultHandler="org.eclipse.ui.internal.handlers.WidgetMethodHandler:cut" />
  <command
        name="%command.copy.name"
        description="%command.copy.description"
        categoryId="org.eclipse.ui.category.edit"
        id="org.eclipse.ui.edit.copy"
        defaultHandler="org.eclipse.ui.internal.handlers.WidgetMethodHandler:copy" />
  <command
        name="%command.paste.name"
        description="%command.paste.description"
        categoryId="org.eclipse.ui.category.edit"
        id="org.eclipse.ui.edit.paste"
        defaultHandler="org.eclipse.ui.internal.handlers.WidgetMethodHandler:paste" />

So they all use org.eclipse.ui.internal.handlers.WidgetMethodHandler as the default handler which is used when no other handler is active.

This handler uses reflection to look for the method name cut, copy or paste in the currently focussed SWT Widget and calls that method if it is found.

For a pure e4 application there is no default definition of cut/copy/paste commands and the WidgetMethodHandler is not available. SWT controls will continue to support cut/copy/paste but there is no other support.

You can put text in the clipboard using something like:

Clipboard clipboard = new Clipboard(Display.getCurrent());

clipboard.setContents(new Object [] {"Text for clipboard"},
                      new Transfer [] {TextTransfer.getInstance()});

clipboard.dispose()

and get text from the clipboard with:

Clipboard clipboard = new Clipboard(Display.getCurrent());

String text = (String)clipboard.getContents(TextTransfer.getInstance());

clipboard.dispose()