开发者

RCP SelectionService

I have two views: one with a TreeViewer and another that has SWT widgets. I want activities in the view with the widgets to cause changes in the view with the TreeViewer. In reading the various resources on the subject, they all talk about allowing JFace viewers to issue selections and receive notifications but they don't talk about being able to trigger these selections from a Text widget, for example.

Can I do something like this, assuming the view in which this code is, is an ISelectionProvider?

Text someText = new Text( parent, SWT.BORDER ) ;
someText.addKeyListener( new KeyListener() {
  @Override
  public void keyPressed( KeyEvent e ) {}

  @Override
  public void keyReleased( KeyEvent e ) {
    ...
    ...
    CellInfo cellInfo = new CellInfo( /*text collected while typing*/);
    currentSelection = new StructuredSelection( cellInfo ) ; 
    setSelection( currentSelection ) ;

...
...

public void setSelection( ISelection selection ) {
  Object[] list = getListeners() ;//listeners.getListeners();  
  for (int i = 0; i < list.length; i++) {  
    ((ISelectionChangedListener) list[i])  
     .selection开发者_运维知识库Changed(new SelectionChangedEvent(this, selection));  
  } 
}

This seems to work on the selection side, but the on the other side where I want to consume the selection, nothing happens. In that viewer I implement the ISelectionListener interface and do the following in the beginning of the createPartControl() method:

getSite().getPage().addSelectionListener(this);

But the selectionChanged() method never gets called.

My guess is that I am doing something wrong on the producer side since the consumer side seems pretty straightforward. Any pointers?

Thanks!

Jon


Have you told the Workbench about your selection provider? On the producer side, in your createPartControl() you need:

getSite().setSelectionProvider(this);


Due to a design decision that is applied throughout SWT, events are usually not sent in response to programmatic changes (as opposed to user actions). However, you could do something like:

// (1) set a new selection for the view's viewer
myViewer.setSelection(ISelection, boolean);
// (2) setup event to be fired
// (2.1) create new org.eclipse.swt.widgets.Event
Event event = new Event();
// (2.2) set some fields in event
// e.g., event.widget = myViewer.getControl();
// (3) fire event via Viewer's Control (is Widget)
myViewer.getControl().notifyListeners(SWT.Selection, event);

But be aware that this method causes a good occasion to shoot yourself in the foot (from my own experience).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