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

需求描述:

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

实现片段:

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

        <editor
              name="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;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
 
File fileToOpen = new File("externalfile.xml");
 
if (fileToOpen.exists() && fileToOpen.isFile()) {
    IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
    IWorkbenchPage page = PlatformUI.getWorkbench().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.fromOSString(inJsonLocalFile.getAbsolutePath());
	IFileStore inJsonLocation = EFS.getLocalFileSystem().getStore(inJsonLocalPath);
	FileStoreEditorInput inJsonEditorInput = new FileStoreEditorInput(inJsonLocation);

   如果报错提示说EFS这个类找不到,记得在dependencies中加入‘org.eclipse.core.filesystem’,如果已经存在,还是报EFS类找不到,删除它重新加入'org.eclipse.core.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的所有内容。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值