简体   繁体   中英

How to use the Edit button in eclipse for a plugin eclipse

I have integrated a menu to the eclipse toolbar to create an editor. I would like to link the button in the Edit menu of eclipse (which allows to copy, paste, cut, delete ...), to the methods I use in my Editor.

For example, my editor creates a canvas in which I can draw. I would like to use the button "copy" in the Edit eclipse menu to copy my draw. The method is already implemented (on the picture, you can see the "copy" button which works), I just need to make the link between the Edit eclipse menu and my method. 在此处输入图片说明 How can I do that ?

If I am not clear enough, tell me.

You use the editor action bar contributor class that you specify in the org.eclipse.ui.editors extension point to do this

<extension point="org.eclipse.ui.editors"> 
  <editor 
     id="com.xyz.Editor" 
     contributorClass="com.xyz.EditorContributor" 
     ...more ....> 
  </editor> 
</extension> 

Your contributor class will normally extend org.eclipse.ui.part.EditorActionBarContributor . For a text editor or a mult-page editor there are other contributor classes you can extend.

You override the setActiveEditor method to be told when your editor becomes active. When your editor is active you set the global action handler for the global actions you want to handle, something like:

public class EditorContributor extends EditorActionBarContributor
{
  private IEditorPart activeEditorPart = null;

  @Override
  public void setActiveEditor(IEditorPart targetEditor)
  {
    if (activeEditorPart == targetEditor)
      return;

    activeEditorPart = targetEditor;

    if (targetEditor instanceof MyEditor) {
        IActionBars actionBars = getActionBars();

        actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);
        // ... more global action handlers
    }
  }
}

where copyAction is an Action that you write.

You can also override the contributeToToolBar and contributeToMenu methods to add new actions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM