当前位置: 首页 > 知识库问答 >
问题:

如何在Eclipse编辑器中将IFile处理程序转换为活动文件

羊舌兴文
2023-03-14

我正在准备一个eclipse插件,用于检查测试套件中的代码质量(编译器错误/警告/语法检查是在默认编译器中完成的)。如果测试代码出了问题,我们想通知测试套件的开发人员,比如GOTO jumps越过了标签,可能会导致无限循环(测试套件很旧,它们不是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个答案

邢硕
2023-03-14

并不是所有的编辑器都在实际编辑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;
}
 类似资料:
  • 我有一个处理程序,我想在其中从我的工作台中的活动编辑器获取文本。从下面的屏幕截图中,我想获得test.java(“public class test...”)中的所有内容。 我已经成功地在“源”菜单下添加了一个新命令。只是不确定现在从哪里获取活动编辑器中的文本。到目前为止,我在尝试获取文本(它只是在弹出窗口中显示文件名)时所获得的信息如下:

  • 问题内容: 如果我有Java源文件( .java)或类文件( .class),如何将其转换为.exe文件? 我的程序也需要安装程序。 问题答案: javapackager Java Packager工具可编译,打包并准备Java和JavaFX应用程序以进行分发。javapackager命令是命令行版本。 – Oracle文档 该实用程序随JDK一起提供。它可以生成带有该-标志的文件,等等。 Win

  • 我试图在一个简单的JavaFX应用程序中向一个可编辑的组合框中添加一个关键事件处理程序。由于场景构建器不提供对组合框中的TextField的访问,我不得不在代码中添加事件处理程序。 下面是我添加处理程序的尝试。 设置事件处理程序的更合适的方法是什么? 编辑:添加完整的源代码

  • 问题内容: 通常,此代码用于将转换为: 但这仅适用于项目中的Java文件。如何获取when 文件不在工作区中(不在项目中)? 问题答案: 我需要使用Eclipse AST框架中的Binding Resolutions。但是显然,这是不可能的-对于任何需要它的人,请访问以下站点: https://bugs.eclipse.org/bugs/show_bug.cgi?id=206391

  • 我需要将一些模拟数据的行尾转换为Unix格式。我在Win 7上使用原子编辑器。 在网上,我发现一个Atom软件包可以转换行尾,但该软件包说它已被弃用,因为Atom现在将其作为标准功能。 我在Atom中找不到任何这样的标准功能——Atom的留档甚至不知道什么是行尾转换。 “关于Atom”告诉我Atom是v.1.23.3的最新版本 在我看来,这似乎是一个悖论。 不,我不能只用记事本。) 我已经研究了很