1 11 package org.eclipse.jface.operation; 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.ProgressMonitorWrapper; 17 import org.eclipse.jface.dialogs.Dialog; 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.swt.widgets.Display; 20 21 39 class AccumulatingProgressMonitor extends ProgressMonitorWrapper { 40 41 44 private Display display; 45 46 49 private Collector collector; 50 51 private String currentTask = ""; 53 private class Collector implements Runnable { 54 private String subTask; 55 56 private double worked; 57 58 private IProgressMonitor monitor; 59 60 66 public Collector(String subTask, double work, IProgressMonitor monitor) { 67 this.subTask = subTask; 68 this.worked = work; 69 this.monitor = monitor; 70 } 71 72 76 public void worked(double workedIncrement) { 77 this.worked = this.worked + workedIncrement; 78 } 79 80 84 public void subTask(String subTaskName) { 85 this.subTask = subTaskName; 86 } 87 88 91 public void run() { 92 clearCollector(this); 93 if (subTask != null) { 94 monitor.subTask(subTask); 95 } 96 if (worked > 0) { 97 monitor.internalWorked(worked); 98 } 99 } 100 } 101 102 110 public AccumulatingProgressMonitor(IProgressMonitor monitor, Display display) { 111 super(monitor); 112 Assert.isNotNull(display); 113 this.display = display; 114 } 115 116 119 public void beginTask(final String name, final int totalWork) { 120 synchronized (this) { 121 collector = null; 122 } 123 display.syncExec(new Runnable () { 124 public void run() { 125 currentTask = name; 126 getWrappedProgressMonitor().beginTask(name, totalWork); 127 } 128 }); 129 } 130 131 136 private synchronized void clearCollector(Collector collectorToClear) { 137 if (this.collector == collectorToClear) { 140 this.collector = null; 141 } 142 } 143 144 149 private void createCollector(String subTask, double work) { 150 collector = new Collector(subTask, work, getWrappedProgressMonitor()); 151 display.asyncExec(collector); 152 } 153 154 157 public void done() { 158 synchronized (this) { 159 collector = null; 160 } 161 display.syncExec(new Runnable () { 162 public void run() { 163 getWrappedProgressMonitor().done(); 164 } 165 }); 166 } 167 168 171 public synchronized void internalWorked(final double work) { 172 if (collector == null) { 173 createCollector(null, work); 174 } else { 175 collector.worked(work); 176 } 177 } 178 179 182 public void setTaskName(final String name) { 183 synchronized (this) { 184 collector = null; 185 } 186 display.syncExec(new Runnable () { 187 public void run() { 188 currentTask = name; 189 getWrappedProgressMonitor().setTaskName(name); 190 } 191 }); 192 } 193 194 197 public synchronized void subTask(final String name) { 198 if (collector == null) { 199 createCollector(name, 0); 200 } else { 201 collector.subTask(name); 202 } 203 } 204 205 208 public synchronized void worked(int work) { 209 internalWorked(work); 210 } 211 212 215 public void clearBlocked() { 216 217 final IProgressMonitor pm = getWrappedProgressMonitor(); 221 if (!(pm instanceof IProgressMonitorWithBlocking)) { 222 return; 223 } 224 225 display.asyncExec(new Runnable () { 226 229 public void run() { 230 ((IProgressMonitorWithBlocking) pm).clearBlocked(); 231 Dialog.getBlockedHandler().clearBlocked(); 232 } 233 }); 234 } 235 236 239 public void setBlocked(final IStatus reason) { 240 final IProgressMonitor pm = getWrappedProgressMonitor(); 244 if (!(pm instanceof IProgressMonitorWithBlocking)) { 245 return; 246 } 247 248 display.asyncExec(new Runnable () { 249 252 public void run() { 253 ((IProgressMonitorWithBlocking) pm).setBlocked(reason); 254 Dialog.getBlockedHandler().showBlocked(pm, reason, currentTask); 256 } 257 }); 258 } 259 } 260 | Popular Tags |