Eclipse插件开发,将不同的编辑器与同一文件扩展名相关联

时间:2011-02-10 01:56:59

标签: java eclipse eclipse-plugin

我正在开发一个eclipse插件,它将某个编辑器与特定的文件扩展名相关联,比如“.abc”。

问题是我想将.abc文件与该编辑器关联,仅用于我自己的项目。 就像现在一样,无论在哪个项目中,它都将始终使用该编辑器打开.abc文件。

如果“.abc”文件属于我自己的项目,我怎样才能打开自己的编辑器?

1 个答案:

答案 0 :(得分:8)

您需要使用content-type扩展点定义org.eclipse.core.contenttype。然后,您需要将编辑器与特定内容类型(而不是文件扩展名)相关联。

接下来,您需要将项目性质与刚刚定义的内容类型相关联。

在具有特定性质的项目之外时,您可能还需要创建应该用于文件的第二种内容类型。

这是我们在Groovy-Eclipse中使用的一个示例,因此默认情况下在groovy项目中使用groovy编辑器打开* .groovy文件,但是通过groovy项目之外的文本编辑器:

 <extension point="org.eclipse.core.contenttype.contentTypes">
    <content-type
       base-type="org.eclipse.jdt.core.javaSource"
       file-extensions="groovy"
       id="groovySource"
       name="Groovy Source File (for Groovy projects)"
       priority="high"/>

    <content-type
       base-type="org.eclipse.core.runtime.text"
       file-extensions="groovy"
       id="groovyText"
       name="Groovy Text File (for non-Groovy projects)"
       priority="low"/>
</extension>

<extension
     id="groovyNature"
     name="Groovy Nature"
     point="org.eclipse.core.resources.natures">
  <runtime>
     <run class="org.codehaus.jdt.groovy.model.GroovyNature"/>
  </runtime>
  <requires-nature id="org.eclipse.jdt.core.javanature"/>
  <content-type
        id="org.eclipse.jdt.groovy.core.groovySource">
  </content-type>

在这里,我们为groovy项目定义groovySource,为非groovy项目定义groovyText。另请注意,内容类型的优先级不同。

然后,在其他地方,我们将GroovyEditor与groovySource内容类型相关联。

相关问题