KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > BusyIndicator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  *******************************************************************************/

11 package org.eclipse.swt.custom;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.graphics.*;
16 import org.eclipse.swt.widgets.*;
17
18 /**
19  * Support for showing a Busy Cursor during a long running process.
20  */

21 public class BusyIndicator {
22
23     static int nextBusyId = 1;
24     static final String JavaDoc BUSYID_NAME = "SWT BusyIndicator"; //$NON-NLS-1$
25
static final String JavaDoc BUSY_CURSOR = "SWT BusyIndicator Cursor"; //$NON-NLS-1$
26

27 /**
28  * Runs the given <code>Runnable</code> while providing
29  * busy feedback using this busy indicator.
30  *
31  * @param display the display on which the busy feedback should be
32  * displayed. If the display is null, the Display for the current
33  * thread will be used. If there is no Display for the current thread,
34  * the runnable code will be executed and no busy feedback will be displayed.
35  * @param runnable the runnable for which busy feedback is to be shown.
36  * Must not be null.
37  *
38 * @exception IllegalArgumentException <ul>
39  * <li>ERROR_NULL_ARGUMENT - if the runnable is null</li>
40  * </ul>
41  */

42
43 public static void showWhile(Display display, Runnable JavaDoc runnable) {
44     if (runnable == null)
45         SWT.error(SWT.ERROR_NULL_ARGUMENT);
46     if (display == null) {
47         display = Display.getCurrent();
48         if (display == null) {
49             runnable.run();
50             return;
51         }
52     }
53     
54     Integer JavaDoc busyId = new Integer JavaDoc(nextBusyId);
55     nextBusyId++;
56     Cursor cursor = display.getSystemCursor(SWT.CURSOR_WAIT);
57     Shell[] shells = display.getShells();
58     for (int i = 0; i < shells.length; i++) {
59         Integer JavaDoc id = (Integer JavaDoc)shells[i].getData(BUSYID_NAME);
60         if (id == null) {
61             shells[i].setCursor(cursor);
62             shells[i].setData(BUSYID_NAME, busyId);
63         }
64     }
65         
66     try {
67         runnable.run();
68     } finally {
69         shells = display.getShells();
70         for (int i = 0; i < shells.length; i++) {
71             Integer JavaDoc id = (Integer JavaDoc)shells[i].getData(BUSYID_NAME);
72             if (id == busyId) {
73                 shells[i].setCursor(null);
74                 shells[i].setData(BUSYID_NAME, null);
75             }
76         }
77     }
78 }
79 }
80
Popular Tags