When you click on a file in the Package Explorer or Navigator, the file will open the associated editor. If you are looking for a way to do the same action through code, this tip is for you.
There are three things you need to open an editor:
(*) The IEditorInput for the resource you want to open
(*) A IWorkbenchPage, in which you want to open the editor
(*) The id of the editor, which you want to open.
For this tip, we assume that you want to open a file. So we can use FileEditorInput.
IEditorInput editorInput = new FileEditorInput(fileToBeOpened);
We will open the editor in the active page of the active window.
IWorkbenchWindow window=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
Open the TextEditor in that page.
page.openEditor(editorInput, "org.eclipse.ui.DefaultTextEdtior");
So here goes the entire snippet:
IFile fileToBeOpened = ...;
IEditorInput editorInput = new FileEditorInput(fileToBeOpened);
IWorkbenchWindow window=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
page.openEditor(editorInput, "org.eclipse.ui.DefaultTextEdtior");
Feb 20, 2007
Opening an Editor programmatically
Posted by
Prakash G.R.
at
4:32 PM
2
comments
Labels: eclipse
Feb 19, 2007
Finding the Install Location of a Plugin
Usually a plugin should not depend on its location. In rare cases we may have to find the location of the plugin to load some resources. But there is no standard API to get the plugin's installed location. This tips explains you to add one.
This can be done with just four lines of code. Add this In the Activator class of your plugin.
public String getInstallLocation() throws Exception {
Bundle bundle = getBundle();
URL locationUrl =
FileLocator.find(bundle,new Path("/"), null);
URL fileUrl =
FileLocator.toFileURL(locationUrl);
return fileUrl.getFile();
}
If your want this method outside your Activator class, you need replace the first line with this:
Bundle bundle = Platform.getBundle("com.example.plugin.id"
Posted by
Prakash G.R.
at
6:59 PM
1 comments
Labels: eclipse
Feb 17, 2007
Associating a Wizard with a Perspective
Have you seen the "Open Associated Perspective" dialog when you create a new Java Project from a Resource Perspective or Debug Perspective? Wonder how to associate a perspective to your own wizard? This tip explains it.
We normally extend the org.eclipse.ui.newWizards - wizard extension point for the creation of new elements including a new project. It has an optional attribute called finalPerspective - the perspective that will get activated when the wizard is finished. This happens only if the wizard is a new project wizard. So how do we make our other wizards to switch to some predefined perspective?
Three simple steps:
- The obvious step. add the finalPerspective attribute to the definition of our wizard element in the extension.
- Make the Wizard class implement the interface IExecutableExtension. This interface has only one method setInitializationData. Implement the method in the class and assign the parameter IConfigurationElement to a field in the method.
- In the performFinish method of the Wizard, call BasicNewProjectResourceWizard.updatePerspective method.
If you don't want this perspective switch happening, when some another perspective is active, you can specify that perspective in the preferredPerspectives attribute.
If the switching perspective doesn't happen, probably you have turned it off. In the preferences page, look at the option the option General->Perspectives->Open the associated perspective.
I've added the Resource perspective as the final perspective for sample New File Wizard created by Eclipse.
Posted by
Prakash G.R.
at
9:29 AM
1 comments
Labels: eclipse
