KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > operations > TimeTriggeredProgressMonitorDialog


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.operations;
12
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
18 import org.eclipse.jface.operation.IRunnableWithProgress;
19 import org.eclipse.swt.custom.BusyIndicator;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.PlatformUI;
23
24 /**
25  * The TimeTriggeredProgressMonitorDialog is a progress monitor dialog that only
26  * opens if the runnable provided exceeds the specified long operation time.
27  *
28  * @since 3.1
29  */

30 public class TimeTriggeredProgressMonitorDialog extends ProgressMonitorDialog {
31
32     /**
33      * The time considered to be the long operation time.
34      */

35     private int longOperationTime;
36
37     /**
38      * The time at which the dialog should be opened.
39      */

40     private long triggerTime = -1;
41
42     /**
43      * Whether or not we've already opened a dialog.
44      */

45     private boolean dialogOpened = false;
46     
47     /**
48      * Wrappered monitor so we can check ticks and open the dialog when
49      * appropriate
50      */

51     private IProgressMonitor wrapperedMonitor;
52
53     /**
54      * Create a new instance of the receiver.
55      *
56      * @param parent
57      * the parent of the dialog
58      * @param longOperationTime
59      * the time (in milliseconds) considered to be a long enough
60      * execution time to warrant opening a dialog.
61      */

62     public TimeTriggeredProgressMonitorDialog(Shell parent,
63             int longOperationTime) {
64         super(parent);
65         setOpenOnRun(false);
66         this.longOperationTime = longOperationTime;
67     }
68
69    /**
70      * Create a monitor for the receiver that wrappers the superclasses monitor.
71      *
72      */

73     public void createWrapperedMonitor() {
74         wrapperedMonitor = new IProgressMonitor() {
75
76             IProgressMonitor superMonitor = TimeTriggeredProgressMonitorDialog.super
77                     .getProgressMonitor();
78
79             /*
80              * (non-Javadoc)
81              *
82              * @see org.eclipse.core.runtime.IProgressMonitor#beginTask(java.lang.String,
83              * int)
84              */

85             public void beginTask(String JavaDoc name, int totalWork) {
86                 superMonitor.beginTask(name, totalWork);
87                 checkTicking();
88             }
89
90             /**
91              * Check if we have ticked in the last 800ms.
92              */

93             private void checkTicking() {
94                 if (triggerTime < 0) {
95                     triggerTime = System.currentTimeMillis() + longOperationTime;
96                 }
97                 if (!dialogOpened && System.currentTimeMillis() > triggerTime) {
98                     open();
99                     dialogOpened = true;
100                 }
101             }
102
103
104
105             /*
106              * (non-Javadoc)
107              *
108              * @see org.eclipse.core.runtime.IProgressMonitor#done()
109              */

110             public void done() {
111                 superMonitor.done();
112                 checkTicking();
113             }
114
115             /*
116              * (non-Javadoc)
117              *
118              * @see org.eclipse.core.runtime.IProgressMonitor#internalWorked(double)
119              */

120             public void internalWorked(double work) {
121                 superMonitor.internalWorked(work);
122                 checkTicking();
123             }
124
125             /*
126              * (non-Javadoc)
127              *
128              * @see org.eclipse.core.runtime.IProgressMonitor#isCanceled()
129              */

130             public boolean isCanceled() {
131                 return superMonitor.isCanceled();
132             }
133
134             /*
135              * (non-Javadoc)
136              *
137              * @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(boolean)
138              */

139             public void setCanceled(boolean value) {
140                 superMonitor.setCanceled(value);
141
142             }
143
144             /*
145              * (non-Javadoc)
146              *
147              * @see org.eclipse.core.runtime.IProgressMonitor#setTaskName(java.lang.String)
148              */

149             public void setTaskName(String JavaDoc name) {
150                 superMonitor.setTaskName(name);
151                 checkTicking();
152
153             }
154
155             /*
156              * (non-Javadoc)
157              *
158              * @see org.eclipse.core.runtime.IProgressMonitor#subTask(java.lang.String)
159              */

160             public void subTask(String JavaDoc name) {
161                 superMonitor.subTask(name);
162                 checkTicking();
163             }
164
165             /*
166              * (non-Javadoc)
167              *
168              * @see org.eclipse.core.runtime.IProgressMonitor#worked(int)
169              */

170             public void worked(int work) {
171                 superMonitor.worked(work);
172                 checkTicking();
173
174             }
175         };
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see org.eclipse.jface.dialogs.ProgressMonitorDialog#getProgressMonitor()
182      */

183     public IProgressMonitor getProgressMonitor() {
184         if (wrapperedMonitor == null) {
185             createWrapperedMonitor();
186         }
187         return wrapperedMonitor;
188     }
189     
190    /*
191     * (non-Javadoc)
192     *
193     * @see org.eclipse.jface.operations.IRunnableContext#run(boolean, boolean, IRunnableWithProgress)
194     */

195     public void run(final boolean fork, final boolean cancelable,
196             final IRunnableWithProgress runnable) throws InvocationTargetException JavaDoc,
197             InterruptedException JavaDoc {
198         final InvocationTargetException JavaDoc[] invokes = new InvocationTargetException JavaDoc[1];
199         final InterruptedException JavaDoc[] interrupt = new InterruptedException JavaDoc[1];
200         Runnable JavaDoc dialogWaitRunnable = new Runnable JavaDoc() {
201             public void run() {
202                 try {
203                     TimeTriggeredProgressMonitorDialog.super.run(fork, cancelable, runnable);
204                 } catch (InvocationTargetException JavaDoc e) {
205                     invokes[0] = e;
206                 } catch (InterruptedException JavaDoc e) {
207                     interrupt[0]= e;
208                 }
209             }
210         };
211         final Display display = PlatformUI.getWorkbench().getDisplay();
212         if (display == null) {
213             return;
214         }
215         //show a busy cursor until the dialog opens
216
BusyIndicator.showWhile(display, dialogWaitRunnable);
217         if (invokes[0] != null) {
218             throw invokes[0];
219         }
220         if (interrupt[0] != null) {
221             throw interrupt[0];
222         }
223      }
224 }
225
Popular Tags