簡體   English   中英

Eclipse插件:在右鍵單擊彈出菜單中添加了新項,如何確定文件名/路徑?

[英]Eclipse Plugin: added new item to right-click popup menu, how can the file name/path be determined?

當在菜單上選擇新項目時觸發的execute方法中的ExecutionEvent似乎並不引用右鍵單擊的文件。 我想知道是否需要為此添加偵聽器,如果這樣做會干擾執行方法。

事件中得到的是當前選擇,它可能是文件,也可能是其他內容,具體取決於您如何定義菜單“ enabledWhen”。 您可以從選擇中獲取文件。

IStructuredSelection selection = HandlerUtil.getCurrentStructuredSelection(event);

if (!selection.isEmpty()) {
  IFile file = Adapters.adapt(selection.getFirstElement(), IFile.class, true);

  if (file != null) {
      ... your code
  }
}

在某些情況下,無法使用IFile的適配器,但可以使用IResource的適配器。 在這種情況下,請使用:

IResource resource = Adapters.adapt(selection.getFirstElement(), IResource.class);
if (resource instanceof IFile) {
  IFile file = (IFile)resource;

}

注意: getCurrentStructuredSelectionAdapters是相對較新的API。 在舊版本的Eclipse中,您將需要稍微復雜一些的代碼。

HandlerUtilorg.eclipse.ui.handlers.HandlerUtil插件中的org.eclipse.ui.workbenchAdaptersorg.eclipse.core.runtime.Adapters

暫無
暫無

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

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