gpt4 book ai didi

Eclipse RCP : programmatically associate file type with Editor?

转载 作者:行者123 更新时间:2023-12-02 15:16:11 27 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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

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.

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 : programmatically associate file type with Editor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15877123/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com