以编程方式触发 RCP 选择事件

发布于 2025-01-01 02:49:12 字数 1067 浏览 1 评论 0 原文

在我的 Eclipse RCP 应用程序中,我使用了 Selection Service,如这篇精彩的 文章。一个视图中有一个 TreeViewer 注册为 SelectionProvider

getSite().setSelectionProvider(viewer);

另一个视图正在接收来自TreeViewer

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

如果事件是由我正常的鼠标点击触发的,则一切正常。我想通过选择树中的项目以编程方式触发选择事件:

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

这不起作用。方法 selectionChanged 不会在接收者视图中调用。此论坛帖子讨论了该问题。没有解决办法。

编辑

没有正确的方法来处理鼠标触发的单击,就像以编程方式选择一样。单击鼠标可激活以编程方式选择所无法激活的视图。

我的解决方案是通过 Selection Service 以与第一个视图相同的方式注册第二个视图。之后,两个视图都直接从活动编辑器获取选择事件。

In my Eclipse RCP application I use the Selection Service as described in this nice article. 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. 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.

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

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

发布评论

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

评论(7

寄居人 2025-01-08 02:49:12

您可以做两件事:

1)进行选择,然后调用 SWT.SELECTION 的通知侦听器,即:

mybutton.setSelection(true);
mybutton.notifyListeners(SWT.Selection, new Event());

notifyListener 方法旨在用于自定义控件,因此为了更正确,您可以执行选项 2。2

)调用您在侦听器中调用的方法,即:

this.myButton.addSelectionListener(new SelectionListener() {
    public void widgetSelected(final SelectionEvent e) {
      doSomethingaboutTheSelMethod();
 }

在这种情况下,您可以调用:

doSomethingaboutTheSelMethod();

You can do two things:

1) do the selection and then call notify listeners for an SWT.SELECTION i.e. :

mybutton.setSelection(true);
mybutton.notifyListeners(SWT.Selection, new Event());

The notifyListener method is intented to be used for custom controls so to be more correct you could do option number 2.

2) call the method that you call in your listener i.e.:

this.myButton.addSelectionListener(new SelectionListener() {
    public void widgetSelected(final SelectionEvent e) {
      doSomethingaboutTheSelMethod();
 }

In this case you can call:

doSomethingaboutTheSelMethod();
禾厶谷欠 2025-01-08 02:49:12

我刚刚遇到这个问题并这样解决:

treeViewer.getControl.setFocus();
treeViewer.setSelection(new StructuredSelection(element),true);

在进行选择之前将焦点放在树上似乎会通知听众,而以前却没有。

I've just had this problem and solved it like this:

treeViewer.getControl.setFocus();
treeViewer.setSelection(new StructuredSelection(element),true);

Giving the focus to the tree before making the selection seemed to notify the listeners, where before it was not.

千紇 2025-01-08 02:49:12

我也有同样的问题。我使用的解决方法是在调用 treeViewer.getTree().select(treeViewer.getTree().getItem(0)); 方法后以编程方式触发侦听器上的事件。

  1. 查询在树上注册的侦听器并获取对侦听器的引用:

    org.eclipse.swt.widgets.Listener[] 侦听器 = treeViewer.getTree().getListeners(SWT.Selection);
    for (int i=0; i
  2. 创建 SelectionEvent 并通过手动调用 widgetSelected 触发选择强>方法:

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

这个解决方案对我来说效果很好。希望它对你有用。

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.

巡山小妖精 2025-01-08 02:49:12

尝试在

treeViewer.fireSelectionChanged();

执行 a或

treeViewer.firePostSelectionChanged();

设置选择后


编辑

好的,所以上面的调用不起作用...如果您足够绝望,您可以追踪源代码并找出实际调用 selectionChanged() 的内容。只需在您的方法中设置一个断点并启动堆栈即可。也许您知道如何以另一种方式实现此调用。

Try to do a

treeViewer.fireSelectionChanged();

or

treeViewer.firePostSelectionChanged();

after setting the selection.


EDIT

Ok, so the above calls doesn't work... If you are desperate enough you could trace through the sources and find out what actually calls selectionChanged(). Just set a breakpoint in your method and get up the stack. Maybe you get an idea of how to achieve this call on another way.

猫腻 2025-01-08 02:49:12

尝试使用 addPostSelectionListener 而不是 addSelectionListener 在工作台页面注册监听器。查看 StructuredViewer 的代码,以编程方式设置选择时将调用上述方法 firePostSelectionChanged

Try addPostSelectionListener instead of addSelectionListener to register your listener at the workbench page. Looking at the code of StructuredViewer, the mentioned method firePostSelectionChanged will be called when programmatically setting the selection.

策马西风 2025-01-08 02:49:12

StructuredViewer.setSelection(ISelection, boolean) 将触发 SelectionChangedEvent,除非:

  • 查看器正在执行显式保留选择的代码(例如刷新),或者
  • 在收到事件通知之前发生异常(例如,当您从非 UI 线程调用 setSelection 时,会抛出无效的线程访问)。

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).
卸妝后依然美 2025-01-08 02:49:12

确保您的零件(视图)具有焦点!否则 setSelection 将不起作用。

首先在您的类中注入 PartService,或将其作为参数添加到您的命令执行方法 (EPartService partService) 中,然后执行以下操作:

MPart myPart = partService.findPart(MyPart.ID);
partService.activate(myPart, true);
treeViewer.getControl().setFocus();
treeViewer.setSelection(new StructuredSelection(myObject), true);

Make sure your Part (view) has the focus! Otherwise setSelection won't work.

First Inject the PartService in your class, or add it as parameter in your command execute method (EPartService partService), then do the following:

MPart myPart = partService.findPart(MyPart.ID);
partService.activate(myPart, true);
treeViewer.getControl().setFocus();
treeViewer.setSelection(new StructuredSelection(myObject), true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文