简体   繁体   English

当文件包含特定文本时打开定制编辑器(Eclipse RCP)

[英]Opening custom editor when a file contains an specific text (Eclipse RCP)

I'm creating some editors for a simple project, so far everything is fine, except that at the moment, my editors are bound to an specific file extension, for example, one editor opens xml files, other editor opens .properties files, and that's fine for now, but what I need to do, is to launch the editor just when the opened file contains an specific string. 我正在为一个简单的项目创建一些编辑器,到目前为止,一切都很好,但是目前,我的编辑器已绑定到特定的文件扩展名,例如,一个编辑器打开xml文件,另一个编辑器打开.properties文件,以及暂时可以,但是我需要做的是仅在打开的文件包含特定字符串时启动编辑器。 I'm actually trying to launch my editors when the project version is "3.4", so, when the files contains something like this: version="3.4" , it should launch my editor, is there a way to do that, or is there a better way to to that? 我实际上是在尝试将项目版本为“ 3.4”时启动编辑器,因此,当文件包含以下内容时: version="3.4" ,它应该启动我的编辑器,有没有办法做到这一点,或者是有更好的方法吗?

Notice that the project version is specified in a project facet as well. 注意,项目版本也在项目构面中指定。

You can add a describer element as a child of content-type which defines a class to be called which can check if a file is valid for this content type. 您可以添加一个describer元素作为content-type的子元素,该元素定义要调用的类,该类可以检查文件是否对该内容类型有效。

<content-type id="customType"
   base-type="org.eclipse.jst.jsf.facesconfig.facesConfigFile"
   file-extensions="xml">
  <describer class="package.MyDescriber"/>
</content-type>

The class must implement IContentDescriber (or ITextContentDescriber ). 该类必须实现IContentDescriber (或ITextContentDescriber )。

The method: 方法:

public int describe(InputStream contents, IContentDescription description)

is called to check the contents. 称为检查内容。 Return VALID if it is OK, INVALID if not. 如果可以,则返回VALID否则返回INVALID

You can also support other properties for the file (such as determining the character encoding) if needed. 如果需要,您还可以支持文件的其他属性(例如确定字符编码)。

More information in the Eclipse help on the extension point. Eclipse帮助中有关扩展点的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM