I want to get the file name of the currently open tab in the eclipse-IDE-editor. Basically I am developing a plugin with Java and I want to extract the name of the currently open file from the eclipse-IDE-editor programmatically.
There might be a shorter way, but this code should do it:
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
String name = activePage.getActiveEditor().getEditorInput().getName();
Of course, make sure you check for possible nulls, etc.
EDIT: Run this from a UI thread. For example:
final String[] name = new String[1];
UIJob job = new UIJob("Get active editor") //$NON-NLS-1$
{
public IStatus runInUIThread(IProgressMonitor monitor)
{
try
{
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
name[0] = activePage.getActiveEditor().getEditorInput().getName();
}
catch (Exception e)
{
// return some other status
}
return Status.OK_STATUS;
}
};
job.schedule();
job.join();
System.out.println(name[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With