1414package net .sf .j2s .ajax .junit ;
1515
1616import junit .framework .AssertionFailedError ;
17+ import org .eclipse .swt .events .DisposeEvent ;
18+ import org .eclipse .swt .events .DisposeListener ;
19+ import org .eclipse .swt .widgets .Display ;
1720import org .eclipse .swt .widgets .Shell ;
1821
1922/**
2326 */
2427public class AsyncSWT {
2528
29+ private static boolean shellAutoClose = true ;
30+
31+ public static void setShellAutoClose (boolean close ) {
32+ shellAutoClose = close ;
33+ }
34+
35+ public static boolean isShellAutoClose () {
36+ return shellAutoClose ;
37+ }
38+
2639 /**
2740 * @param shell
2841 * @param runnable
@@ -48,22 +61,17 @@ public class AsyncSWT {
4861}
4962 }
5063 } finally {
51- shell.close ();
52- //if (f.next != null) {
53- // f.next ();
54- //}
64+ if (net.sf.j2s.ajax.junit.AsyncSWT.isShellAutoClose ()) {
65+ shell.close ();
66+ }
5567 }
5668 } else {
57- shell.close ();
58- //if (f.next != null) {
59- // f.next ();
60- //}
69+ if (net.sf.j2s.ajax.junit.AsyncSWT.isShellAutoClose ()) {
70+ shell.close ();
71+ }
6172 }
6273};
6374var key = "j2s.swt.shell.finish.layout";
64- //if (window[key] != null) {
65- // f.next = window[key];
66- //}
6775window[key] = f;
6876var closeFun = closeCleanUp;
6977if (Clazz.instanceOf (runnable, net.sf.j2s.ajax.junit.AsyncTestRunnable)) {
@@ -95,15 +103,19 @@ public static void waitLayout(Shell shell, Runnable runnable) {
95103 }
96104 }
97105 } finally {
98- shell .close ();
106+ if (isShellAutoClose ()) {
107+ shell .close ();
108+ }
99109 }
100110 } else {
101- shell .close ();
111+ if (isShellAutoClose ()) {
112+ shell .close ();
113+ }
102114 }
103115 if (runnable instanceof AsyncTestRunnable ) {
104116 final AsyncTestRunnable aTest = (AsyncTestRunnable ) runnable ;
105117 //aTest.callback();
106- new Thread ( new Runnable () {
118+ final Runnable callback = new Runnable () {
107119 public void run () {
108120 try {
109121 Thread .sleep (100 );
@@ -112,7 +124,20 @@ public void run() {
112124 }
113125 aTest .callback ();
114126 }
115- }).start ();
127+ };
128+ if (isShellAutoClose ()) {
129+ new Thread (callback ).start ();
130+ } else {
131+ shell .addDisposeListener (new DisposeListener () {
132+ public void widgetDisposed (DisposeEvent e ) {
133+ new Thread (callback ).start ();
134+ }
135+ });
136+ Display display = shell .getDisplay ();
137+ while (!shell .isDisposed ()) {
138+ if (!display .readAndDispatch ()) display .sleep ();
139+ }
140+ }
116141 }
117142 }
118143}
0 commit comments