1 11 package org.eclipse.ui.internal.dialogs; 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.swt.widgets.Display; 19 import org.eclipse.ui.internal.ExceptionHandler; 20 21 29 public class EventLoopProgressMonitor extends ProgressMonitorWrapper implements 30 IProgressMonitorWithBlocking { 31 34 private static int T_THRESH = 100; 35 36 39 private static int T_MAX = 50; 40 41 44 private long lastTime = System.currentTimeMillis(); 45 46 50 private String taskName; 51 52 56 public EventLoopProgressMonitor(IProgressMonitor monitor) { 57 super(monitor); 58 } 59 60 63 public void beginTask(String name, int totalWork) { 64 super.beginTask(name, totalWork); 65 taskName = name; 66 runEventLoop(); 67 } 68 69 72 public void clearBlocked() { 73 Dialog.getBlockedHandler().clearBlocked(); 74 } 75 76 79 public void done() { 80 super.done(); 81 taskName = null; 82 runEventLoop(); 83 } 84 85 88 public void internalWorked(double work) { 89 super.internalWorked(work); 90 runEventLoop(); 91 } 92 93 96 public boolean isCanceled() { 97 runEventLoop(); 98 return super.isCanceled(); 99 } 100 101 104 private void runEventLoop() { 105 long t = System.currentTimeMillis(); 108 if (t - lastTime < T_THRESH) { 109 return; 110 } 111 lastTime = t; 112 Display disp = Display.getDefault(); 114 if (disp == null) { 115 return; 116 } 117 118 ExceptionHandler handler = ExceptionHandler.getInstance(); 120 121 for (;;) { 122 try { 123 if (!disp.readAndDispatch()) { 124 break; 125 } 126 } catch (Throwable e) { handler.handleException(e); 128 break; 129 } 130 131 if (System.currentTimeMillis() - t > T_MAX) { 135 break; 136 } 137 } 138 } 139 140 143 public void setBlocked(IStatus reason) { 144 Dialog.getBlockedHandler().showBlocked(this, reason, taskName); 145 } 146 147 150 public void setCanceled(boolean b) { 151 super.setCanceled(b); 152 taskName = null; 153 runEventLoop(); 154 } 155 156 159 public void setTaskName(String name) { 160 super.setTaskName(name); 161 taskName = name; 162 runEventLoop(); 163 } 164 165 168 public void subTask(String name) { 169 if (taskName == null) { 171 taskName = name; 172 } 173 super.subTask(name); 174 runEventLoop(); 175 } 176 177 180 public void worked(int work) { 181 super.worked(work); 182 runEventLoop(); 183 } 184 185 189 protected String getTaskName() { 190 return taskName; 191 } 192 } 193 | Popular Tags |