Eclipse plugin - Editor associated to file extension and perspective

旧城冷巷雨未停 提交于 2019-12-01 11:18:25

(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, e.g. when I create my own perspectives.

I'd recommend going the path of Eclipse' XML/Plug-in manifest editors, for example. Tabs at the bottom allow the user to choose between the different views, independent of any perspective choice or configuration.

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.
  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

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. 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; if a second plugin is added, have it create a separate tab on the same editor.

Paul Verest

Other option may be programmatically change file type association with Java code shown in

Eclipse RCP: programmatically associate file type with Editor?

Then there is only a question how to execute that code on perspective change event.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!