Eclipse RCP:以编程方式设置活动部件或选择非活动部件

时间:2023-01-21 11:49:28

in my Eclipse Plugin I have this workflow:

在我的Eclipse插件中,我有这个工作流程:

  1. Get the currently selected project in the Package Explorer
  2. 在Package Explorer中获取当前选定的项目
  3. Do something
  4. 做一点事
  5. Get the currently selected project in the Package Explorer (same as 1)
  6. 在Package Explorer中获取当前选定的项目(与1相同)
  7. Do something different
  8. 做点别的事

1 (and 3) are realized like that:

1(和3)是这样实现的:

ISelectionService selectionService = PlatformUI.getWorkbench()
    .getActiveWorkbenchWindow().getSelectionService();
ISelection selection = selectionService.getSelection();
[...]

Now, the problem is that before 1, the Package Explorer is selected, because that's the only way to trigger the workflow. But step 2 changes the active part because it refreshes a TreeView which makes it the active part. When I now try to run 3, which is the same method as 1, I have a problem: The Package Explorer is no longer the activePart of the selectionService and therefore selection is null.

现在,问题是在1之前,选择了Package Explorer,因为这是触发工作流的唯一方法。但是第2步更改了活动部分,因为它刷新了TreeView,使其成为活动部分。当我现在尝试运行3时,这与1的方法相同,我遇到了一个问题:Package Explorer不再是selectionService的activePart,因此选择为null。

My questions are: Is there any way to get the ISelectionService for a particular View which is not the active one? If not, is there a way to programmatically set the active part before executing step 3?

我的问题是:有没有办法让特定View的ISelectionService不是活动的?如果没有,有没有办法在执行步骤3之前以编程方式设置活动部分?

Btw that's an Eclipse 3.x plugin.

顺便说一句,这是一个Eclipse 3.x插件。

1 个解决方案

#1


3  

If you find the IViewPart for the package explorer you can access its ISelectionProvider directly using:

如果找到包浏览器的IViewPart,则可以使用以下命令直接访问其ISelectionProvider:

IViewPart part = .. find package explorer view part

IViewSite viewSite = part.getViewSite();

ISelectionProvider provider = viewSite.getSelectionProvider();

ISelection selection = provider.getSelection();

#1


3  

If you find the IViewPart for the package explorer you can access its ISelectionProvider directly using:

如果找到包浏览器的IViewPart,则可以使用以下命令直接访问其ISelectionProvider:

IViewPart part = .. find package explorer view part

IViewSite viewSite = part.getViewSite();

ISelectionProvider provider = viewSite.getSelectionProvider();

ISelection selection = provider.getSelection();