在Eclipse插件开发中选择“运行方式”时,如何检索多页编辑器插件的内容?

时间:2018-01-16 11:40:39

标签: eclipse-plugin eclipse-pde

需要一个eclipse插件来允许用户输入脚本并通过IDE运行该脚本。为此,我使用多页面编辑器模板实现了一个eclipse插件。我添加了扩展程序org.eclipse.debug.ui.launchShortcuts,并提供了对我的接口org.eclipse.debug.ui.ILaunchShortcut的实现的引用。此接口需要提供这些方法的实现:

public void launch(final ISelection selection, final String mode);
public void launch(final IEditorPart editor, final String mode);

当用户 - 例如我 - 右键单击​​项目资源管理器中的文件,选择“运行方式”并选择我提供的启动器,然后执行此启动方法。从项目资源管理器中选择文件时,会遇到第一种方法 - 即ISelection版本。当右键单击编辑器区域并通过上下文菜单中的“运行方式”选项选择启动器时,将触发第二种方法 - 即IEditorPart版本。

我现在需要了解如何检索该编辑器页面的内容。在第一种方法中,我相信我可以使用以下代码检索文件名:

IFile file = (IFile) ((IStructuredSelection) selection).getFirstElement();
String path = file.getFullPath().makeAbsolute().toString();

但是,该路径与项目的根相关。我不知道如何检索项目根目录的路径。

第二种方法的实现更成问题,因为我不知道如何根据IEditorPart找到路径。

我希望你能提供帮助。非常感谢

1 个答案:

答案 0 :(得分:0)

如果您询问如何获取编辑器当前正在编辑的文件,您可以使用以下内容:

IPath filepath = null;

IEditorInput input = editor.getEditorInput();

IFile file = input.getAdapter(IFile.class);
if (file != null) {
   filepath = file.getFullPath();
}

if (filepath == null) {
    ILocationProvider locationProvider = input.getAdapter(ILocationProvider.class);
    if (locationProvider != null) {
        filepath = locationProvider.getPath(input);
    }
}

试图直接获取编辑器的IFile frpm,如果失败则尝试寻找位置提供者。

相关问题