1 11 package org.eclipse.ui.internal.operations; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 18 import org.eclipse.jface.operation.IRunnableWithProgress; 19 import org.eclipse.swt.custom.BusyIndicator; 20 import org.eclipse.swt.widgets.Display; 21 import org.eclipse.swt.widgets.Shell; 22 import org.eclipse.ui.PlatformUI; 23 24 30 public class TimeTriggeredProgressMonitorDialog extends ProgressMonitorDialog { 31 32 35 private int longOperationTime; 36 37 40 private long triggerTime = -1; 41 42 45 private boolean dialogOpened = false; 46 47 51 private IProgressMonitor wrapperedMonitor; 52 53 62 public TimeTriggeredProgressMonitorDialog(Shell parent, 63 int longOperationTime) { 64 super(parent); 65 setOpenOnRun(false); 66 this.longOperationTime = longOperationTime; 67 } 68 69 73 public void createWrapperedMonitor() { 74 wrapperedMonitor = new IProgressMonitor() { 75 76 IProgressMonitor superMonitor = TimeTriggeredProgressMonitorDialog.super 77 .getProgressMonitor(); 78 79 85 public void beginTask(String name, int totalWork) { 86 superMonitor.beginTask(name, totalWork); 87 checkTicking(); 88 } 89 90 93 private void checkTicking() { 94 if (triggerTime < 0) { 95 triggerTime = System.currentTimeMillis() + longOperationTime; 96 } 97 if (!dialogOpened && System.currentTimeMillis() > triggerTime) { 98 open(); 99 dialogOpened = true; 100 } 101 } 102 103 104 105 110 public void done() { 111 superMonitor.done(); 112 checkTicking(); 113 } 114 115 120 public void internalWorked(double work) { 121 superMonitor.internalWorked(work); 122 checkTicking(); 123 } 124 125 130 public boolean isCanceled() { 131 return superMonitor.isCanceled(); 132 } 133 134 139 public void setCanceled(boolean value) { 140 superMonitor.setCanceled(value); 141 142 } 143 144 149 public void setTaskName(String name) { 150 superMonitor.setTaskName(name); 151 checkTicking(); 152 153 } 154 155 160 public void subTask(String name) { 161 superMonitor.subTask(name); 162 checkTicking(); 163 } 164 165 170 public void worked(int work) { 171 superMonitor.worked(work); 172 checkTicking(); 173 174 } 175 }; 176 } 177 178 183 public IProgressMonitor getProgressMonitor() { 184 if (wrapperedMonitor == null) { 185 createWrapperedMonitor(); 186 } 187 return wrapperedMonitor; 188 } 189 190 195 public void run(final boolean fork, final boolean cancelable, 196 final IRunnableWithProgress runnable) throws InvocationTargetException , 197 InterruptedException { 198 final InvocationTargetException [] invokes = new InvocationTargetException [1]; 199 final InterruptedException [] interrupt = new InterruptedException [1]; 200 Runnable dialogWaitRunnable = new Runnable () { 201 public void run() { 202 try { 203 TimeTriggeredProgressMonitorDialog.super.run(fork, cancelable, runnable); 204 } catch (InvocationTargetException e) { 205 invokes[0] = e; 206 } catch (InterruptedException e) { 207 interrupt[0]= e; 208 } 209 } 210 }; 211 final Display display = PlatformUI.getWorkbench().getDisplay(); 212 if (display == null) { 213 return; 214 } 215 BusyIndicator.showWhile(display, dialogWaitRunnable); 217 if (invokes[0] != null) { 218 throw invokes[0]; 219 } 220 if (interrupt[0] != null) { 221 throw interrupt[0]; 222 } 223 } 224 } 225 | Popular Tags |