KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > progress > ProgressMonitorFocusJobDialog


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.progress;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IProgressMonitorWithBlocking;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
18 import org.eclipse.core.runtime.jobs.IJobChangeListener;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.custom.BusyIndicator;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Rectangle;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.progress.IProgressConstants;
34 import org.eclipse.ui.progress.WorkbenchJob;
35
36 /**
37  * The ProgressMonitorFocusJobDialog is a dialog that shows progress for a
38  * particular job in a modal dialog so as to give a user accustomed to a modal
39  * UI a more familiar feel.
40  */

41 class ProgressMonitorFocusJobDialog extends ProgressMonitorJobsDialog {
42     Job job;
43
44     /**
45      * Create a new instance of the receiver with progress reported on the job.
46      *
47      * @param parentShell
48      * The shell this is parented from.
49      */

50     public ProgressMonitorFocusJobDialog(Shell parentShell) {
51         super(parentShell == null ? ProgressManagerUtil.getNonModalShell()
52                 : parentShell);
53         setShellStyle(getDefaultOrientation() | SWT.BORDER | SWT.TITLE
54                 | SWT.RESIZE | SWT.MODELESS);
55         setCancelable(true);
56         enableDetailsButton = true;
57     }
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see org.eclipse.jface.dialogs.ProgressMonitorDialog#cancelPressed()
63      */

64     protected void cancelPressed() {
65         job.cancel();
66         super.cancelPressed();
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.eclipse.jface.dialogs.ProgressMonitorDialog#configureShell(org.eclipse.swt.widgets.Shell)
73      */

74     protected void configureShell(Shell shell) {
75         super.configureShell(shell);
76         shell.setText(job.getName());
77
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
84      */

85     protected void createButtonsForButtonBar(Composite parent) {
86         Button runInWorkspace = createButton(
87                 parent,
88                 IDialogConstants.CLOSE_ID,
89                 ProgressMessages.ProgressMonitorFocusJobDialog_RunInBackgroundButton,
90                 true);
91         runInWorkspace.addSelectionListener(new SelectionAdapter() {
92             /*
93              * (non-Javadoc)
94              *
95              * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
96              */

97             public void widgetSelected(SelectionEvent e) {
98                 Rectangle shellPosition = getShell().getBounds();
99                 job.setProperty(IProgressConstants.PROPERTY_IN_DIALOG,
100                         Boolean.FALSE);
101                 finishedRun();
102                 ProgressManagerUtil.animateDown(shellPosition);
103             }
104         });
105         runInWorkspace.setCursor(arrowCursor);
106
107         cancel = createButton(parent, IDialogConstants.CANCEL_ID,
108                 IDialogConstants.CANCEL_LABEL, false);
109         cancel.setCursor(arrowCursor);
110
111         createDetailsButton(parent);
112     }
113
114     /**
115      * Returns a listener that will close the dialog when the job completes.
116      *
117      * @return IJobChangeListener
118      */

119     private IJobChangeListener createCloseListener() {
120         return new JobChangeAdapter() {
121             /*
122              * (non-Javadoc)
123              *
124              * @see org.eclipse.core.runtime.jobs.IJobChangeListener#done(org.eclipse.core.runtime.jobs.IJobChangeEvent)
125              */

126             public void done(IJobChangeEvent event) {
127                 // first of all, make sure this listener is removed
128
event.getJob().removeJobChangeListener(this);
129                 if (!PlatformUI.isWorkbenchRunning()) {
130                     return;
131                 }
132                 // nothing to do if the dialog is already closed
133
if (getShell() == null) {
134                     return;
135                 }
136                 WorkbenchJob closeJob = new WorkbenchJob(
137                         ProgressMessages.ProgressMonitorFocusJobDialog_CLoseDialogJob) {
138                     /*
139                      * (non-Javadoc)
140                      *
141                      * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
142                      */

143                     public IStatus runInUIThread(IProgressMonitor monitor) {
144                         Shell currentShell = getShell();
145                         if (currentShell == null || currentShell.isDisposed()) {
146                             return Status.CANCEL_STATUS;
147                         }
148                         finishedRun();
149                         return Status.OK_STATUS;
150                     }
151                 };
152                 closeJob.setSystem(true);
153                 closeJob.schedule();
154             }
155         };
156     }
157
158     /**
159      * Return the ProgressMonitorWithBlocking for the receiver.
160      *
161      * @return IProgressMonitorWithBlocking
162      */

163     private IProgressMonitorWithBlocking getBlockingProgressMonitor() {
164         return new IProgressMonitorWithBlocking() {
165             /*
166              * (non-Javadoc)
167              *
168              * @see org.eclipse.core.runtime.IProgressMonitor#beginTask(java.lang.String,
169              * int)
170              */

171             public void beginTask(String JavaDoc name, int totalWork) {
172                 final String JavaDoc finalName = name;
173                 final int finalWork = totalWork;
174                 runAsync(new Runnable JavaDoc() {
175                     /*
176                      * (non-Javadoc)
177                      *
178                      * @see java.lang.Runnable#run()
179                      */

180                     public void run() {
181                         getProgressMonitor().beginTask(finalName, finalWork);
182                     }
183                 });
184             }
185
186             /*
187              * (non-Javadoc)
188              *
189              * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#clearBlocked()
190              */

191             public void clearBlocked() {
192                 runAsync(new Runnable JavaDoc() {
193                     /*
194                      * (non-Javadoc)
195                      *
196                      * @see java.lang.Runnable#run()
197                      */

198                     public void run() {
199                         ((IProgressMonitorWithBlocking) getProgressMonitor())
200                                 .clearBlocked();
201                     }
202                 });
203             }
204
205             /*
206              * (non-Javadoc)
207              *
208              * @see org.eclipse.core.runtime.IProgressMonitor#done()
209              */

210             public void done() {
211                 runAsync(new Runnable JavaDoc() {
212                     /*
213                      * (non-Javadoc)
214                      *
215                      * @see java.lang.Runnable#run()
216                      */

217                     public void run() {
218                         getProgressMonitor().done();
219                     }
220                 });
221             }
222
223             /*
224              * (non-Javadoc)
225              *
226              * @see org.eclipse.core.runtime.IProgressMonitor#internalWorked(double)
227              */

228             public void internalWorked(double work) {
229                 final double finalWork = work;
230                 runAsync(new Runnable JavaDoc() {
231                     /*
232                      * (non-Javadoc)
233                      *
234                      * @see java.lang.Runnable#run()
235                      */

236                     public void run() {
237                         getProgressMonitor().internalWorked(finalWork);
238                     }
239                 });
240             }
241
242             /*
243              * (non-Javadoc)
244              *
245              * @see org.eclipse.core.runtime.IProgressMonitor#isCanceled()
246              */

247             public boolean isCanceled() {
248                 return getProgressMonitor().isCanceled();
249             }
250
251             /**
252              * Run the runnable as an asyncExec if we are already open.
253              *
254              * @param runnable
255              */

256             private void runAsync(final Runnable JavaDoc runnable) {
257
258                 if (alreadyClosed) {
259                     return;
260                 }
261                 Shell currentShell = getShell();
262
263                 Display display;
264                 if (currentShell == null) {
265                     display = Display.getDefault();
266                 } else {
267                     if (currentShell.isDisposed())// Don't bother if it has
268
// been closed
269
return;
270                     display = currentShell.getDisplay();
271                 }
272
273                 display.asyncExec(new Runnable JavaDoc() {
274                     /*
275                      * (non-Javadoc)
276                      *
277                      * @see java.lang.Runnable#run()
278                      */

279                     public void run() {
280                         if (alreadyClosed) {
281                             return;// Check again as the async may come too
282
// late
283
}
284                         Shell shell = getShell();
285                         if (shell != null && shell.isDisposed())
286                             return;
287
288                         runnable.run();
289                     }
290                 });
291             }
292
293             /*
294              * (non-Javadoc)
295              *
296              * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#setBlocked(org.eclipse.core.runtime.IStatus)
297              */

298             public void setBlocked(IStatus reason) {
299                 final IStatus finalReason = reason;
300                 runAsync(new Runnable JavaDoc() {
301                     /*
302                      * (non-Javadoc)
303                      *
304                      * @see java.lang.Runnable#run()
305                      */

306                     public void run() {
307                         ((IProgressMonitorWithBlocking) getProgressMonitor())
308                                 .setBlocked(finalReason);
309                     }
310                 });
311             }
312
313             /*
314              * (non-Javadoc)
315              *
316              * @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(boolean)
317              */

318             public void setCanceled(boolean value) {
319                 // Just a listener - doesn't matter.
320
}
321
322             /*
323              * (non-Javadoc)
324              *
325              * @see org.eclipse.core.runtime.IProgressMonitor#setTaskName(java.lang.String)
326              */

327             public void setTaskName(String JavaDoc name) {
328                 final String JavaDoc finalName = name;
329                 runAsync(new Runnable JavaDoc() {
330                     /*
331                      * (non-Javadoc)
332                      *
333                      * @see java.lang.Runnable#run()
334                      */

335                     public void run() {
336                         getProgressMonitor().setTaskName(finalName);
337                     }
338                 });
339             }
340
341             /*
342              * (non-Javadoc)
343              *
344              * @see org.eclipse.core.runtime.IProgressMonitor#subTask(java.lang.String)
345              */

346             public void subTask(String JavaDoc name) {
347                 final String JavaDoc finalName = name;
348                 runAsync(new Runnable JavaDoc() {
349                     /*
350                      * (non-Javadoc)
351                      *
352                      * @see java.lang.Runnable#run()
353                      */

354                     public void run() {
355                         getProgressMonitor().subTask(finalName);
356                     }
357                 });
358             }
359
360             /*
361              * (non-Javadoc)
362              *
363              * @see org.eclipse.core.runtime.IProgressMonitor#worked(int)
364              */

365             public void worked(int work) {
366                 internalWorked(work);
367             }
368         };
369     }
370
371     /*
372      * (non-Javadoc)
373      *
374      * @see org.eclipse.jface.window.Window#open()
375      */

376     public int open() {
377         int result = super.open();
378
379         // add a listener that will close the dialog when the job completes.
380
IJobChangeListener listener = createCloseListener();
381         job.addJobChangeListener(listener);
382         if (job.getState() == Job.NONE) {
383             // if the job completed before we had a chance to add
384
// the listener, just remove the listener and return
385
job.removeJobChangeListener(listener);
386             finishedRun();
387             cleanUpFinishedJob();
388         }
389
390         return result;
391     }
392
393     /**
394      * Opens this dialog for the duration that the given job is running.
395      *
396      * @param jobToWatch
397      * @param originatingShell
398      * The shell this request was created from. Do not block on this
399      * shell.
400      */

401     public void show(Job jobToWatch, final Shell originatingShell) {
402         job = jobToWatch;
403         // after the dialog is opened we can get access to its monitor
404
job.setProperty(IProgressConstants.PROPERTY_IN_DIALOG, Boolean.TRUE);
405
406         ProgressManager.getInstance().progressFor(job).addProgressListener(
407                 getBlockingProgressMonitor());
408
409         setOpenOnRun(false);
410         aboutToRun();
411         // start with a quick busy indicator. Lock the UI as we
412
// want to preserve modality
413
BusyIndicator.showWhile(PlatformUI.getWorkbench().getDisplay(),
414                 new Runnable JavaDoc() {
415                     public void run() {
416                         try {
417                             Thread
418                                     .sleep(ProgressManagerUtil.SHORT_OPERATION_TIME);
419                         } catch (InterruptedException JavaDoc e) {
420                             // Do not log as this is a common operation from the
421
// lock listener
422
}
423                     }
424                 });
425
426         WorkbenchJob openJob = new WorkbenchJob(
427                 ProgressMessages.ProgressMonitorFocusJobDialog_UserDialogJob) {
428             /*
429              * (non-Javadoc)
430              *
431              * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
432              */

433             public IStatus runInUIThread(IProgressMonitor monitor) {
434
435                 // if the job is done at this point, we don't need the dialog
436
if (job.getState() == Job.NONE) {
437                     finishedRun();
438                     cleanUpFinishedJob();
439                     return Status.CANCEL_STATUS;
440                 }
441
442                 // now open the progress dialog if nothing else is
443
if (!ProgressManagerUtil.safeToOpen(
444                         ProgressMonitorFocusJobDialog.this, originatingShell)) {
445                     return Status.CANCEL_STATUS;
446                 }
447
448                 // Do not bother if the parent is disposed
449
if (getParentShell() != null && getParentShell().isDisposed()) {
450                     return Status.CANCEL_STATUS;
451                 }
452
453                 open();
454
455                 return Status.OK_STATUS;
456             }
457         };
458         openJob.setSystem(true);
459         openJob.schedule();
460
461     }
462
463     /**
464      * The job finished before we did anything so clean up the finished
465      * reference.
466      */

467     private void cleanUpFinishedJob() {
468         ProgressManager.getInstance().checkForStaleness(job);
469     }
470
471     /*
472      * (non-Javadoc)
473      *
474      * @see org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
475      */

476     protected Control createDialogArea(Composite parent) {
477         Control area = super.createDialogArea(parent);
478         // Give the job info as the initial details
479
getProgressMonitor().setTaskName(
480                 ProgressManager.getInstance().getJobInfo(this.job)
481                         .getDisplayString());
482         return area;
483     }
484 }
485
Popular Tags