Open a eclipse Editor programmatically - eclipse

I am trying to add eclipse editor in 'open with' menu for files with a particular extension.
I do that with launcher in org.eclipse.ui.editors extension point.In the launcher I use "open editor" method which requires to pass editor Id. Is there any way such that we can open a editor programmatically without passing the editor ID? Can we open editor programmatically by passing the instance of the class which implements the editor?..

Take a look on the IDE class's function openEditor():
IFile fileToOpen = ...
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
IDE.openEditor( page, fileToOpen );
Note that a few checks might be important here (if the file exists or not, can be opened, you can access the active page - it is not null, etc.)

Related

Open eclipse view along with custom editor in PDE

I have created a custom eclipse editor and want a view to be opened in the event the editor is active. If the view is already open then nothing should be done. I also want this functionality for the console in that the console should be open in the event my custom editor is active. Is this possible to do in Eclipse plugin development environment?
You can open the view in the init method of your editor using something like:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.showView("org.eclipse.ui.console.ConsoleView");
This will open the view if it is not open or make it the active view if it is already open.

Opening compare editor results inside a wizard page

In my custom plugin I am trying open compare result inside a wizard page but they always open in an editor or as a new dialog.
I am overriding setVisible(booolean)method, so as soon as wizard page is visible I am opening compare editor using CompareUI.openCompareEditor()call, but this opens up a new editor in the background.
Is it possible to open compare editor results inside a wizard page.
Thanks!!
There is no existing support for this.
You could read the source of the dialog that is displayed by openCompareDialog.openCompareDialog - this is org.eclipse.compare.internal.CompareDialog.
It looks like it might be possible to do the same thing in a wizard page without using internal classes by adapting what that dialog does.

How can I use a non-IFileEditorInput as the default EditorInput in my plugin?

I am in the middle of creating an Eclipse plugin that will open an editor. Everything is mapped out well - my plugin.xml is set up correctly to open the editor for anything with the .xyz extension. The only thing holding me back is the IEditorInput.
I have a subclass of IEditorInput that I created for use with my editor. When I open the editor programmatically, I can create that EditorInput and open the editor correctly. However, when I open the editor using Project Explorer (Right click > Open With > My Editor), it is opened with a FileEditorInput.
How can I change the default behavior of Project Explorer to create the correct IEditorInput? Is there something in plugin.xml that I'm missing?
Thanks!
The editor doesn't get to choose the kind of editor input object it's given. It can use a IDocumentProvider to support different kinds of IEditorInput, but you should never artificially limit your editor to working with one kind of input, even if you do manage to change how the Project Explorer works.

Opening a default editor, on a treeviewer selection eclipse rcp(eg: as eclipse knows that j.java files must be opened in text editor)

I have a file with extension .xyz .i want to open an editor when it is double clicked How to go about doing it.I have a treeviewer that lists only my files that i need ,in those i have a .xyz file.I have added an editor in the extension point and in the extensions textfield i hav e added .xyz also.I dont want to add a doubleclicklistener to the viewer.Is there a way to add or tell eclipse that if this is the extension of the file then by default it should open.
You need to add an org.eclipse.ui.editors extension point.
<extension point="org.eclipse.ui.editors">
<editor
name="MyEditor Name"
extensions="my_file_extension"
icon="icons/sample.gif"
class="com.me.MyEditor"
id="com.me.MyEditorID">
</editor>
Well if you have your own viewer implementation you will need to define the complete behavior in that viewer, even the opening of files. Since you have registered an editor against that extension so whenever that file is opened in Eclipse, either by your viewer or some other viewer (Project Explorer, Navigator) Eclipse will automatically open the file in the assigned editor.
In your double click listener you can use org.eclipse.ui.actions.OpenFileAction or something like it to ensure that the file is opened by Eclipse according to Workbench rules. Btw why do you want to avoid adding a double click listener?

How to set default editor tab in Eclipse?

I'm using the HTML editor resp. the Structured Text Editor in Eclipse. It always opens in the tab Visual/Source:
Is it possible to tell Eclipse it should always open this editor in the Source tab?
You seem to use a plugin which associates with HTML files. For example Eclipse normally loads XML files for the first time with Design tab, and once you switch to source tab, it remembers the next time to open any document associated with XML Editor in Source tab. I don't know remembering is up to Eclipse or up to the plugin associated with the file, but a quick workaround would be:
to right click on the HTML file in package explorer > Open With > choose another editor (e.g. text editor). This only associates with current file. If you want to change file association for all HTMLs:
goto Preferences (under menu Window) > General > Editor > File Associations and change HTML file association there.
In Eclipse goto Windows-->Preferences-->Type Editors change the associated editors for File Types after that click on OK
You didn't say what version of Eclipse you're using. My HTML / Structured Text editors didn't have the tabs the same as yours. I'm using 3.4.2.
You can extend that editor by writing your own plug-in for Eclipse. Outside of the 'create a plug-in project' stuff, start by finding the extension points for the target editor. Then your plug-in can just register as an extension and add a new property instead of writing a whole editor. The property should show up on a preference page and then your code can take care of switching the active view of the editor to the 'Source' tab based on that property.
Right click the file and then "open with" and open it in another HTML or texteditor.
And then map this editor as the default editor for this filetype by right clicking the document and setting the file extension.
I always do this to get rid of the memory greedy WYSIWYG editors.