简体   繁体   English

Eclipse插件-与文件扩展名和透视图相关的编辑器

[英]Eclipse plugin - Editor associated to file extension and perspective

I'm developing tow eclipse plugin, I have the next problem: 我正在开发两个日食插件,我有下一个问题:

I have two perspective that manages the same files. 我有两个角度来管理相同的文件。 I would like to make an association between file extension - editor - perspective. 我想在文件扩展名-编辑器-透视图之间建立关联。

I mean if I open the file extension .XXX in perspective 1 it uses the editor A, but if I open the same file extension .XXX in perspective 2, it uses the editor B. 我的意思是,如果我在透视图1中打开文件扩展名.XXX,它将使用编辑器A;但是,如果我在透视图2中打开相同的文件扩展名.XXX,它将使用编辑器B。

is it possible? 可能吗? Since now, I used the launcher but now I need more differentiation. 从现在开始,我使用了启动器,但是现在我需要更多的区别。

Thanks. 谢谢。

(Sorry, this is one of those "don't do that!" non-answers. :)) (对不起,这是“不要这样做!”的非回答之一。:))

As mentioned in the comments, I'd recommend against opening a different editor depending on the current perspective. 如评论中所述,我建议不要根据当前角度打开其他编辑器。 I think that goes against the expectations of the user, and has some unintuitive consequences, eg when I create my own perspectives. 我认为这违背了用户的期望,并且会带来一些不直观的后果,例如,当我创建自己的观点时。

I'd recommend going the path of Eclipse' XML/Plug-in manifest editors, for example. 例如,我建议您使用Eclipse的XML / Plug-in清单编辑器。 Tabs at the bottom allow the user to choose between the different views, independent of any perspective choice or configuration. 底部的选项卡允许用户在不同的视图之间进行选择,而与任何透视图选择或配置无关。

Eclipse插件清单编辑器

While I agree that this seems a little strange to have the default editor be different for the same file based on the open perspective, here is how you could do it. 虽然我同意,基于打开的角度对同一文件使用默认编辑器有所不同,这似乎有些奇怪,但是您可以按照以下方法进行操作。

  1. Create two new Content Type extensions 创建两个新的内容类型扩展
  2. Register your first editor as default editor for 1st new Content Type 将您的第一个编辑器注册为第一个新内容类型的默认编辑器
  3. Register your 2nd editor as the default editor for the 2nd new Content Type 将您的第二编辑器注册为第二新内容类型的默认编辑器
  4. For each content type, you have a 'content type describer'. 对于每种内容类型,您都有一个“内容类型描述程序”。 In these describer classes, have it check the active workbench page for the current perspective ID and if it matches the expected value, then VALID, if perspective id doesn't match, return INVALID. 在这些描述程序类中,让其检查活动工作台页面上的当前透视图ID,如果它与期望值匹配,则为VALID,如果透视图ID不匹配,则返回INVALID。
  5. For both editors you need to associate those editors with a content-type instead of a file-extension or filename 对于这两个编辑器,您都需要将这些编辑器与内容类型(而不是文件扩展名或文件名)相关联
  6. Now only one content type will match at a time depending on which perspective is open. 现在,根据打开的透视图,一次只能匹配一种内容类型。 Make sure that one of the content types is the 'default' so that it will always match if the user has some other perspective open. 确保其中一种内容类型是“默认”,以便在用户打开其他透视图时始终匹配。

Update #1 added some examples 更新#1添加了一些示例

There are some online tutorials for this. 有一些在线 教程 But here is some example code to make it easier to see what work is required. 但是这里有一些示例代码,使您可以更轻松地了解所需的工作。 Here is how you declare your content types (you would need two of them) 这是您声明内容类型的方式(您将需要两个)

<plugin>
   <extension
         point="org.eclipse.core.contenttype.contentTypes">
      <content-type
            base-type="org.eclipse.core.runtime.xml"
            describer="com.liferay.ide.core.FirstContentTypeDescriber"
            id="com.liferay.ide.core.contentType1"
            name="First Content Type"
            priority="normal">
      </content-type>
   </extension>
</plugin>

Then in the Describer class you would do your matching logic. 然后,在Describer类中,您将执行匹配逻辑。 Then in the editor extension point you reference a content type instead of a file-name or extension like this: 然后,在编辑器扩展点中,您引用内容类型,而不是像这样的文件名或扩展名:

   <extension
         point="org.eclipse.ui.editors">
      <editor
            class="com.liferay.ide.ui.FirstEditor"
            default="false"
            id="com.liferay.ide.ui.editor1"
            name="My First Editor">
         <contentTypeBinding
               contentTypeId="com.liferay.ide.core.firstContentType">
         </contentTypeBinding>
      </editor>
   </extension>

I would recommend to rethink your approach, and take some cues from WindowBuilder: have one editor associated with the file type which opens a tabbed editor; 我建议重新考虑您的方法,并从WindowBuilder中获取一些提示:让一个与文件类型相关联的编辑器打开一个选项卡式编辑器; if a second plugin is added, have it create a separate tab on the same editor. 如果添加了第二个插件,请在同一编辑器上创建一个单独的选项卡。

Other option may be programmatically change file type association with Java code shown in 其他选项可能是通过编程方式更改文件类型与Java代码的关联,如

Eclipse RCP: programmatically associate file type with Editor? Eclipse RCP:以编程方式将文件类型与编辑器关联?

Then there is only a question how to execute that code on perspective change event. 然后只有一个问题,如何在透视图更改事件上执行该代码。

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

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