【问题标题】:Eclipse RCP: programmatically associate file type with Editor?Eclipse RCP:以编程方式将文件类型与编辑器关联?
【发布时间】:2013-05-08 13:39:27
【问题描述】:

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

这就是 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

【问题讨论】:

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


    【解决方案1】:

    我知道您的问题是“以编程方式”提出的,但我会完整介绍这些方法。

    如果你正在编写提供编辑器的插件,那么你应该简单地在你的 plugin.xml 中声明扩展。

    如果您正在分发一个完整的 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);
    

    【讨论】:

      【解决方案2】:

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

      Eclipse 帮助文档

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

      然后搜索 org.eclipse.core.contenttype.contentTypes。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      相关资源
      最近更新 更多