简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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