diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java index 7e1054a..d950489 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java @@ -11,6 +11,7 @@ package org.eclipse.swt.widgets; import java.util.*; +import java.util.List; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; @@ -4646,6 +4647,25 @@ return true; } +/** + * Block spinning the event loop until {@link BlockCondition#isBlocked()} + * return false + * + * @param block + * the block condition + */ +public void block(BlockCondition block) { + checkDevice(); + if( ! block.isBlocked() ) { + return; + } + while (block.isBlocked()) { + if (!readAndDispatch()) { + sleep(); + } + } +} + int sourceProc (int info) { return 0; } @@ -5956,4 +5976,29 @@ return 0; } +public static class BlockCondition { + private boolean blocked = true; + private List listenerList = new ArrayList(); + + public void addListener(Runnable r) { + listenerList.add(r); + } + + public void removeListener(Runnable r) { + listenerList.remove(r); + } + + public boolean isBlocked() { + return blocked; + } + + public void unblock() { + blocked = false; + Object[] listeners = listenerList.toArray(); + for( int i = 0; i < listeners.length; i++ ) { + ((Runnable)listeners[i]).run(); + } + } +} + } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Shell.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Shell.java index 369e2e9..6c86984 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Shell.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Shell.java @@ -16,6 +16,7 @@ import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.internal.cocoa.*; +import org.eclipse.swt.widgets.Display.BlockCondition; /** * Instances of this class represent the "windows" @@ -2328,4 +2329,18 @@ closeWidget(true); } +/** + * Open the shell and block until it is closed + */ +public void openBlocking() { + final BlockCondition c = new BlockCondition(); + addDisposeListener(new DisposeListener() { + + public void widgetDisposed(DisposeEvent e) { + c.unblock(); + } + }); + open(); + getDisplay().block(c); +} }