.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

How to make views communicate in Eclipse RCP


In eclipse Rich Client Platform programming, some times you need to keep contact between views. How can we do that?
After a couple of hours research, I found the way to make two views interact each other.
There are a few articles on web, but I think they make us embarrassed.
I will make it more shortly and clearly.
We have two views: ViewA and ViewB.
In this case: we assume that ViewB is the event sender, and ViewA is the rest one, the receiver.
So now, what we have to do with each of them.
ViewB
we have to register the selection provider to the SelectionService of workbench. That simply we put this in the function createPartControl of ViewB:

private TreeViewer treeView;
public void createPartControl(Composite parent){
     treeView=new TreeViewer(parent,SWT.MULTI);
     treeView.setContentProvider(new TreeContentProvider());
     treeView.setLabelProvider(new TreeLabelProvider());
     treeView.setInput(input);
     this.getSite().setSelectionProvider(this.treeView);//the one i mentioned above -> we have just registered the           SelectionProvider
}

ViewA
This is the receiver, it consume the selection service.
We have two thing needed to be done:
First: create the listener object
In this view, we create an listener for the selection when it is changed(from ViewB).

private final ISelectionListener outLineSelectionChangedListener = new ISelectionListener()
    {
        @Override
        public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection)
        {
            if (sourcepart != ConnectionEditor.this)
            {
                if (selection instanceof IStructuredSelection)
                {
                    Object o = ((IStructuredSelection) selection).getFirstElement();
                    if (o instanceof ConnectionOutlineField)
                    {
                        ConnectionOutlineField data = (ConnectionOutlineField) o;
                        if (data != null)
                        {
                            // TODO do something with the received object
                        }
                    }
                }
            }
        }
    };

Above ConnectionOutlineField object is the object on the tree viewer of mine.
Second: we create register the listener to Selection Service. In the createPartControl function, we put this at the end.

@Override
public void createPartControl(Composite parent)
{
    this.getSite().getWorkbenchWindow().getSelectionService()
            .addSelectionListener(this.outLineSelectionChangedListener);
}

As the resume, with the sender part, we set the selection provider(1) through setSelectionProvider(TreeViewer) function. Otherwise, with the receiver part, we create the listener(2), and register that listener(3). So, just 3 steps you have to do if you want two views communicate each other.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: