【发布时间】:2016-06-21 09:56:49
【问题描述】:
我想在单击右键菜单时在eclipse中获取文件名及其包路径。
Action类实现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。
我已经通过以下链接, How to get the active package path in eclipse workspace
但没有结果。
【问题讨论】:
-
你什么时候运行这段代码?
-
@greg-449 一旦用户点击了右键菜单。
-
sel的实际类型是什么? -
右键在哪里?在视图中,编辑器,......?
-
@RüdigerHerrmann sel 类型为 org.eclipse.jface.text.TextSelection
标签: java eclipse eclipse-plugin