【问题标题】:How to get selected file name and its package path in eclipse plugin如何在eclipse插件中获取选定的文件名及其包路径
【发布时间】: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


【解决方案1】:

您显示的代码应该适用于视图中的选择,但如果您使用的是编辑器,则需要以不同的方式处理。

类似:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

IEditorPart editor = page.getActiveEditor();

IEditorInput input = editor.getEditorInput();

IFile file = (IFile)Platform.getAdapterManager().getAdapter(input, IFile.class);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2012-01-31
    相关资源
    最近更新 更多