Get the active file, from the editor. [message #300299] |
Tue, 07 March 2006 10:08 |
Eclipse User |
|
|
|
Originally posted by: robbelibobban.gmail.com
How do i in code get the active file from the editor, from my plugin ?
I use the CDT editor.
I want to make a syntax check on the file.
/Robert
|
|
|
Re: Get the active file, from the editor. [message #300308 is a reply to message #300299] |
Tue, 07 March 2006 14:24 |
Eclipse User |
|
|
|
Originally posted by: robbelibobban.gmail.com
Forgot to use the search function =/
This is the way to do it.
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.part.FileEditorInput;
import org.osgi.framework.Bundle;
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow window = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
IEditorPart editor = page.getActiveEditor();
IEditorInput input = editor.getEditorInput();
IPath path = ((FileEditorInput)input).getPath();
/Robert
|
|
|
Re: Get the active file, from the editor. [message #300353 is a reply to message #300308] |
Tue, 07 March 2006 22:08 |
Eclipse User |
|
|
|
Originally posted by: varavamu.yahoo.com
you will have to perform null checks along the way :) since there is no
guarantee that 'window' or 'page' will return non-null values.
hth,
Vijay
Robert wrote:
> Forgot to use the search function =/
>
> This is the way to do it.
>
> import org.eclipse.ui.IWorkbench;
> import org.eclipse.ui.IWorkbenchPage;
> import org.eclipse.ui.IWorkbenchWindow;
> import org.eclipse.ui.IEditorPart;
> import org.eclipse.ui.IEditorInput;
> import org.eclipse.ui.part.FileEditorInput;
> import org.osgi.framework.Bundle;
>
> IWorkbench wb = PlatformUI.getWorkbench();
> IWorkbenchWindow window = wb.getActiveWorkbenchWindow();
> IWorkbenchPage page = window.getActivePage();
> IEditorPart editor = page.getActiveEditor();
> IEditorInput input = editor.getEditorInput();
> IPath path = ((FileEditorInput)input).getPath();
>
> /Robert
>
|
|
|
Re: Get the active file, from the editor. [message #300483 is a reply to message #300353] |
Thu, 09 March 2006 17:26 |
Eclipse User |
|
|
|
Originally posted by: nathalie.lepy.tiscali.fr
In fact, this solution only works if you are somewhere in the Workbench,
in other cases, it doesn't work (if you want to get the active editor
file, from a dialog box for example, it doesn't work).
I tried this piece of code, and for the time being it seems to work :
----------
IWorkbench workb = LotosPlugin.getDefault().getWorkbench();
if (workb == null) return false;
IWorkbenchWindow[] wins = workb.getWorkbenchWindows();
if (wins.length <= 0) return false;
int i = 0;
while ( (i < wins.length)
&& (! (wins[i].getPartService().getActivePart() instanceof IEditorPart))
)
i++;
if (i >= wins.length) return false;
IEditorPart editor =
(IEditorPart) wins[i].getPartService().getActivePart();
IEditorInput input = editor.getEditorInput();
if (input == null) return false;
IPath path = ((FileEditorInput)input).getPath();
if (path == null) return false;
----------
Nath.
Vijay Aravamudhan wrote:
> you will have to perform null checks along the way :) since there is no
> guarantee that 'window' or 'page' will return non-null values.
> hth,
> Vijay
> Robert wrote:
>> Forgot to use the search function =/
>>
>> This is the way to do it.
>>
>> import org.eclipse.ui.IWorkbench;
>> import org.eclipse.ui.IWorkbenchPage;
>> import org.eclipse.ui.IWorkbenchWindow;
>> import org.eclipse.ui.IEditorPart;
>> import org.eclipse.ui.IEditorInput;
>> import org.eclipse.ui.part.FileEditorInput;
>> import org.osgi.framework.Bundle;
>>
>> IWorkbench wb = PlatformUI.getWorkbench();
>> IWorkbenchWindow window = wb.getActiveWorkbenchWindow();
>> IWorkbenchPage page = window.getActivePage();
>> IEditorPart editor = page.getActiveEditor();
>> IEditorInput input = editor.getEditorInput();
>> IPath path = ((FileEditorInput)input).getPath();
>>
>> /Robert
>>
|
|
|
|
Powered by
FUDForum. Page generated in 0.04238 seconds