Thursday, May 15, 2008

Getting Active Editor and Active File associated with active editor

I had a requirement to find the active editor in the runtime eclispe and also the file , with which editor is associated to...

This is how we get active editor from RuntimeEclipse

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();

The above line gives us the active editor , ofcurse u have to make null checks everytime...
getActiveWorkbenchWindow() first and then make null check and if its not null,then getActivePage()
and again check for null and then getActiveEditor().

and then getEditorInput (method in editorpart) and do the following
IEditorInput input = getEditorInput();
IFile activeFile = ((IFileEditorInput) input).getFile();
IProject project = activeFile.getProject();
like that u can get the project and can create files as u wish,,,

No comments: