KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > TimeoutProgressMonitorDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
16 import org.eclipse.jface.operation.IRunnableWithProgress;
17 import org.eclipse.jface.operation.ModalContext;
18 import org.eclipse.swt.custom.BusyIndicator;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Shell;
21
22 public class TimeoutProgressMonitorDialog extends ProgressMonitorDialog {
23     // the timeout
24
private int timeout;
25     // the number of currently running runnables.
26
private int runningRunnables = 0;
27
28     /**
29      * Creates a progress monitor dialog under the given shell.
30      * The dialog has a standard title and no image.
31      * <code>open</code> is non-blocking.
32      *
33      * @param parent the parent shell
34      * @param timeout the delay after which the dialog will be opened during a run()
35      */

36     public TimeoutProgressMonitorDialog(Shell parent, int timeout) {
37         super(parent);
38         this.timeout = timeout;
39     }
40
41     /* (non-Javadoc)
42      * Method declared on ITeamRunnableContext.
43      * Runs the given <code>IRunnableWithProgress</code> with the progress monitor for this
44      * progress dialog. The dialog is opened before it is run, and closed after it completes.
45      */

46     public void run(final boolean fork, boolean cancelable, final IRunnableWithProgress runnable) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
47         setCancelable(cancelable);
48         create(); // create the Shell but don't open it yet
49
try {
50             runningRunnables++;
51             final Display display = getShell().getDisplay();
52             display.timerExec(timeout, new Runnable JavaDoc() {
53                 public void run() {
54                     Shell shell = getShell();
55                     if (shell != null && ! shell.isDisposed()) open();
56                 }
57             });
58             
59             final Exception JavaDoc[] holder = new Exception JavaDoc[1];
60             BusyIndicator.showWhile(display, new Runnable JavaDoc() {
61                 public void run() {
62                     try {
63                         ModalContext.run(runnable, fork, getProgressMonitor(), display);
64                     } catch (InvocationTargetException JavaDoc ite) {
65                         holder[0] = ite;
66                     } catch (InterruptedException JavaDoc ie) {
67                         holder[0] = ie;
68                     }
69                 }
70             });
71             if (holder[0] != null) {
72                 if (holder[0] instanceof InvocationTargetException JavaDoc) {
73                     throw (InvocationTargetException JavaDoc) holder[0];
74                 } else if (holder[0] instanceof InterruptedException JavaDoc) {
75                     throw (InterruptedException JavaDoc) holder[0];
76                 }
77             }
78         } finally {
79             runningRunnables--;
80             close();
81         }
82     }
83     
84     public boolean close() {
85         if (runningRunnables <= 0) return super.close();
86         return false;
87     }
88 }
89
Popular Tags