KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IProgressMonitorWithBlocking;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.jface.viewers.ViewerComparator;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Cursor;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.internal.misc.Policy;
37
38 /**
39  * The ProgressMonitorJobsDialog is the progress monitor dialog used by the
40  * progress service to allow locks to show the current jobs.
41  */

42 public class ProgressMonitorJobsDialog extends ProgressMonitorDialog {
43     private DetailedProgressViewer viewer;
44
45     /**
46      * The height of the viewer. Set when the details button is selected.
47      */

48     private int viewerHeight = -1;
49
50     Composite viewerComposite;
51
52     private Button detailsButton;
53
54     private long watchTime = -1;
55
56     protected boolean alreadyClosed = false;
57
58     private IProgressMonitor wrapperedMonitor;
59
60     //Cache initial enablement in case the enablement state is set
61
//before the button is created
62
protected boolean enableDetailsButton = false;
63
64     /**
65      * Create a new instance of the receiver.
66      *
67      * @param parent
68      */

69     public ProgressMonitorJobsDialog(Shell parent) {
70         super(parent);
71         setShellStyle(getShellStyle() | SWT.RESIZE);
72     }
73
74     /*
75      * (non-Javadoc)
76      *
77      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
78      */

79     protected Control createDialogArea(Composite parent) {
80         Composite top = (Composite) super.createDialogArea(parent);
81         viewerComposite = new Composite(parent, SWT.NONE);
82         GridLayout layout = new GridLayout();
83         layout.marginHeight = 0;
84         layout.marginWidth = 0;
85         viewerComposite.setLayout(layout);
86         GridData viewerData = new GridData(GridData.FILL_BOTH);
87         viewerData.horizontalSpan = 2;
88         viewerData.heightHint = 0;
89         viewerComposite.setLayoutData(viewerData);
90         return top;
91     }
92
93     /**
94      * The details button has been selected. Open or close the progress viewer
95      * as appropriate.
96      *
97      */

98     void handleDetailsButtonSelect() {
99         Shell shell = getShell();
100         Point shellSize = shell.getSize();
101         Composite composite = (Composite) getDialogArea();
102         if (viewer != null) {
103             viewer.getControl().dispose();
104             viewer = null;
105             composite.layout();
106             shell.setSize(shellSize.x, shellSize.y - viewerHeight);
107             detailsButton.setText(ProgressMessages.ProgressMonitorJobsDialog_DetailsTitle);
108         } else {
109             //Abort if there are no jobs visible
110
if (ProgressManager.getInstance().getRootElements(Policy.DEBUG_SHOW_ALL_JOBS).length == 0) {
111                 detailsButton.setEnabled(false);
112                 return;
113             }
114
115             viewer = new DetailedProgressViewer(viewerComposite, SWT.MULTI
116                     | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
117             viewer.setComparator(new ViewerComparator() {
118                 /*
119                  * (non-Javadoc)
120                  *
121                  * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer,
122                  * java.lang.Object, java.lang.Object)
123                  */

124                 public int compare(Viewer testViewer, Object JavaDoc e1, Object JavaDoc e2) {
125                     return ((Comparable JavaDoc) e1).compareTo(e2);
126                 }
127             });
128
129             viewer.setContentProvider(new ProgressViewerContentProvider(viewer,true,false){
130                 public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
131                     return super.getElements(inputElement);
132                 }}
133             );
134             
135             viewer.setLabelProvider(new ProgressLabelProvider());
136             viewer.setInput(this);
137             GridData viewerData = new GridData(GridData.FILL_BOTH);
138             viewer.getControl().setLayoutData(viewerData);
139             GridData viewerCompositeData = (GridData) viewerComposite.getLayoutData();
140             viewerCompositeData.heightHint = convertHeightInCharsToPixels(10);
141             viewerComposite.layout(true);
142             viewer.getControl().setVisible(true);
143             viewerHeight = viewerComposite.computeTrim(0, 0, 0, viewerCompositeData.heightHint).height;
144             detailsButton.setText(ProgressMessages.ProgressMonitorJobsDialog_HideTitle);
145             shell.setSize(shellSize.x, shellSize.y + viewerHeight);
146         }
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
153      */

154     protected void createButtonsForButtonBar(Composite parent) {
155         super.createButtonsForButtonBar(parent);
156         createDetailsButton(parent);
157     }
158
159     /**
160      * Create a spacer label to get the layout to not bunch the widgets.
161      *
162      * @param parent
163      * The parent of the new button.
164      */

165     protected void createSpacer(Composite parent) {
166         //Make a label to force the spacing
167
Label spacer = new Label(parent, SWT.NONE);
168         spacer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
169                 | GridData.GRAB_HORIZONTAL));
170     }
171
172     /**
173      * Create the details button for the receiver.
174      *
175      * @param parent
176      * The parent of the new button.
177      */

