Eclipse RCP:以编程方式将文件类型与编辑器关联?

时间:2013-04-08 10:44:03

标签: eclipse eclipse-plugin editor eclipse-rcp file-association

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

这就是Eclipse-RCP Java代码可以执行以下UI交互存档的内容:

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-file-associations-determine-which-editor-in-list-of-associated-editors
Eclipse: associate an editor with a content type
Get associated file extensions for an Eclipse editor
Opening a default editor, on a treeviewer selection eclipse rcp(eg: as eclipse knows that j.java files must be opened in text editor)

eclipse rcp change icon for xml configuration file

2 个答案:

答案 0 :(得分:4)

我知道你的问题是“以编程方式”说的,但我会彻底了解这些方法。

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

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

如果要分发完整的RPC应用程序,可以编辑提供编辑器的插件的plugin.xml,或者添加一个仅引用该编辑器的插件。

但是,如果你必须以编程方式执行它,那么你正在操纵RPC实例的内部。 Eclipse没有为此提供公共API,但此代码将执行此操作:

            // 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);

答案 1 :(得分:1)

关联文件类型意味着将您的编辑器内容与预定义的内容相关联。 这可以通过plugin.xml轻松实现。 只需按照以下链接: -

Eclipse帮助文档

http://help.eclipse.org/indigo/index.jsp

并搜索org.eclipse.core.contenttype.contentTypes。