簡體   English   中英

在Eclipse中以編程方式選擇資源不會展開樹

[英]Selecting a resource programatically in Eclipse does not expand tree

我的RCP Eclipse應用程序存在以下問題:我試圖在Project Explorer樹中的IResource上設置選擇。 我使用的代碼是:

final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
final IViewPart viewPart = page.findView(IPageLayout.ID_PROJECT_EXPLORER);
final ISelectionProvider selProvider = viewPart.getSite().getSelectionProvider();
selProvider.setSelection(new StructuredSelection(mrcReportRuleOccurrence.getIResource()));

僅當葉子已經在樹中展開后,該代碼才似乎起作用。 如果樹完全折疊,則代碼不會使eclipse擴展包含的文件夾並在項目瀏覽器中選擇所需的文件。 有誰知道如何做到這一點?

您可以嘗試檢查選擇提供程序是否為Viewer並使用setSelection(selection. reveal)方法:

if (selProvider instanceof Viewer) {
  ((Viewer)selProvider).setSelection(selection, true);
}

可能還需要將TreeSelectionTreePath用於選擇,而不僅僅是StructuredSelection

TreePath path = new TreePath(.... array of objects ...);

TreeSelection selection = new TreeSelection(path);

其中“對象數組”是通過樹到資源的路徑。

但是,選擇其他視圖樹中的項並不是ISelectionProvider接口的用途。 不能保證可以使用此接口完成此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM