如何在Eclipse编辑器

时间:2015-06-09 12:40:20

标签: java eclipse eclipse-plugin

我正在准备一个eclipse插件,用于检查测试套件中的代码质量(编译器错误/警告/语法检查是由默认编译器完成的)。如果测试代码出现问题,我们想通知测试套件的开发人员,比如GOTO跳过标签并且可能导致无限循环(测试套件很旧,它们不是java或任何普通语言)

我们想在特定行和特定消息中报告IMarker警告(我在某些ArrayList中有两行和消息,现在我只需将它们放在打开的文件中)。但是我无法为打开的文件获取IFile处理程序(不在任何项目中,只是编辑器窗格中的活动选项卡)。

如何在Eclipse的编辑器窗格中获取活动文件的IFile处理程序?

以下代码导致异常:

IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
IEditorPart editor = page.getActiveEditor();
IFileEditorInput iFileInput = (IFileEditorInput) editor.getEditorInput(); //exception is here

控制台输出(例外):

java.lang.ClassCastException: org.eclipse.ui.ide.FileStoreEditorInput cannot be cast to org.eclipse.ui.IFileEditorInput
at se.ericsson.ttcnplugin.handlers.AltHandler.execute(AltHandler.java:45)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:293)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:476)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
at org.eclipse.ui.internal.handlers.HandlerService.executeCommand(HandlerService.java:169)
at org.eclipse.ui.internal.handlers.SlaveHandlerService.executeCommand(SlaveHandlerService.java:241)
at org.eclipse.ui.menus.CommandContributionItem.handleWidgetSelection(CommandContributionItem.java:820)
at org.eclipse.ui.menus.CommandContributionItem.access$19(CommandContributionItem.java:806)
at org.eclipse.ui.menus.CommandContributionItem$5.handleEvent(CommandContributionItem.java:796)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1258)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3540)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3161)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
at org.eclipse.equinox.launcher.Main.run(Main.java:1408)
at org.eclipse.equinox.launcher.Main.main(Main.java:1384)

1 个答案:

答案 0 :(得分:0)

并非所有编辑器实际上都在编辑IFile个对象,有些编辑器可能正在编辑不在工作区中的文件。这些编辑器使用的编辑器输入不是基于IFileEditorEditor

如果您输入的是FileStoreEditorInput,它实现了IURIEditorInput,它只是为您提供了正在编辑的文件的URI。

你可以使用类似下面的代码来尝试从编辑器输入中获取IFile:

public static IFile getFileFromEditorInput(IEditorInput input)
{
  if (input == null)
    return null;

  if (input instanceof IFileEditorInput)
    return ((IFileEditorInput)input).getFile();

  IPath path = getPathFromEditorInput(input);
  if (path == null)
    return null;

  return ResourcesPlugin.getWorkspace().getRoot().getFile(path);
}


public static IPath getPathFromEditorInput(IEditorInput input)
{
  if (input instanceof ILocationProvider)
    return ((ILocationProvider)input).getPath(input);

  if (input instanceof IURIEditorInput)
   {
     URI uri = ((IURIEditorInput)input).getURI();
     if (uri != null)
      {
        IPath path = URIUtil.toPath(uri);
        if (path != null)
          return path;
      }
   }

  return null;
}

如果编辑器没有编辑工作区文件,则返回的IFile可能为null。