如何让工作台窗口在基于 Eclipse 的项目中打开模式对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/443245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the workbench window to open a modal dialog in an Eclipse based project?
提问by Tirno
In order to open a modal dialog, you need to pass a parent window, and pass the necessary flags for the dialog to be modal of course.
为了打开模态对话框,您需要传递一个父窗口,并传递必要的标志以使对话框成为模态。
Depending on where you are in the eclipse infrastructure, finding this parent window is not always easy.
根据您在 eclipse 基础设施中的位置,找到这个父窗口并不总是那么容易。
How can the parent window be accessed?
如何访问父窗口?
回答by zvikico
The piece of code from the previous answer will work. However, keep in mind you can only open your dialog from the UI thread. If you are opening the dialog from a different thread, e.g. a background process, you need to do something like this:
上一个答案中的一段代码将起作用。但是,请记住,您只能从 UI 线程打开对话框。如果您从不同的线程(例如后台进程)打开对话框,则需要执行以下操作:
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
public void run() {
Shell activeShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
}
});
Otherwise you will get an exception when creating the dialog.
否则,您将在创建对话框时遇到异常。
回答by Tirno
From a view or an editor (this part is easy):
从视图或编辑器(这部分很简单):
this.getSite().getWorkbenchWindow().getShell()
From elsewhere, access a view or editor and same as above.
从别处访问视图或编辑器,与上述相同。
If you find yourself in a class where you don't have access to a view or editor, you probably don't want to be calling any UI code, but if you really want to shoot yourself in the foot:
如果您发现自己在一个无法访问视图或编辑器的类中,您可能不想调用任何 UI 代码,但如果您真的想用脚射击自己:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()
回答by paul
Not quite exactly what you want to do but you may need to use SWT.APPLICATION_MODAL, SWT.DIALOG_TRIMetc. when creating your dialog in order to make it a modal dialog (but perhaps that's not what your question was about).
不完全是您想要做的,但您可能需要在创建对话框时使用SWT.APPLICATION_MODAL、SWT.DIALOG_TRIM等,以使其成为模态对话框(但也许这不是您的问题)。
See this linkfor more info.
有关更多信息,请参阅此链接。