KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.ui.internal.progress;
12 import org.eclipse.core.runtime.IProgressMonitor;
13 import org.eclipse.core.runtime.NullProgressMonitor;
14 import org.eclipse.core.runtime.jobs.Job;
15 import org.eclipse.jface.action.IStatusLineManager;
16 import org.eclipse.ui.IWorkbenchWindow;
17 import org.eclipse.ui.PlatformUI;
18 import org.eclipse.ui.progress.UIJob;
19 import org.eclipse.ui.internal.WorkbenchWindow;
20 /**
21  * The WorkbenchProgressListener is a class that listens to progress on the
22  * workbench and reports accordingly.
23  */

24 class WorkbenchMonitorProvider {
25     /**
26      * Get the progress monitor for a job. If it is a UIJob get the main
27      * monitor from the status line. Otherwise return no monitor.
28      *
29      * @return IProgressMonitor
30      */

31     IProgressMonitor getMonitor(Job job) {
32         if (job instanceof UIJob)
33             return getUIProgressMonitor(job.getName());
34         else
35             return new NullProgressMonitor();
36     }
37     /**
38      * Return the status line manager if there is one.
39      *
40      * @return IStatusLineWithProgressManager
41      */

42     private IStatusLineManager getStatusLineManager() {
43         if (PlatformUI.isWorkbenchRunning()) {
44             IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
45             if (window != null && window instanceof WorkbenchWindow) {
46                 return ((WorkbenchWindow) window).getStatusLineManager();
47             }
48         }
49         return null;
50     }
51     /**
52      * Get a progress monitor for use with UIThreads. This monitor will use the
53      * status line directly if possible.
54      *
55      * @param jobName.
56      * Used if the task name is null.
57      * @return IProgressMonitor
58      */

59     private IProgressMonitor getUIProgressMonitor(final String JavaDoc jobName) {
60         return new IProgressMonitor() {
61             IProgressMonitor internalMonitor;
62             /*
63              * (non-Javadoc) @see org.eclipse.core.runtime.IProgressMonitor#beginTask(java.lang.String,
64              * int)
65              */

66             public void beginTask(String JavaDoc name, int totalWork) {
67                 if (name == null || name.length() == 0)
68                     getInternalMonitor().beginTask(jobName, totalWork);
69                 else
70                     getInternalMonitor().beginTask(name, totalWork);
71             }
72             /*
73              * (non-Javadoc) @see org.eclipse.core.runtime.IProgressMonitor#done()
74              */

75             public void done() {
76                 getInternalMonitor().done();
77             }
78             /*
79              * (non-Javadoc) @see org.eclipse.core.runtime.IProgressMonitor#internalWorked(double)
80              */

81             public void internalWorked(double work) {
82                 getInternalMonitor().internalWorked(work);
83             }
84             /*
85              * (non-Javadoc) @see org.eclipse.core.runtime.IProgressMonitor#isCanceled()
86              */

87             public boolean isCanceled() {
88                 return getInternalMonitor().isCanceled();
89             }
90             /*
91              * (non-Javadoc) @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(boolean)
92              */

93             public void setCanceled(boolean value) {
94                 getInternalMonitor().setCanceled(value);
95             }
96             /*
97              * (non-Javadoc) @see org.eclipse.core.runtime.IProgressMonitor#setTaskName(java.lang.String)
98              */

99             public void setTaskName(String JavaDoc name) {
100                 getInternalMonitor().setTaskName(name);
101             }
102             /*
103              * (non-Javadoc) @see org.eclipse.core.runtime.IProgressMonitor#subTask(java.lang.String)
104              */

105             public void subTask(String JavaDoc name) {
106                 getInternalMonitor().subTask(name);
107             }
108             /*
109              * (non-Javadoc) @see org.eclipse.core.runtime.IProgressMonitor#worked(int)
110              */

111             public void worked(int work) {
112                 getInternalMonitor().worked(work);
113             }
114             /**
115              * Get the monitor that is being wrapped. This is called lazily as
116              * we will not be able to get the monitor for the workbench outside
117              * of the UI Thread and so we will have to wait until the monitor
118              * is accessed.
119              *
120              * Return a NullProgressMonitor if the one from the workbench
121              * cannot be found.
122              *
123              * @return IProgressMonitor
124              */

125             private IProgressMonitor getInternalMonitor() {
126                 if (internalMonitor == null) {
127                     IStatusLineManager manager = getStatusLineManager();
128                     if (manager == null)
129                         internalMonitor = new NullProgressMonitor();
130                     else
131                         internalMonitor = manager.getProgressMonitor();
132                 }
133                 return internalMonitor;
134             }
135         };
136     }
137 }
138
Popular Tags