try to avoid multiple instances of eclipse editor opening same file - eclipse

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.

Related

In an eclipse plugin: How can I programmatically highlight lines of codes in the java editor?

I am trying to develop an eclipse plugin that does some documentation check on java code and highlights some lines of code in the editor.
To achieve my goal, I DON'T want to create a new editor in eclipse, I simply want to extend the default java editor to draw a line under (or highlight) the methods that do not satisfy some set of predetermined requirements.
Do I need to create a PresentationReconciler? If yes, how do I make the JDT or workbench use my reconciler.
I have never done plugin development and this is my first attempt.
Several starting points for you:
Annotations are an UI feature of JFace's text editor that allows you to visually mark some places in an open editor.
Markers are a Workbench feature, more high-level. They are generic "objects that may be associated with Workbench resources", and they can display in several places: in text editors (as annotations) or in the Problems view, for example.
Depending on what you want to do, you would plug in your plug-in into extension points related to either of those.
The Eclipse Java editor is located in the org.eclipse.jdt.internal.ui.javaeditor.JavaEditor package.
The "internal" in the package name means that the Eclipse development team can change how the Java editor works with new revisions.
Try this help page: Juno Help on syntax highlighting
At the end of the page, it describes how to dynamically add a PresentationReconciler, which is used for syntax highlighting. See if that fits the problem that you want to solve.
I assume you already have a plugin project.
In your plugin.xml, open the tab Extensions, click Add..., search for org.eclipse.ui.editors, then you should see a template named Editor, which will produce a simple xml editor to experiment and play with. Also, you will be able to see the needed structure to define a custom editor.
Hope this helps...
I don't know if you still have a need for this, but you are going to want to use Annotations to keep track of what parts of the editor you need to highlight.
For actually doing the graphical effect of highlighting, you could do syntax highlighting via a PresentationReconciler, but I have no experience with that.
We used a technique we borrowed from http://editbox.sourceforge.net/, replacing the background image of the editor Shell. Its open source, so check it out. (Our code might also help -- its at https://github.com/IDE4edu/EclipseEditorOverlay )

Jface TreeViewer nodes Link with files

I writing an eclipse plugin to analyze files for some defects, I get the list of java files, their classes and methods in a jface tree view, I need to open the file in the editor when I double click on the file node of the tree view.
please help me with this.
thanks,
Shasinda
You should either use a common navigator framework with corresponding filters that already has the link and open function built-in for Eclipse Resources, however, is much trickier to set up - see the blog post series starting with http://cvalcarcel.wordpress.com/2009/07/08/writing-an-eclipse-plug-in-part-1-what-im-going-to-do/
Alternatively, you could add a double click listener that opens an editor (or checks existing editors of the current workbenchpage) by looking the open editors. For the basic idea, see the corresponding Eclipse FAQ entry.
Use IDE.openEditor to open the editor.

different editors for different perspectives in Eclipse [duplicate]

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.

How to capture CTRL+CLICK in a custom Text Editor

I got a custom editor by subclassing TextEditor in Eclipse plugin. I am trying to implement a CTRL+CLICK action(like 'open declaration' in java editor) in my custom editor. But I cannot figure out how to capture CTRL+CLICK. I tried to add a KeyListener to the editor's sourceViewer in its constructor or initialzeEditor(), which didn't work. Has anyone have an idea how to do this?
Thank.
Take a look at
extension point 'org.eclipse.ui.workbench.texteditor.hyperlinkDetectors'
classes org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector, org.eclipse.jface.text.hyperlink.IHyperlinkDetector and org.eclipse.jface.text.hyperlink.IHyperlink
If you need detailed examples, then take a look at plugin.xml of org.eclipse.jdt.ui. (You can get the source for org.eclipse.jdt.ui either from CVS or from Eclipse Classic SDK install)

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.