Eclipse RCP:以编程方式将文件类型与编辑器相关联? [英] Eclipse RCP: programmatically associate file type with Editor?

查看:273
本文介绍了Eclipse RCP:以编程方式将文件类型与编辑器相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式将文件类型与编辑器相关联?

这是Eclipse-RCP Java代码可以使用以下UI进行归档互动:

That is what Eclipse-RCP Java code can do what is archived with the following UI interaction:

Window -> Preferences
General -> Editors -> File Associations
Add... > File type: *.json
Select *.json file type 
Add... (Associated editors) > JavaScript Editor
Make it default

升级到Q

https://stackoverflow.com/questions/12429221/ eclipse文件关联确定编辑器的关联编辑器

Eclipse:将编辑器与内容类型相关联

获取Eclipse编辑器的相关文件扩展名

打开一个默认编辑器,在treeviewer选择eclipse rcp(例如:作为eclipse知道那个j.java文件必须在文本编辑器中打开)

xml配置文件的eclipse rcp更改图标

推荐答案

我知道您的问题以编程方式表示,但我会彻底地运行这些方法。

I know your questions says "programmatically" but I'll give a complete run down of the methods.

如果您正在编写提供编辑器的插件,那么您应该只是在plugin.xml中声明扩展名。

If you are writing the plugin that provides the editor, then you should simply declare the extension in your plugin.xml.


   <extension
         point="org.eclipse.ui.editors">
      <editor
            ...
            extensions="json"
            ...

如果您正在分发一个完整的RPC应用程序,您可以编辑插件的plugin.xml,该插件可以提供编辑器,也可以添加一个仅适用于该编辑器的插件。

If you are distributing a complete RPC application, you can edit the plugin.xml for the plugin that provides the editor or add a plugin that just refers to that editor.

但是,如果您必须以编程方式执行操作,则操作RPC实例的内部。 Eclipse不提供公共API,但是这个代码将会执行:

But, if you have to do it programmatically, you are manipulating the internals of an RPC instance. Eclipse does not provide a public API for that but this code will do it:

            // error handling is omitted for brevity
    String extension = "json";
    String editorId = "theplugin.editors.TheEditor";

    EditorRegistry editorReg = (EditorRegistry)PlatformUI.getWorkbench().getEditorRegistry();
    EditorDescriptor editor = (EditorDescriptor) editorReg.findEditor(editorId);
    FileEditorMapping mapping = new FileEditorMapping(extension);
    mapping.addEditor(editor);
    mapping.setDefaultEditor(editor);

    IFileEditorMapping[] mappings = editorReg.getFileEditorMappings();
    FileEditorMapping[] newMappings = new FileEditorMapping[mappings.length+1];
    for (int i = 0; i < mappings.length; i++) {
        newMappings[i] = (FileEditorMapping) mappings[i];
    }
    newMappings[mappings.length] = mapping;
    editorReg.setFileEditorMappings(newMappings);

这篇关于Eclipse RCP:以编程方式将文件类型与编辑器相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