1 /*******************************************************************************
2 * Copyright (c) 2007 SAS Institute.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * SAS Institute - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.utils.ui.internal.awt;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.FocusAdapter;
15 import org.eclipse.swt.events.FocusEvent;
16 import org.eclipse.swt.widgets.Dialog;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Shell;
22 class SwtInputBlocker extends Dialog {
23 static private SwtInputBlocker instance = null;
24 static private int blockCount = 0;
27 private SwtInputBlocker(Shell parent) {
28 super(parent, SWT.NONE);
31 private Object open() {
32 assert Display.getCurrent() != null; // On SWT event thread
34 final Shell parent = getParent();
35 shell = new Shell(parent, SWT.APPLICATION_MODAL);
37 shell.addFocusListener(new FocusAdapter() {
38 public void focusGained(FocusEvent e) {
39 // On some platforms (e.g. Linux/GTK), the 0x0 shell still appears as a dot
40 // on the screen, so make it invisible by moving it below other windows. This
41 // is unnecessary under Windows and causes a flash, so only make the call when necessary.
42 if (Platform.isGtk()) {
43 shell.moveBelow(null);
45 AwtEnvironment.getInstance(shell.getDisplay()).requestAwtDialogFocus();
50 Display display = parent.getDisplay();
51 while (!shell.isDisposed()) {
52 if (!display.readAndDispatch()) {
59 private void close() {
65 static void unblock() {
66 assert blockCount >= 0;
67 assert Display.getCurrent() != null; // On SWT event thread
70 // System.out.println("Deleting SWT blocker");
71 if (blockCount == 0) {
74 if ((blockCount == 1) && (instance != null)) {
82 assert blockCount >= 0;
84 // System.out.println("Creating SWT blocker");
85 final Display display = Display.getCurrent();
86 assert display != null; // On SWT event thread
89 if (blockCount == 1) {
90 assert instance == null; // should be no existing blocker
92 // get a shell to parent the blocking dialog
93 Shell shell = AwtEnvironment.getInstance(display).getShell();
95 // If there is a shell to block, block input now. If there are no shells,
96 // then there is no input to block. In the case of no shells, we are not
97 // protecting against a shell that might get created later. This is a rare
98 // enough case to skip, at least for now. In the future, a listener could be
100 // TODO: if (shell==null) add listener to block shells created later?
102 // Block is implemented with a hidden modal dialog. Using setEnabled(false) is another option, but
103 // on some platforms that will grey the disabled controls.
105 instance = new SwtInputBlocker(shell);