Eclipse: Link a view to an editor

Main parts of Eclipse RCP applications are editors and views. Sometimes your view needs to perform some action when an editor is activated. You can present some additional information in the view depending on the element in the editor. You probably know this behaviour since there is a Link to editor button in the toolbar of the Package explorer of the Eclipse IDE. After activating this button the IDE always selects the class in the tree of the view you edit in the active editor. There are no build in classes in the Eclipse RCP to implement this behavior.

To execute some action in your view when an editor is activated your view needs to implement a new small interface: ILinkedWithEditorView. Main method is editorActivated which is called when a editor is activated e.g. by a click of a user. Method getViewSite() is already implemented by Eclipses ViewPart class.

public interface ILinkedWithEditorView {
  /**
   * Called when an editor is activated
   * e.g. by a click from the user.
   * @param The activated editor part.
   */
  void editorActivated(IEditorPart activeEditor);

  /**
   * @return The site for this view.
   */
  IViewSite getViewSite();
}

To activate this interface you need to implement a new IPartListener2 which reacts on events fired by Eclipse „parts“ (views and editors are parts). When you create the listener you have to pass a ILinkedWithEditorView instance. When a part event is fired the listener calls editorActivated of the linked view.

public class LinkWithEditorPartListener implements IPartListener2 {
  private final ILinkedWithEditorView view;

  public LinkWithEditorPartListener(ILinkedWithEditorView view) {
    view = view;
  }

  public void partActivated(IWorkbenchPartReference ref) {
    if (ref.getPart(true) instanceof IEditorPart) {
      view.editorActivated(view.getViewSite().getPage().getActiveEditor());
    }
  }

  public void partBroughtToTop(IWorkbenchPartReference ref) {
    if (ref.getPart(true) == view) {
      view.editorActivated(view.getViewSite().getPage().getActiveEditor());
    }
  }

  public void partOpened(IWorkbenchPartReference ref) {
    if (ref.getPart(true) == view) {
      view.editorActivated(view.getViewSite().getPage().getActiveEditor());
    }
  }

  public void partVisible(IWorkbenchPartReference ref) {
    if (ref.getPart(true) == view) {
      IEditorPart editor = view.getViewSite().getPage().getActiveEditor();
      if(editor!=null) {
        view.editorActivated(editor);
      }
    }
  }

  public void partClosed(IWorkbenchPartReference ref) {}
  public void partDeactivated(IWorkbenchPartReference ref) {}
  public void partHidden(IWorkbenchPartReference ref) {}
  public void partInputChanged(IWorkbenchPartReference ref) {}
}

You have to use both the interface and the listener in the view your want to link to editors. The view itself implements the interface ILinkedWithEditorView. The IPartListener2 is created with an instance of your view. It is registered as part-listener in createPartControl:

public class MyLinkedView extends ViewPart implements ILinkedWithEditorView {

  private IPartListener2 linkWithEditorPartListener  = new LinkWithEditorPartListener(this);
  private Action linkWithEditorAction;
  private boolean linkingActive = false;

  @Override
  public void createPartControl(Composite parent) {
    linkWithEditorAction = new Action("Link with Editor", IAction.AS_CHECK_BOX)
      @Override
      public void run() {
        toggleLinking(isChecked());
      }
    };
    linkWithEditorAction.setImageDescriptor(getImageDescriptor());
    getViewSite().getActionBars().getToolBarManager().add(linkWithEditorAction);
    getSite().getPage().addPartListener(linkWithEditorPartListener);
  }

  @Override
  public void editorActivated(IEditorPart activeEditor) {
      if (!isLinkingActive() || !getViewSite().getPage().isPartVisible(this)) {
        return;
      }
      // do something with content of the editor
  }

  protected void toggleLinking(boolean checked) {
    this.linkingActive = checked;
    if (checked) {
      editorActivated(getSite().getPage().getActiveEditor());
    }
  }
}

Thank’s to Kimberly Horne and her article about linking views and editors which was an important resource for this implementation.

  1. 9. Oktober 2012
  2. 15. November 2013

Hinterlasse einen Kommentar