3

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.

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
Rafael
  • 31
  • 3
  • 1
    Are you sure this conforms to the UI guidelines for perspectives? I'm not sure this is what a user would expect, I've never seen a different editor being opened from the same action, just based on the current perspective. Also, I'm creating a lot of perspectives myself. How would I choose which editor to use in each one of them? – Henrik Heimbuerger Jul 28 '11 at 09:55
  • I have 2 plugins (one perspective per plugin). One is for dessign with a gef editor and the other one is for develop using the dessign. Some user would have both plugins installed and they need to open the same file with both editors (depend if they are in the dessign perspective or in the developer perspective) – Rafael Jul 28 '11 at 10:08
  • This makes sense for JavaScript Editor, that maybe different for browser and server side. Voted up for the questioned. Currently not really answered. – Paul Verest Apr 27 '13 at 16:08
  • Is it possible to have handler for an event like "Perspective is changed", than in this event change default Editor associated with this extension programmatically? Are better ways to do? – Paul Verest Dec 26 '13 at 06:29

4 Answers4

4

(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

Henrik Heimbuerger
  • 9,924
  • 6
  • 56
  • 69
2

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>
gamerson
  • 5,220
  • 1
  • 28
  • 45
  • Could you please link some related Docs. For example what 'content type describer' should be like, and what is Content Type ? I just know – Paul Verest Dec 26 '13 at 08:02
  • I added a few links and for the ContentTypeDescriber, the extension point doc gives you the class that you need to implement, the ITextContentDescriber is the one you want to implement. However, you may have a hard time determining if your file is the right content unless your files have a very specific signature (like XML doctype, etc). – gamerson Dec 26 '13 at 09:25
  • Files are JavaScript .js files. The problem is that they maybe in web or server side (like Node.js, MongoDB) context. – Paul Verest Dec 26 '13 at 09:41
  • The main problem is the ContentDescriber API doesn't let you have access to the IFile. But you can use reflection to grab it like we did here: https://github.com/liferay/liferay-ide/blob/8e46a833c58fd2397bdab2798e5806ac5e23fb68/common/plugins/com.liferay.ide.core/src/com/liferay/ide/core/describer/LiferayLanguagePropertiesFileDescriber.java#L53-74 – gamerson Dec 26 '13 at 10:04
  • Having read more about Content Type, I don't think it is the way for .js files, as JSDT is not using this framework. – Paul Verest Dec 26 '13 at 10:25
  • 1
    Actually take a look at org.eclipse.wst.jsdt.core/plugin.xml line 193, you will see there is a content-type for JS files. Then if you look at the JavaScriptEditor it will be tied to org.eclipse.wst.jsdt.core.jsSource content type. So you could use this framework to create 2 new content types that both extend the existing jsSource content type. – gamerson Dec 27 '13 at 06:43
  • Link for references https://github.com/eclipse/webtools.jsdt.core/blob/master/bundles/org.eclipse.wst.jsdt.core/plugin.xml#L183-205 – Paul Verest Dec 27 '13 at 09:06
1

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.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
0

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.

Community
  • 1
  • 1
Paul Verest
  • 60,022
  • 51
  • 208
  • 332