我正在尝试在Eclipse中构建一个弹出菜单。实际上,插件有一个弹出的Action,当你右键单击File时会显示一个新选项。我需要知道我右键点击的文件名和项目名称。有谁知道怎么做?
感谢。
答案 0 :(得分:1)
IStructuredSelection currentSelection = (IStructuredSelection)getContext().getSelection();
if(!currentSelection.isEmpty() && ResourceSelectionUtil.allResourcesAreOfType(currentSelection, IResource.PROJECT | IResource.FOLDER | IResource.FILE)){
IResource resource = (IResource)currentSelection.getFirstElement();
}
答案 1 :(得分:0)
首先,让我们为PackageExplorer View创建一个弹出菜单。
<plugin>
<extension
point="org.eclipse.ui.commands">
<command
categoryId="TestPopupMenu.commands.category"
id="TestPopupMenu.commands.sampleCommand"
name="Sample Command">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="testpopupmenu.handlers.SampleHandler"
commandId="TestPopupMenu.commands.sampleCommand">
</handler>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
<command
commandId="TestPopupMenu.commands.sampleCommand"
id="TestPopupMenu.menus.sampleCommand"
mnemonic="S">
<visibleWhen>
<with variable="activeMenuSelection">
<iterate
ifEmpty="false">
<adapt type="org.eclipse.core.resources.IResource">
<test property="org.eclipse.core.resources.name" value="*.*" />
</adapt>
</iterate>
</with>
</visibleWhen>
</command>
</menuContribution>
</extension>
</plugin>
然后,将以下代码添加到处理程序的execute()
方法中,它将在控制台中打印出绝对路径:
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
//get selection service
ISelectionService service = window.getSelectionService();
//get selection
IStructuredSelection structured = (IStructuredSelection) service
.getSelection();
//get selected file
IFile file = (IFile) structured.getFirstElement();
//get the path
IPath path = file.getLocation();
System.out.println(path.toPortableString());
return null;