![](/img/trans.png)
[英]How to properly set the content of an IFile in Eclipse plugin when the editor is opened
[英]How to get IFile handler to an active file in Eclipse editor
我正在准備一個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)
並非所有的編輯器實際上都在編輯IFile
對象,有些可以編輯不在工作區中的文件。 這些編輯器使用的編輯器輸入不基於IFileEditorEditor
。
如果您有輸入,則為FileStoreEditorInput
,它實現IURIEditorInput
,該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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.