RCP选型服务

发布于 2024-11-09 01:50:47 字数 1280 浏览 5 评论 0原文

我有两个视图:一个带有 TreeViewer ,另一个带有 SWT 小部件。我希望小部件视图中的活动能够导致 TreeViewer 视图发生变化。在阅读有关该主题的各种资源时,他们都谈论允许 JFace 查看器发出选择并接收通知,但他们没有谈论能够从文本小部件触发这些选择。

假设此代码所在的视图是一个 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])  
     .selectionChanged(new SelectionChangedEvent(this, selection));  
  } 
}

这似乎在选择方面有效,但在我想要使用选择的另一方面,没有任何反应。在该查看器中,我实现了 ISelectionListener 接口,并在 createPartControl() 方法的开头执行以下操作:

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

但是 selectionChanged() 方法从不被叫。

我的猜测是,我在生产者方面做错了,因为消费者方面似乎非常简单。有什么指点吗?

谢谢!

乔恩

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])  
     .selectionChanged(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷…雨湿花 2024-11-16 01:50:47

您是否向工作台告知了您选择的提供商?在生产者方面,在 createPartControl() 中您需要:

getSite().setSelectionProvider(this);

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

getSite().setSelectionProvider(this);
隐诗 2024-11-16 01:50:47

由于整个 SWT 中应用的设计决策,通常不会发送事件对程序更改的响应(而不是用户操作)。然而,您可以做类似的事情:

// (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);

但是要注意这种方法会导致一个搬起石头砸自己脚的好机会(根据我自己的经验)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文