在我的 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.
发布评论
评论(7)
您可以做两件事:
1)进行选择,然后调用 SWT.SELECTION 的通知侦听器,即:
notifyListener 方法旨在用于自定义控件,因此为了更正确,您可以执行选项 2。2
)调用您在侦听器中调用的方法,即:
在这种情况下,您可以调用:
You can do two things:
1) do the selection and then call notify listeners for an SWT.SELECTION i.e. :
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.:
In this case you can call:
我刚刚遇到这个问题并这样解决:
在进行选择之前将焦点放在树上似乎会通知听众,而以前却没有。
I've just had this problem and solved it like this:
Giving the focus to the tree before making the selection seemed to notify the listeners, where before it was not.
我也有同样的问题。我使用的解决方法是在调用
treeViewer.getTree().select(treeViewer.getTree().getItem(0));
方法后以编程方式触发侦听器上的事件。查询在树上注册的侦听器并获取对侦听器的引用:
创建 SelectionEvent 并通过手动调用 widgetSelected 触发选择强>方法:
这个解决方案对我来说效果很好。希望它对你有用。
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.Query the listeners registered on the Tree and get a reference to your listener:
Create a SelectionEvent and trigger the selection by manually calling the widgetSelected method:
This solution worked fine for me. Hope it does for you.
尝试在
执行 a或
设置选择后
。 编辑
好的,所以上面的调用不起作用...如果您足够绝望,您可以追踪源代码并找出实际调用
selectionChanged()
的内容。只需在您的方法中设置一个断点并启动堆栈即可。也许您知道如何以另一种方式实现此调用。Try to do a
or
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.尝试使用
addPostSelectionListener
而不是addSelectionListener
在工作台页面注册监听器。查看StructuredViewer
的代码,以编程方式设置选择时将调用上述方法firePostSelectionChanged
。Try
addPostSelectionListener
instead ofaddSelectionListener
to register your listener at the workbench page. Looking at the code ofStructuredViewer
, the mentioned methodfirePostSelectionChanged
will be called when programmatically setting the selection.StructuredViewer.setSelection(ISelection, boolean)
将触发SelectionChangedEvent
,除非:setSelection
时,会抛出无效的线程访问)。StructuredViewer.setSelection(ISelection, boolean)
will fireSelectionChangedEvent
unless:setSelection
from a non-UI thread which throws Invalid thread access).确保您的零件(视图)具有焦点!否则 setSelection 将不起作用。
首先在您的类中
注入
PartService,或将其作为参数添加到您的命令执行方法 (EPartService partService
) 中,然后执行以下操作: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: