Search code examples
javaeclipse-rcprcp

How do i connect two views in an RCP Application


I am working on an RCP application, in which I want to connect 2 views so that when I click a node of TreeViewer in view1 the TableViewer in view2 must be populated.

Screenshot of my application

I have 2 view classes namely ViewPart1(TreeViewer) and ViewPart2(TableViewer). Can anyone help me out in achieving this?


Solution

  • Use the selection service.

    In the view (view1) which is providing the selection you must set the selection provider to be the tree:

    getSite().setSelectionProvider(viewer);
    

    where viewer is a TreeViewer (or TableViewer) or something else that implements ISelectionProvider.

    In the view which wants to see selections set up a listener:

    ISelectionService selService = getSite().getWorkbenchWindow().getSelectionService();
    
    selService.addSelectionListener(listener);
    

    where listener is something that implements ISelectionListener.

    Note: Your listener will be told about selection changes everywhere, it is up to you to sort out which ones you want to react to.