Kaadzia
Kaadzia

Reputation: 1431

Eclipse RCP: How can I update a view when the selected editor changes?

This should be quite a common problem, but I couldn't find anything helpful on the topic.

We are developing an application with Eclipse RCP. The application shows data in an editor of which usually multiple instances are open. In an additional view you can edit the editor-values. When the values are changed in the view they are updated in the editor and it's dirty flag is set.

So far it works fine. What we're missing is: When another editor instance gets the focus, our view should show the data of this editor.

I managed to do that for two views. The second view is sucessfully updated using a TableViewer as selection Provider and registering a SelectionListener in the other view. I tried the same thing for the editor using a Viewer I subclassed from ContentViewer, but it didn't work.

Can this approach be working? Or do I need a different approach on the problem?

Upvotes: 2

Views: 4711

Answers (2)

Kaadzia
Kaadzia

Reputation: 1431

Thank you cerealk, that was exactly what I needed. :-)

Update the View when another Editor is selected

public class myView {

    // Create an IPartListener2 
    IPartListener2 pl = new IPartListener2() {

        // If the Editor I'm interested in was updated ...
             public void partActivated(IWorkbenchPartReference ref) {
                  IWorkbenchPart part = ref.getPart(true);
                  if (part instanceof DetailEditor) {

                  // ... update the view
                  Contact contactFromSelectedEditor = ((DetailEditor) part).detailComposite.contact;
                  detailComposite.update(contactFromSelectedEditor);
                  }
             }
        ...
        }

    // Add the IPartListener2 to the page   
    IWorkbenchPage page = this.getSite().getPage();
    page.addPartListener(pl);

}


Why use an IPartListener2 instead of an IPartListener

The IPartListener2 replaces IPartListener with 3.5.

As explained in this this answer:

You should always use IPartListener2 as it can handle part-change events on parts that have not yet been created because they are hidden in a stack behind another part.

This listener will also tell you when a part is made visible or hidden or when an editor's input is changed.

Upvotes: 2

dmitriykovalev
dmitriykovalev

Reputation: 644

May be you can subclass your view from PageBookView and then provide special adapter for your editor. Outline View is implemented using this approach.

Upvotes: 3

Related Questions