Reputation: 161
i'm preparing an eclipse plugin which checks quality of code in out test suites (compiler errors/warnings/ syntax checks are done by default compiler). We'd like to inform a developer of test suite if something is wrong in test code, like GOTO jumps goes over the label and it may result in infinite loop (test suites are very old, they are not in java or any normal language).
We'd like to report IMarker with warnings in specific lines and specific message (I've got both line & message in some ArrayList, now i need just to put them on opened file). But I'm unable to get the IFile handler for opened file (not in any project, just an active tab in editor pane).
How do i get an IFile handler for active file in editor pane in Eclipse?
Following code results in exception:
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
IEditorPart editor = page.getActiveEditor();
IFileEditorInput iFileInput = (IFileEditorInput) editor.getEditorInput(); //exception is here
A console output (exception):
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)
Upvotes: 1
Views: 1441
Reputation: 111142
Not all editors are actually editing IFile
objects, some can be editing files which are not in the workspace. These editors use an editor input which is not based on IFileEditorEditor
.
In the case you have the input is FileStoreEditorInput
which implements IURIEditorInput
which just gives you the URI of the file being edited.
You can use code something like the following to try and get the IFile from the editor input:
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;
}
The returned IFile
may be null if the editor is not editing a workspace file.
Upvotes: 1