java - Eclipse插件: FileStoreEditorInput access unsaved data

标签 java eclipse plugins

我正在尝试访问不属于工作区的文件数据,经过一些测试发现我的对象属于 FileStoreEditorInput 类型。我发现访问 FileStoreEditorInput 数据的解决方案使用了普通的 Java 概念,它不应该访问 Eclipse 环境中未保存的数据。

对于在工作区中打开的文件,我让它与 FileEditorInput 和以下(复杂的)代码一起使用:

private static IEditorReference[] ii;

private static String isOpenInEclipse(String path){
    Display.getDefault().syncExec(new Runnable() {
        @Override
        public void run() {
            ii = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
        }
    });

    for(IEditorReference ier : ii){
        // Fetch the editor's page
        try {
            IEditorInput iei = ier.getEditorInput();
            if(iei instanceof FileEditorInput){
                IFile file = ((FileEditorInput)iei).getFile();
                IEditorPart p;
                // If the editor got the same path and is a text editor, return its data
                if(path.contains(file.getRawLocation().toOSString()) && 
                        (p = ier.getEditor(false)) instanceof ITextEditor){
                    IDocumentProvider provider = ((ITextEditor)p).getDocumentProvider();
                    IDocument document = provider.getDocument(p.getEditorInput());
                    return document.get();
                }
            }
        } catch (PartInitException e) {
            e.printStackTrace();
        }
    }

    return null;
}

FileStoreEditorInput 是否有类似的解决方案,以便我可以添加该方法?

最佳答案

FileStoreEditorInput 实现了 IURIEditorInput,因此您可以使用以下方式获取 File 对象:

if (editorInput instanceof IURIEditorInput)
 {
   IURIEditorInput uriInput = (IURIEditorInput)editorInput;

   URI uri = uriInput.getURI();

   File file = new File(uri);

   ... test path

   .. get document contents in the same way
 }

关于java - Eclipse插件: FileStoreEditorInput access unsaved data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30557163/

相关文章:

python - python 中的插件管理器

javascript - 使用 Google App Engine 使用精简的 JS 构建网站以进行调试和生产

java - "Abstraction in Java."的正确定义是

java - 如何向 IntelliJ 添加源代码库(例如来自 Github)?

java - 在 web 应用程序外部连接 Spring bean 的最简单/正确的方法是什么?

eclipse - 如何使用 eclipse 在 testng 中设置 sysproperty?

java - Selenium 新的等待条件不起作用被迫使用 Thread.Sleep

c++ - 如何配置 Vim 插件 YouCompleteMe 来读取 Makefile?

jenkins - 具有多个用户的多个从站

java - EventQueue.isDispatchThread() 中的无限循环