How to determine that a JFace or SWT Dialog is currently open?
-
28-09-2019 - |
문제
In our RCP application, we need to resort to using a global key event handler (via Display.addFilter()) for more advanced key event handling/routing irrespective of current focus. We need to be able to determine if a dialog box is currently open for some of the routing logic.
Seems like a fairly trivial question but I keep hitting dead ends going off Widget hierarchy, Shells, WindowManagers.
I am looking for a robust solution that would not require any extra work on the part of Dialog implementers or client code that uses standard framework dialogs.
해결책
In the example below, shell
is a defined Shell in the scope. You could modify the code to compare activeShell
with a list of Shells.
shell.getDisplay().addFilter(SWT.KeyDown, new Listener() {
public void handleEvent(final Event event) {
if (shell.isDisposed()) {
return;
}
final Shell activeShell = shell.getDisplay().getActiveShell();
if (activeShell != null && activeShell.equals(shell)) {
if (event.stateMask == SWT.MOD1 && event.character == 'w') {
shell.dispose();
}
}
}
});
This example code will close shell
when ⌘
+W
is pressed on Mac.