【问题标题】:Eclipse: Associating editors with natureIdEclipse:将编辑器与 natureId 关联
【发布时间】:2014-07-07 17:04:50
【问题描述】:

是否可以将编辑器与项目 natureId 关联?我有一个项目,其中包含具有不同文件扩展名的文件,但它们使用相同的编辑器进行编辑。

我试图通过将扩展类型留空来将内容类型与 natureId 和编辑器匹配。不幸的是它不起作用或者我做得不对:(

如果可能的话,也可以覆盖项目的默认编辑器!?

到目前为止我已经尝试过:

  <extension
    point="org.eclipse.core.contenttype.contentTypes">
 <content-type
       describer="test.FOPContentDescriber"
       id="test.content.type"
       name="FO Files"
       priority="high">
 </content-type>
</extension>
<extension
     point="org.eclipse.ui.editors">
  <editor
        class="test.editors.FOEditor"
        contributorClass="test.editors.FOEditorContributor"
        default="false"
        icon="icons/fo_editor_16.png"
        id="test.editors.FOEditor"
        name="%editor.name">
     <contentTypeBinding
           contentTypeId="test.content.type">
     </contentTypeBinding>
  </editor>

内容描述器类:

package test.fop;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.content.IContentDescriber;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.core.runtime.content.ITextContentDescriber;


public class FOPContentDescriber implements IContentDescriber {

    public FOPContentDescriber() {
        System.out.println("init content describer");
    }

    @Override
    public int describe(InputStream contents, IContentDescription description) throws IOException {
        System.out.println("Call describer");
        return ITextContentDescriber.VALID;
    }

    @Override
    public QualifiedName[] getSupportedOptions() {
        return IContentDescription.ALL;
    }

}

【问题讨论】:

  • 使用的编辑器由文件内容类型决定,如果没有定义内容类型,则由扩展名决定。项目没有默认编辑器。
  • 每个文件都包含我可以识别的格式...文件内容有扩展点吗?

标签: java eclipse eclipse-plugin editor


【解决方案1】:

您可以使用org.eclipse.core.contenttype.contentTypes 扩展点定义文件内容类型。

您可以使用内容类型定义的describer 元素来指定实现IContentDescriber 的类。将调用此类以检查文件是否对内容类型有效。描述器还可以从文件中提取信息并将其存储在内容描述中。

注意:内容描述器仅在输入流中给出,无法找到文件路径。

当您使用 org.eclipse.ui.editors 扩展点定义编辑器时,您可以使用 contentTypeBinding 元素将编辑器绑定到内容类型。

【讨论】:

  • 我也尝试过这样做。请参阅上面的编辑... FOEditorDescriber 类根本没有被调用,我不明白为什么。
  • 我认为您需要为content-type 指定file-extensions 属性,它不会只在所有内容上尝试描述器。
  • 我也尝试过使用文件扩展名,但它不起作用......问题是我想通过内容而不是文件扩展名来识别编辑器,因为文件扩展名不幸的是随机的。
  • 不幸的是你是对的。如果设置了文件扩展名,它会调用描述器。
猜你喜欢
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多