root/dwt/custom/BusyIndicator.d

Revision 315:349b8c12e243, 3.0 kB (checked in by Frank Benoit <benoit@tionex.de>, 3 months ago)

Sync dwt/custom with dwt-linux

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