4

I am developing a RCP application and I need cut/copy/paste in this app. As there are already commands which are delivered by eclipse (org.eclipse.ui.edit.copy, ...) I want to use them (I already added them to the toolbar, e.g.) in an editor. I played around a little, but I don't get it how I can react to the copy/paste command. E.g. how do I get informed in an editor if something was copied or pasted?

Is there an easy way to use the commands like the Save Command. There I just have to implement the ISaveablePart and then the doSave() or doSaveAs() methods are called...I really like this, but I didn't find ICopyablePart,... interfaces ;)

Cœur
  • 37,241
  • 25
  • 195
  • 267
TerenceJackson
  • 1,776
  • 15
  • 24

1 Answers1

7

If you need specific behaviour to copy (or any command) within your editor or view, you would activate a handler for it. Usually in your createPartControl(Composite) method:

IHandlerService serv = (IHandlerService) getSite().getService(IHandlerService.class);
MyCopyHandler cp = new MyCopyHandler(this);
serv.activateHandler(org.eclipse.ui.IWorkbenchCommandConstants.EDIT_COPY, cp);

The other common way is to provide a handler through your plugin.xml:

<handler commandId="org.eclipse.ui.edit.copy"
         handler="com.example.app.MyCopyHandler">
   <activeWhen>
      <with variable="activePartId">
         <equals value="com.example.app.MyEditorId"/>
      </with>
   </activeWhen>
</handler>

Then in your handler, you would get the active part and call your copy implementation on it. ex:

IWorkbenchPart part = HandlerUtil.getActivePart(event);
if (part instanceof MyEditor) {
    ((MyEditor)part).copy();
}
Paul Webster
  • 10,614
  • 1
  • 25
  • 32
  • Hi Paul, I tried it both ways. But I always get this exception: "There is no handler to execute for command org.eclipse.ui.edit.copy." Do you know why I get this? – TerenceJackson May 23 '11 at 09:27
  • Hi Paul, I have solved the problem. It occurs if the isHandled() returnes false... Returning true will call the execute method... – TerenceJackson May 23 '11 at 11:41
  • 1
    Try and subclass AbstractHandler instead of implementing IHandler. It'll behave correctly in most cases then. – Paul Webster May 23 '11 at 11:50