1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 IBM Corporation and others.
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module org.eclipse.swt.custom.BusyIndicator;
14 
15 
16 
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Cursor;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Shell;
21 import java.lang.all;
22 
23 /**
24  * Support for showing a Busy Cursor during a long running process.
25  *
26  * @see <a href="http://www.eclipse.org/swt/snippets/#busyindicator">BusyIndicator snippets</a>
27  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
28  */
29 public class BusyIndicator {
30 
31     static int nextBusyId = 1;
32     static const String BUSYID_NAME = "SWT BusyIndicator"; //$NON-NLS-1$
33     static const String BUSY_CURSOR = "SWT BusyIndicator Cursor"; //$NON-NLS-1$
34 
35 /**
36  * Runs the given <code>Runnable</code> while providing
37  * busy feedback using this busy indicator.
38  *
39  * @param display the display on which the busy feedback should be
40  *        displayed.  If the display is null, the Display for the current
41  *        thread will be used.  If there is no Display for the current thread,
42  *        the runnable code will be executed and no busy feedback will be displayed.
43  * @param runnable the runnable for which busy feedback is to be shown.
44  *        Must not be null.
45  *
46 * @exception IllegalArgumentException <ul>
47  *    <li>ERROR_NULL_ARGUMENT - if the runnable is null</li>
48  * </ul>
49  */
50 
51 public static void showWhile(Display display, Runnable runnable) {
52     if (runnable is null)
53         SWT.error(SWT.ERROR_NULL_ARGUMENT);
54     if (display is null) {
55         display = Display.getCurrent();
56         if (display is null) {
57             runnable.run();
58             return;
59         }
60     }
61 
62     Integer busyId = new Integer(nextBusyId);
63     nextBusyId++;
64     Cursor cursor = display.getSystemCursor(SWT.CURSOR_WAIT);
65     Shell[] shells = display.getShells();
66     for (int i = 0; i < shells.length; i++) {
67         Integer id = cast(Integer)shells[i].getData(BUSYID_NAME);
68         if (id is null) {
69             shells[i].setCursor(cursor);
70             shells[i].setData(BUSYID_NAME, busyId);
71         }
72     }
73 
74     try {
75         runnable.run();
76     } finally {
77         shells = display.getShells();
78         for (int i = 0; i < shells.length; i++) {
79             Integer id = cast(Integer)shells[i].getData(BUSYID_NAME);
80             if ( id !is null && id == busyId) {
81                 shells[i].setCursor(null);
82                 shells[i].setData(BUSYID_NAME, null);
83             }
84         }
85     }
86 }
87 }