在自定义 RCP 应用程序中使用 org.eclipse 剪切/复制/粘贴

2022-01-20 00:00:00 复制 editor eclipse rcp cut

我正在开发一个 RCP 应用程序,我需要在这个应用程序中剪切/复制/粘贴.由于已经有 eclipse 提供的命令(org.eclipse.ui.edit.copy,...)我想在编辑器中使用它们(我已经将它们添加到工具栏,例如).我玩了一点,但我不明白我如何对复制/粘贴命令做出反应.例如.如果某些内容被复制或粘贴,我如何在编辑器中获得通知?

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?

有没有一种简单的方法来使用像保存命令这样的命令.在那里我只需要实现 ISaveablePart,然后调用 doSave() 或 doSaveAs() 方法......我真的很喜欢这个,但我没有找到 ICopyablePart,......接口;)

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

推荐答案

如果您需要在编辑器或视图中复制(或任何命令)特定行为,您可以为其激活处理程序.通常在您的 createPartControl(Composite) 方法中:

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

另一种常见的方式是通过你的 plugin.xml 提供一个处理程序:

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

相关文章