different editors for different perspectives in Eclipse [duplicate] - eclipse

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, 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.
Create two new Content Type extensions
Register your first editor as default editor for 1st new Content Type
Register your 2nd editor as the default editor for the 2nd new Content Type
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.
For both editors you need to associate those editors with a content-type instead of a file-extension or filename
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.

Related

Eclipse add type hierarchy to under opened files

I have two laptops and on one of them in Eclipse I was able to add a type hierarchy that shows class members, etc. directly under the opened files (or whatever you call the tabs that are right about the red box in my image). I have no idea how to do this though, anyone have any suggestions?
Also, on a related note, when I do just open the type hierarchy it always requires that I open it given the context of the current opened .java file. On my other laptop it's sort of dynamic and allows me to explore the type hierarchy of whatever file I'm viewing. I want it all to work like how it is in Microsoft Visual Studio.
I think you mean the breadcrumb - Navigate > Show in Breadcrumb

try to avoid multiple instances of eclipse editor opening same file

I registered a custom editor with eclipse extension point "org.eclipse.ui.editors" as follows.
<extension
point="org.eclipse.ui.editors">
<editor
class="com.xxx.designer.scxml.ui.ScxmlDiagramEditor"
id="com.xxx.designer.scxml.ui.ScxmlDiagramEditor"
extensions="scdiagram"
name="SCXML Editor">
</editor>
It works fine, this editor is associated with .scdiagram files. However, every time I click on one same .scdiagram file, it opens a new instance for me instead of highlighting the opened file. Any idea on this?
Thanks,
Jie
All you need to do is make your IEditorInput return true for the same files. You could implement this by comparing the canonical names of your files. Don't forget to also override the hashCode() method. What issues should be considered when overriding equals and hashCode in Java?
See Lars Vogel's excellent tutorial Eclipse Editor Plugin Tutorial for more details on using Editors.
Here's how my question was resolved eventually. It's related to Graphiti and solution is also tied to the framework.
Since I subclassed Graphiti's diagram editor, all I need to do is to set org.eclipse.graphiti.ui.editor.DiagramEditorMatchingStrategy as the editor's matching strategy in plugin.xml.

Eclipse file associations for extensions with multiple periods

I have several Mako templates in my project that are named things like header.html.tmpl and settings.py.tmpl. I would like to add file associations to Eclipse to open these with the appropriate editors. For example, I would like header.html.tmpl to be opened in the HTML editor, settings.py.tmpl to be opened in the Python editor, etc. I go to Preferences->File Associations and I try adding *.py.tmpl to the list but after I click "Ok" nothing happens and the desired extension does not appear in the list. If I try instead using .py.tmpl I can add it to the list and add the Python editor to its associated editors but when I double-click a .py.tmpl file it doesn't use the correct editor.
Obviously it would probably work to just use -tmpl instead of .tmpl, but I'm wondering if anyone knows a way around this or can confirm that it's a bug/missing feature.
Its a bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=168573

Eclipse: associate an editor with a content type

Can I change editor associations based on the content type rather than individual file types?
If I go to Window -> Preferences -> General -> Editors -> File Associations, I see a list of file types based on the file extension.
Clicking on "*.py" for example, the Associated Editors box lists editors that are "locked by 'Python File' content type".
I can see the message at the top that says I can "See 'Content Types' for content-type based associations", but that section of the preferences (General -> Content Types) only shows file extensions associated with the content types, not editors.
Since the hierarchy of content types is organized in a way that most files are under the "Text" content type, one of the things I'd like to do is add gvim as an external editor available for Text/* files rather than go to each file type individually and add the editor.
Thank you.
The only way I found to do that is by installing a plugin into your install that has the editor-to-content-type binding. In this example, I make Gedit available for the text content type:
<extension point="org.eclipse.ui.editors">
<editor command="gedit"
default="false"
id="z.ex.rcp.mail.gedit"
name="Gedit">
<contentTypeBinding contentTypeId="org.eclipse.core.runtime.text"/>
</editor>
</extension>
See http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/extension-points/org_eclipse_ui_editors.html for more details.
Having the same problem: if the editor is choosen by "content-type", you can not change the default editor or add a new one: the content-type screen has no option to edit that.
But, I discovered that you can configure (all) your extension(s) in your "File Associations". Eclipse will add automatically the editors (based on the Content-Type screen) AND You will then be able to add another editor and/or to change the default used one.
This is based only on a very short experience, and I didn't find any supporting documentation, but I hope it can help anybody...
This is now natively possible fro the content-type preference page, since Eclipse Photon (2018) https://www.eclipse.org/eclipse/news/4.8/platform.php#content-type-editor-association

How do I open my editor while I am selecting a file in project explorer?

When i am selecting a file in the project explorer, it should allow me to open my editor instead it shows text editor.How can we handle this programmatically in eclipse plugin development ?
regards
Mathan
Window - Preferences - General - Editors - File Associations
Their you can select which editor is open, for the different file endings.
When you want to do this with a self implemented editor you have to implement the extension poin for editors
org.eclipse.ui.editors
The field "extensions" defines which file ending is associated to the editor
There is also possibility to register your own content type of file. It can be registered as a sub-type of existing content type i.e. XML. To do that you should add an extension point of org.eclipse.core.contenttype.contentTypes
Sample extension point:
<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
base-type="org.eclipse.core.runtime.xml"
default-charset="UTF-8"
describer="com.example.MyContentDescriber"
file-extensions="xml"
id="org.eclipse.core.runtime.xml.exampleContentType"
name="exampleContentType"
priority="normal">
</content-type>
As you can see new content type is extending an xml content type. The next step is to implement content describer, which looks into the file and determines if it is of your type. There are two interfaces you can implement to do the job: IContentDescriber or ITextContentDescriber. Path to your implementation of describer must be specified in content type as it is shown in the snippet.
Then your editor can be declared as the one, who will be handling your kind of content types.
<editor class="com.example.MyEditor"
default="true"
icon="res/icons/dialog.png"
id="[some_id]"
name="Dialog editor">
<contentTypeBinding
contentTypeId="org.eclipse.core.runtime.xml.exampleContentType">
</contentTypeBinding>
</editor>
Important things:
files that have been already opened in eclipse might 'remember' in which editor they were last opened and this one will be chosen,
your describer might be used a lot so you should consider performance issues while implementing it
More in the topic:
ECLIPSE: Contributing content types
Any plugin that contributes an editor to the workbench can associate an extension the editor understands in their plugin.xml.
The extensions attribute describes the file types that the editor understands. (You could also specify filenames if you need to be more specific.)
I believe this is enough to make your editor open when you click a file of your extension. (at least be preferred the first time you open the file. i.e. eclipse will prefer the last used editor for that file that session).
More info.
Using the file extension method is preferred because that's easy. However if you can't do that, then the only alternative I know of is to provide an action provider in the Common Navigator configuration.
You will need to subclass CommonActionProvider and in the fillContextMenu() method you can look at the resource and then decide to have your own Open action (for your editor) or add the standard Open action.