close Eclipse editor if file is open twice - eclipse

I´m creating my own editor here for Eclipse and found a problem.
If I open a file and it´s already open, Eclipse opens a new editor.
So, I need to either avoid this or close the editor right after it´s opened.
My editor class is a child of MultiPageEditorPart and it has 2 tabs: first one is a Java editor and second one is a text editor. Java editor opens up a .java file and text editor opens up my own file.
I saw some posts saying about how to fix this, but I don´t implement IEditorInput interface anywhere here.
Can anyone help me with this?
Thanks a lot
Here´s my editor definition:
<extension point="org.eclipse.ui.editors">
<editor id="br.com.senior.wb.asas.editor.AsasEditor"
class="br.com.senior.wb.asas.editor.AsasEditor"
contributorClass="br.com.senior.wb.asas.editor.AsasEditorContributor"
extensions="java, afm"
icon="icons/editor_asas.png" name="Editor ASAS">
</editor>
</extension>

If you mean you want to open one editor when either the java or afm file is opened when the other file is already open then you need to use the matchingStrategy attribute of the editor definition to define an editor matching strategy.
Something like:
<extension point="org.eclipse.ui.editors">
<editor id="br.com.senior.wb.asas.editor.AsasEditor"
class="br.com.senior.wb.asas.editor.AsasEditor"
contributorClass="br.com.senior.wb.asas.editor.AsasEditorContributor"
matchingStrategy="br.com.senior.wb.asas.editor.AsasEditorMatchingStrategy"
extensions="java, afm"
icon="icons/editor_asas.png" name="Editor ASAS">
</editor>
</extension>
public class AsasEditorMatchingStrategy implements IEditorMatchingStrategy
{
public boolean matches(IEditorReference editorRef, IEditorInput input)
{
if (!(input instanceof IFileEditorInput))
return false;
IFile inputFile = (IFile)input.getAdapter(IFile.class);
if (inputFile == null)
return false;
IFile currInputFile = (IFile)editorRef.getEditorInput().getAdapter(IFile.class);
if (currInputFile == null)
return false;
if (!inputFile.getProject().equals(currInputFile.getProject()))
return false;
// TODO add more checks that 'inputFile' and 'currInputFile' are a matching pair of files
}

Related

Eclipse RCP: New Editor Wizard

I have a small experience with Eclipse RCP - 3.X and I created my own editor via org.eclipse.ui.editors extension point and in order to have multiple instance of that editor I have implemented a new editor wizard as you can see below;
IFile file = page1.createNewFile();
IWorkbenchWindow window = _workbench.getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
try {
IDE.openEditor(page, file, SimpleEditor.ID, true);
} catch (PartInitException e) {
e.printStackTrace();
}
My question, the only way I found is that, create a new file and associate that file with your unique Editor ID. What I want is, I would like parse some initial values -that defined by user- to editor from the wizard. But we really don't instantiate the EditorPart class in the process.
How can I achieve that?
The IDE.openEditor call returns the IEditorPart that was opened - this will be an instance of your editor class so you can do:
IEditorPart part = IDE.openEditor(page, file, SimpleEditor.ID, true);
if (part instanceof SimpleEditor) {
SimpleEditor editor = (SimpleEditor)part;
// TODO call methods you define in the editor to set the parameters
}
Alternatively you can use a custom IEditorInput and call the IDE
openEditor(IWorkbenchPage page,
IEditorInput input, String editorId)
method. The init method of the editor is given the IEditorInput you specify.

Annotations in custom Eclipse text editor don't show up

I'm trying to write a custom text editor with a reconciler that will update the AST and create markers for the errors, if there are any. I managed to add markers that show up in the left ruler of the editor and in the problems view. However, I also want those errors to be underlined in the text. That doesn't work. None of the text is underlined. If I double click on one of the errors in the problem view however, the corresponding text in the text editor is selected. As far as I understand, I need to add an annotation in addition to the marker. This is what I have tried so far:
final IResource resource = ResourceUtil.getResource(getEditorInput());
final IMarker marker = resource.createMarker("com.test.myproblemmarker");
marker.setAttribute(IMarker.MESSAGE, "hello");
marker.setAttribute(IMarker.CHAR_START, 2);
marker.setAttribute(IMarker.CHAR_END, 10);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
final IDocumentProvider idp = getDocumentProvider();
final IDocument document = idp.getDocument(getEditorInput());
final IAnnotationModel iamf = idp.getAnnotationModel(getEditorInput());
final SimpleMarkerAnnotation ma = new SimpleMarkerAnnotation("org.eclipse.ui.workbench.texteditor.error", marker);
ma.setText("hello");
iamf.connect(document);
iamf.addAnnotation(ma, new Position(2, 8));
ma.update();
iamf.disconnect(document);
ma.update();
The relevant parts from plugin.xml:
<extension id="com.test.problemmarker" point="org.eclipse.core.resources.markers" name="A Problem">
<super type="org.eclipse.core.resources.problemmarker"/>
<super type="org.eclipse.core.resources.textmarker"/>
<persistent value="true"/>
</extension>
<extension point="org.eclipse.ui.editors.markerAnnotationSpecification" id="myannotationspecification" name="MyAnnotation">
<specification
annotationType="com.test.problemannotation"
label="MyAnnotation"
icon="icons/icon16x16.png"
overviewRulerPreferenceKey="clruler"
overviewRulerPreferenceValue="true"
colorPreferenceKey="clcolor"
colorPreferenceValue="255,0,0"
textPreferenceKey="cltext"
textPreferenceValue="true"
verticalRulerPreferenceKey="clvertical"
verticalRulerPreferenceValue="true"
textStylePreferenceKey="clstyle"
textStylePreferenceValue="UNDERLINE"
presentationLayer="4">
</specification>
</extension>
<extension point="org.eclipse.ui.editors.annotationTypes">
<type
markerSeverity="2"
super="org.eclipse.ui.workbench.texteditor.error"
name="com.test.problemannotation"
markerType="com.test.problemmarker"/>
</extension>
The custom text editor inherits from TextEditor and the reconciler is a MonoReconciler. Seems like a pretty standard setup, when I look at existing editor / reconciler implementations. I appreciate any help!
Ok, I was able to fix it myself. The problem was, that I set my own preference store in the constructor of the TextEditor subclass:
setPreferenceStore(MyPlugin.getDefault().getPreferenceStore());
I set a few breakpoints in the AnnotationPainter class and realized that all
annotations were disabled. When I removed this line from the editor, the annotations appeared.
I also noticed that it is sufficient to just create a marker. A corresponding annotation will be added automatically.

how to call contexts.xml file in eclipse plugin

I have created an eclipse plug-in with a view project. I have a contexts.xml file and i have configured it. Please refer the following code.
<contexts>
<context id="Help" title="Plug-in Help">
<description>context help for the sample view</description>
<topic href="resources/text.html" label="Context-sensitive help">
</topic>
</context>
</contexts>
I have an html file named "text" under resources folder inside the plugin project.
//Listener to invoke the help method of RepositoryAccessor class
bHelp.addMouseListener(new MouseAdapter() {
#Override
public void mouseDown(MouseEvent arg0){
Display display=PlatformUI.getWorkbench().getDisplay();
Shell shell = new Shell(display);
GridLayout grid11=new GridLayout(3,true);
//Layout of controls inside the plugin view
shell.setLayout(grid11);
Text text = new Text(shell, SWT.NONE);
PlatformUI.getWorkbench().getHelpSystem().setHelp(text,"help");
PlatformUI.getWorkbench().getHelpSystem().displayHelp("help");
}
}
bHelp is a button. Once i run the eclipse plugin and click the bHelp button, i get a new shell window and i see only empty label.
Please suggest a method to assign the html contents to the label created in the popup window(new shell).
At Step 1, i click Help Icon and a new shell is open. In Step 2, the label is still showing "sfsf" instead of the contents in "text.html".
You have to use the org.eclipse.help.contexts extension point to tell Eclipse about your contexts.xml:
<extension point="org.eclipse.help.contexts">
<contexts file="contexts.xml"/>
</extension>
Also the setHelp call only registers the help for the control, it does not display the help. If you want to display a help context id use:
PlatformUI.getWorkbench().getHelpSystem().displayHelp("your context id");
Note that the help system always opens its own view to display the help (or expands the dialog if you use TrayDialog or one of its subclasses).
So if you have a Button you would invoke the help with:
button.addSelectionListener(new SelectionAdapter()
{
#Override
public void widgetSelected(SelectionEvent e)
{
PlatformUI.getWorkbench().getHelpSystem().displayHelp("your context id");
}
});

Select editor programmatically

I have written an Eclpse plugin that contains several editor types. For each editor a content type is associated. In some situations I have two possible editors for the same contenttype. I know that I can select the editor via "Open with". But I would like to decide automatically which editor should be opened.
Here is an example
In one plugins I have got the following extension point definitions:
<extension
point="org.eclipse.ui.editors">
<editor
class="de.dstg.adsplus.editors.program.MacroEditor"
contributorClass="de.dstg.adsplus.editors.core.ADSTextEditorActionContributor"
default="true"
icon="icons/macro.png"
id="de.dstg.adsplus.editors.editors.macroEditor"
name="ADS Macro Editor">
<contentTypeBinding
contentTypeId="de.dstg.adsplus.macro">
</contentTypeBinding>
</editor>
</extension
In another plugin I have this definiton:
<extension
point="org.eclipse.ui.editors">
<editor
class="de.dstg.adsplus.focusededitor.FocusedEditor"
contributorClass="de.dstg.adsplus.focusededitor.FocusedEditorContributor"
icon="icons/dveditor.png"
id="de.dstg.adsplus.focusededitor"
name="ADS Focused Editor">
<contentTypeBinding
contentTypeId="de.dstg.adsplus.macro">
</contentTypeBinding>
When I double click on a file with this content type I would like to decide programmatically which editor to open depending on the content of the file or the location where it is stored.
I have tried to find a method that I could implement but without success.
You could use two different content types both using the same file extensions and use a 'content type describer' to distinguish the files.
The content type describer is defined in the content type extension point:
<extension point="org.eclipse.core.contenttype.contentTypes">
<content-type id="ABC"
base-type="org.eclipse.core.runtime.xml"
file-extensions="a,b,c">
<describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber2">
</describer>
</content-type>
The content describer class implements IContentDescriber or ITextContentDescriber. Eclipse calls the describe method to find out if the file is valid for the content type (and to get information about other attributes of the file).

Eclipse RCP: programmatically associate file type with Editor?

How programmatically associate file type with Editor?
That is what Eclipse-RCP Java code can do what is archived with the following UI interaction:
Window -> Preferences
General -> Editors -> File Associations
Add... > File type: *.json
Select *.json file type
Add... (Associated editors) > JavaScript Editor
Make it default
Ralated to Q
https://stackoverflow.com/questions/12429221/eclipse-file-associations-determine-which-editor-in-list-of-associated-editors
Eclipse: associate an editor with a content type
Get associated file extensions for an Eclipse editor
Opening a default editor, on a treeviewer selection eclipse rcp(eg: as eclipse knows that j.java files must be opened in text editor)
eclipse rcp change icon for xml configuration file
I know your questions says "programmatically" but I'll give a complete run down of the methods.
If you are writing the plugin that provides the editor, then you should simply declare the extension in your plugin.xml.
<extension
point="org.eclipse.ui.editors">
<editor
...
extensions="json"
...
If you are distributing a complete RPC application, you can edit the plugin.xml for the plugin that provides the editor or add a plugin that just refers to that editor.
But, if you have to do it programmatically, you are manipulating the internals of an RPC instance. Eclipse does not provide a public API for that but this code will do it:
// error handling is omitted for brevity
String extension = "json";
String editorId = "theplugin.editors.TheEditor";
EditorRegistry editorReg = (EditorRegistry)PlatformUI.getWorkbench().getEditorRegistry();
EditorDescriptor editor = (EditorDescriptor) editorReg.findEditor(editorId);
FileEditorMapping mapping = new FileEditorMapping(extension);
mapping.addEditor(editor);
mapping.setDefaultEditor(editor);
IFileEditorMapping[] mappings = editorReg.getFileEditorMappings();
FileEditorMapping[] newMappings = new FileEditorMapping[mappings.length+1];
for (int i = 0; i < mappings.length; i++) {
newMappings[i] = (FileEditorMapping) mappings[i];
}
newMappings[mappings.length] = mapping;
editorReg.setFileEditorMappings(newMappings);
Associating file type means associating content of ur editor with a predefined one.
This can be easily achieved via plugin.xml..
Just follow the following link:-
Eclipse help Documentation
http://help.eclipse.org/indigo/index.jsp
and search for org.eclipse.core.contenttype.contentTypes.