178     protected void createDetailsButton(Composite parent) {
179         detailsButton = createButton(parent, IDialogConstants.DETAILS_ID,
180                 ProgressMessages.ProgressMonitorJobsDialog_DetailsTitle,
181                 false);
182         detailsButton.addSelectionListener(new SelectionAdapter() {
183             /*
184              * (non-Javadoc)
185              *
186              * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
187              */

188             public void widgetSelected(SelectionEvent e) {
189                 handleDetailsButtonSelect();
190             }
191         });
192         detailsButton.setCursor(arrowCursor);
193         detailsButton.setEnabled(enableDetailsButton);
194     }
195
196     /*
197      * (non-Javadoc)
198      *
199      * @see org.eclipse.jface.dialogs.IconAndMessageDialog#createButtonBar(org.eclipse.swt.widgets.Composite)
200      */

201     protected Control createButtonBar(Composite parent) {
202         Composite composite = new Composite(parent, SWT.NONE);
203         // create a layout with spacing and margins appropriate for the font
204
// size.
205
GridLayout layout = new GridLayout();
206         layout.numColumns = 1; // this is incremented by createButton
207
layout.makeColumnsEqualWidth = false;
208         layout.marginWidth = 0;
209         layout.marginHeight = 0;
210         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
211         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
212         composite.setLayout(layout);
213         GridData data = new GridData(GridData.FILL_HORIZONTAL);
214         data.horizontalSpan = 2;
215         data.horizontalAlignment = GridData.END;
216         data.grabExcessHorizontalSpace = true;
217         composite.setLayoutData(data);
218         composite.setFont(parent.getFont());
219         // Add the buttons to the button bar.
220
if (arrowCursor == null) {
221             arrowCursor = new Cursor(parent.getDisplay(), SWT.CURSOR_ARROW);
222         }
223         createButtonsForButtonBar(composite);
224         return composite;
225     }
226
227     /*
228      * (non-Javadoc)
229      *
230      * @see org.eclipse.jface.dialogs.ProgressMonitorDialog#clearCursors()
231      */

232     protected void clearCursors() {
233         if (detailsButton != null && !detailsButton.isDisposed()) {
234             detailsButton.setCursor(null);
235         }
236         super.clearCursors();
237     }
238
239     /*
240      * (non-Javadoc)
241      *
242      * @see org.eclipse.jface.dialogs.ProgressMonitorDialog#updateForSetBlocked(org.eclipse.core.runtime.IStatus)
243      */

244     protected void updateForSetBlocked(IStatus reason) {
245         if(alreadyClosed)
246             return;
247         
248         super.updateForSetBlocked(reason);
249         enableDetails(true);
250         if (viewer == null) {
251             handleDetailsButtonSelect();
252         }
253     }
254
255     /*
256      * (non-Javadoc)
257      *
258      * @see org.eclipse.jface.dialogs.ProgressMonitorDialog#run(boolean,
259      * boolean, org.eclipse.jface.operation.IRunnableWithProgress)
260      */

261     public void run(boolean fork, boolean cancelable,
262             IRunnableWithProgress runnable) throws InvocationTargetException JavaDoc,
263             InterruptedException JavaDoc {
264         //if it is run in the UI Thread don't do anything.
265
if (!fork) {
266             enableDetails(false);
267         }
268         super.run(fork, cancelable, runnable);
269     }
270
271     /**
272      * Set the enable state of the details button now or when it will be
273      * created.
274      *
275      * @param enableState
276      * a boolean to indicate the preferred' state
277      */

278     protected void enableDetails(boolean enableState) {
279         if (detailsButton == null) {
280             enableDetailsButton = enableState;
281         } else {
282             detailsButton.setEnabled(enableState);
283         }
284     }
285
286     /**
287      * Start watching the ticks. When the long operation time has
288      * passed open the dialog.
289      */

290     public void watchTicks() {
291         watchTime = System.currentTimeMillis();
292     }
293
294     /**
295      * Create a monitor for the receiver that wrappers the superclasses monitor.
296      *
297      */

