如何从eclipse IDE获取当前活动选项卡的文件名? [英] How to get the filename of the Current Active Tab from eclipse IDE?
本文介绍了如何从eclipse IDE获取当前活动选项卡的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在eclipse-IDE编辑器中获取当前打开的选项卡的文件名。基本上我正在使用Java开发一个插件,我想以编程方式从eclipse-IDE编辑器中提取当前打开的文件的名称。
解决方案>
可能有一个较短的方法,但是这个代码应该这样做:
IWorkbenchPage activePage = PlatformUI.getWorkbench() 。.getActiveWorkbenchWindow()getActivePage();
String name = activePage.getActiveEditor()。getEditorInput()。getName();
当然,请确保您检查可能的空值等。
编辑:从UI线程运行。例如:
final String [] name = new String [1];
UIJob job = new UIJob(获取活动编辑器)// $ NON-NLS-1 $
{
public IStatus runInUIThread(IProgressMonitor monitor)
{
尝试
{
IWorkbenchPage activePage = PlatformUI.getWorkbench()。getActiveWorkbenchWindow()。getActivePage();
name [0] = activePage.getActiveEditor()。getEditorInput()。getName();
}
catch(异常e)
{
//返回一些其他状态
}
return Status.OK_STATUS;
}
};
job.schedule();
job.join();
System.out.println(name [0]);
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]);
这篇关于如何从eclipse IDE获取当前活动选项卡的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文