如何让工作台窗口在基于Eclipse的项目中打开模式对话框?

时间:2023-01-12 14:07:37

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?

如何访问父窗口?

3 个解决方案

#1


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.

否则,在创建对话框时会出现异常。

#2


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()

#3


Not quite exactly what you want to do but you may need to use SWT.APPLICATION_MODAL, SWT.DIALOG_TRIM etc. 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 link for more info.

有关详细信息,请参阅此链接。

#1


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.

否则,在创建对话框时会出现异常。

#2


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()

#3


Not quite exactly what you want to do but you may need to use SWT.APPLICATION_MODAL, SWT.DIALOG_TRIM etc. 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 link for more info.

有关详细信息,请参阅此链接。