programmatically实现打开workspace以外的files对应的editor

阅读: 评论:0

programmatically实现打开workspace以外的files对应的editor

programmatically实现打开workspace以外的files对应的editor

需求描述:

    实时地远程获得json格式的内容,在本地以可编辑模式打开供用户修改并保存至远程服务器

实现片段:

    首先将获得的json内容保存至.metadata目录下,比如保存为in.json,然后在l中定义in.json默认的editor:

        <editorname="In/Out Json Editor"icon="icons/json.gif"class="myplugin.editors.inout.InOutJsonEditor"matchingStrategy= "myplugin.editors.inout.InOutJsonEditorMatchingStrategy"id="myplugin.editors.inout.InOutJsonEditor"></editor>
      然后需要在eclipse的editor中打开非workspace中的file:

import java.io.File;
filesystem.EFS;
filesystem.IFileStore;
lipse.ui.PartInitException;
lipse.ui.IWorkbenchPage;
lipse.ui.PlatformUI;
lipse.ui.ide.IDE;File fileToOpen = new File(&#l");if (ists() && fileToOpen.isFile()) {IFileStore fileStore = LocalFileSystem().URI());IWorkbenchPage page = Workbench().getActiveWorkbenchWindow().getActivePage();try {IDE.openEditorOnFileStore( page, fileStore );} catch ( PartInitException e ) {//Put your exception handler here if you wish to}
} else {//Do something if the file does not exist
}

   注意这里是如何使用EFS将Java的File类型转换为Eclipse workspace中需要的IResource类型

	File inJsonLocalFile = new File(jsonDir);IPath inJsonLocalPath = Path.AbsolutePath());IFileStore inJsonLocation = LocalFileSystem().getStore(inJsonLocalPath);FileStoreEditorInput inJsonEditorInput = new FileStoreEditorInput(inJsonLocation);

   如果报错提示说EFS这个类找不到,记得在dependencies中加入‘filesystem’,如果已经存在,还是报EFS类找不到,删除它重新加入&#filesystem'

非本地保存的实现方式:

    有没有可能不保存远程得到的string content至本地文件,直接在eclipse的editor中打开呢?其实是可以的,只不过在IWorkbenchPage的openEditor(input, editorID)方法中传入的第一个参数input不是IFileEditorInput类型,而是IStorageEditorInput类型

    具体可以参照:How do I open an editor on something that is not a file

这里的StringInput实现了接口IStorageEditorInput,所有的内容可以传入StringInput对象,然后将StringInput作为input参数传入input,那么editorID对应的editor就可以得到input的所有内容。

本文发布于:2024-02-05 06:40:31,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170726492963962.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23