[英]how can I add popup menu in right-click new->MyNewMenu on 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;
}
注意: getCurrentStructuredSelection
和Adapters
是相對較新的API。 在舊版本的Eclipse中,您將需要稍微復雜一些的代碼。
HandlerUtil
是org.eclipse.ui.handlers.HandlerUtil
插件中的org.eclipse.ui.workbench
, Adapters
是org.eclipse.core.runtime.Adapters
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.