298     public void createWrapperedMonitor() {
299         wrapperedMonitor = new IProgressMonitorWithBlocking() {
300
301             IProgressMonitor superMonitor = ProgressMonitorJobsDialog.super
302                     .getProgressMonitor();
303
304             /*
305              * (non-Javadoc)
306              *
307              * @see org.eclipse.core.runtime.IProgressMonitor#beginTask(java.lang.String,
308              * int)
309              */

310             public void beginTask(String JavaDoc name, int totalWork) {
311                 superMonitor.beginTask(name, totalWork);
312                 checkTicking();
313             }
314
315             /**
316              * Check if we have ticked in the last 800ms.
317              */

318             private void checkTicking() {
319                 if (watchTime < 0) {
320                     return;
321                 }
322                 if ((System.currentTimeMillis() - watchTime) > ProgressManager
323                         .getInstance().getLongOperationTime()) {
324                     watchTime = -1;
325                     openDialog();
326                 }
327             }
328
329             /**
330              * Open the dialog in the ui Thread
331              */

332             private void openDialog() {
333                 if (!PlatformUI.isWorkbenchRunning()) {
334                     return;
335                 }
336
337                 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable JavaDoc() {
338                     /* (non-Javadoc)
339                      * @see java.lang.Runnable#run()
340                      */

341                     public void run() {
342                         //Reset the watch if it is not safe to open
343
if (!ProgressManagerUtil.safeToOpen(ProgressMonitorJobsDialog.this,null)){
344                               watchTicks();
345                               return;
346                          }
347                              
348                         if (!alreadyClosed) {
349                             open();
350                         }
351                     }
352                 });
353             }
354
355             /*
356              * (non-Javadoc)
357              *
358              * @see org.eclipse.core.runtime.IProgressMonitor#done()
359              */

360             public void done() {
361                 superMonitor.done();
362                 checkTicking();
363             }
364
365             /*
366              * (non-Javadoc)
367              *
368              * @see org.eclipse.core.runtime.IProgressMonitor#internalWorked(double)
369              */

370             public void internalWorked(double work) {
371                 superMonitor.internalWorked(work);
372                 checkTicking();
373             }
374
375             /*
376              * (non-Javadoc)
377              *
378              * @see org.eclipse.core.runtime.IProgressMonitor#isCanceled()
379              */

380             public boolean isCanceled() {
381                 return superMonitor.isCanceled();
382             }
383
384             /*
385              * (non-Javadoc)
386              *
387              * @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(boolean)
388              */

389             public void setCanceled(boolean value) {
390                 superMonitor.setCanceled(value);
391
392             }
393
394             /*
395              * (non-Javadoc)
396              *
397              * @see org.eclipse.core.runtime.IProgressMonitor#setTaskName(java.lang.String)
398              */

399             public void setTaskName(String JavaDoc name) {
400                 superMonitor.setTaskName(name);
401                 checkTicking();
402
403             }
404
405             /*
406              * (non-Javadoc)
407              *
408              * @see org.eclipse.core.runtime.IProgressMonitor#subTask(java.lang.String)
409              */

410             public void subTask(String JavaDoc name) {
411                 superMonitor.subTask(name);
412                 checkTicking();
413             }
414
415             /*
416              * (non-Javadoc)
417              *
418              * @see org.eclipse.core.runtime.IProgressMonitor#worked(int)
419              */

420             public void worked(int work) {
421                 superMonitor.worked(work);
422                 checkTicking();
423
424             }
425
426             /*
427              * (non-Javadoc)
428              *
429              * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#clearBlocked()
430              */

431             public void clearBlocked() {
432                 //We want to open on blocking too
433
if (superMonitor instanceof IProgressMonitorWithBlocking) {
434                     ((IProgressMonitorWithBlocking) superMonitor)
435                             .clearBlocked();
436                 }
437
438             }
439
440             /*
441              * (non-Javadoc)
442              *
443              * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#setBlocked(org.eclipse.core.runtime.IStatus)
444              */

445             public void setBlocked(IStatus reason) {
446                 openDialog();
447                 if (superMonitor instanceof IProgressMonitorWithBlocking) {
448                     ((IProgressMonitorWithBlocking) superMonitor)
449                             .setBlocked(reason);
450                 }
451
452             }
453
454         };
455     }
456
457     /*
458      * (non-Javadoc)
459      *
460      * @see org.eclipse.jface.dialogs.ProgressMonitorDialog#getProgressMonitor()
461      */

462     public IProgressMonitor getProgressMonitor() {
463         if (wrapperedMonitor == null) {
464             createWrapperedMonitor();
465         }
466         return wrapperedMonitor;
467     }
468
469     /*
470      * (non-Javadoc)
471      *
472      * @see org.eclipse.jface.dialogs.ProgressMonitorDialog#close()
473      */

474     public boolean close() {
475         alreadyClosed = true;//As this sometimes delayed cache if it was already closed
476
boolean result = super.close();
477         if (!result) {//If it fails reset the flag
478
alreadyClosed = false;
479         }
480         return result;
481     }
482
483 }
484
Popular Tags