Question: In my Eclipse RCP application I use the Selection Service as described in this nice [article](http://www.eclipse.org/articles/Article-WorkbenchSelections/article.html). There is a *TreeViewer* in one view registered as a *SelectionProvider*:

`getSite().setSelectionProvider(viewer);`

Another view is receiving the events from the *TreeViewer*:

```
selectionListener = new ISelectionListener() {
  public void selectionChanged(IWorkbenchPart part, ISelection selection) {
    pageSelectionChanged(part, selection);
  }
 };
 getSite().getPage().addSelectionListener(selectionListener);

```

Everything works fine, if the events are triggered my normal mouse clicks. I would like to programmatically fire a selection events by selection an item in the tree:

```
treeViewer.setSelection(new StructuredSelection(element),true);

```

This is not working. Method *selectionChanged* is not called in the receiver-view. The issue is discussed in this [forum thread](http://www.eclipse.org/forums/index.php/m/328232/). There is no solution.

**EDIT** 

There is no proper way to handle a mouse triggered click the same way as a programmatically selection. A mouse click activates the view a programmatically selection does not.

My Solution is to register the second view the same way by `Selection Service` as the first view. After that both view are getting selection events directly from the active editor.

Answer: 
I had the same problem. The workaround I used was to trigger the event on the Listener programmatic-ally after calling the `treeViewer.getTree().select(treeViewer.getTree().getItem(0));` method.

1. Query the listeners registered on the Tree and get a reference to your listener:

```
org.eclipse.swt.widgets.Listener[] listeners = treeViewer.getTree().getListeners(SWT.Selection);
for (int i=0; i<listeners.length; i++) {
        if (listeners[i] instanceof TypedListener) {
            if (((TypedListener)listeners[i]).getEventListener() instanceof TreeSelectionListener){
                  // Step 2: Fire the event code goes here
 }}}

```
2. Create a **SelectionEvent** and trigger the selection by manually calling the **widgetSelected** method:

```
Event underlyingEvent = new Event();
underlyingEvent.widget = treeViewer.getTree();
SelectionEvent selectionEvent = new SelectionEvent(underlyingEvent);
((TreeSelectionListener)((TypedListener)listeners[i]).getEventListener()).widgetSelected(selectionEvent);

```

This solution worked fine for me. Hope it does for you.
`StructuredViewer.setSelection(ISelection, boolean)` will fire `SelectionChangedEvent` unless:

* the viewer is executing code that explicitly preserves selection (like refresh) or
* an exception occurs before it gets to event notification (e.g. when you call `setSelection` from a non-UI thread which throws *Invalid thread access*).