Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the filename of the Current Active Tab from eclipse IDE?

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.

like image 901
user844066 Avatar asked Jul 14 '11 07:07

user844066


1 Answers

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]);
like image 151
sgibly Avatar answered Oct 25 '22 05:10

sgibly