Eclipse plugin - Editor associated to file extension and perspective

前端 未结 4 1920
不思量自难忘°
不思量自难忘° 2021-01-15 13:13

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 ext

相关标签:
4条回答
  • 2021-01-15 13:43

    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>
    
    0 讨论(0)
  • 2021-01-15 13:44

    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.

    0 讨论(0)
  • 2021-01-15 13:49

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

    Eclipse plugin-in manifest editor

    0 讨论(0)
  • 2021-01-15 14:02

    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.

    0 讨论(0)
提交回复
热议问题