1 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 24 class WorkbenchMonitorProvider { 25 31 IProgressMonitor getMonitor(Job job) { 32 if (job instanceof UIJob) 33 return getUIProgressMonitor(job.getName()); 34 else 35 return new NullProgressMonitor(); 36 } 37 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 59 private IProgressMonitor getUIProgressMonitor(final String jobName) { 60 return new IProgressMonitor() { 61 IProgressMonitor internalMonitor; 62 66 public void beginTask(String name, int totalWork) { 67 if (name == null || name.length() == 0) 68 getInternalMonitor().beginTask(jobName, totalWork); 69 else 70 getInternalMonitor().beginTask(name, totalWork); 71 } 72 75 public void done() { 76 getInternalMonitor().done(); 77 } 78 81 public void internalWorked(double work) { 82 getInternalMonitor().internalWorked(work); 83 } 84 87 public boolean isCanceled() { 88 return getInternalMonitor().isCanceled(); 89 } 90 93 public void setCanceled(boolean value) { 94 getInternalMonitor().setCanceled(value); 95 } 96 99 public void setTaskName(String name) { 100 getInternalMonitor().setTaskName(name); 101 } 102 105 public void subTask(String name) { 106 getInternalMonitor().subTask(name); 107 } 108 111 public void worked(int work) { 112 getInternalMonitor().worked(work); 113 } 114 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 |