单击右键菜单时,我想在Eclipse中获取文件名及其包路径。
动作类已实现 IObjectActionDelegate
运行方法如下,
public void run(IAction action)
{
ISelection sel = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
if (!(sel instanceof IStructuredSelection))
return null;
}
IStructuredSelection selection = (IStructuredSelection) sel ;
Object obj = selection.getFirstElement();
IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj, IFile.class);
但是“ sel”变量不是IStructuredSelection的实例。因此,它返回null。
我已经通过以下链接,如何在Eclipse工作区中获取活动的包路径
但是没有结果。
您显示的代码应适用于视图中的选择,但是如果您要使用的是编辑器,则需要以不同的方式进行操作。
就像是:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editor = page.getActiveEditor();
IEditorInput input = editor.getEditorInput();
IFile file = (IFile)Platform.getAdapterManager().getAdapter(input, IFile.class);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句