Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Get the active file, from the editor.
Get the active file, from the editor. [message #300299] Tue, 07 March 2006 10:08 Go to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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
>>
Re: Get the active file, from the editor. [message #716506 is a reply to message #300483] Wed, 17 August 2011 15:08 Go to previous message
Kitesurfer () is currently offline Kitesurfer ()
Messages: 84
Registered: December 2009
Member
Hi,

How to get this (determining the full OS-filename) working for files outside the workbench ? (so files that were opened via menu "File" / "Open file ...").

Thx !
Previous Topic:Multiple applications mess up configuration
Next Topic:What r the tools Provided by Eclipse
Goto Forum:
  


Current Time: Wed Sep 17 23:49:49 GMT 2014

Powered by FUDForum. Page generated in 0.02672 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software