Search code examples
eclipseeditorcopyrcpcut

Use org.eclipse cut/copy/paste in custom RCP application


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 ;)


Solution

  • 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();
    }