1 13 package org.eclipse.jface.util; 14 15 import org.eclipse.core.runtime.ISafeRunnable; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.OperationCanceledException; 18 import org.eclipse.core.runtime.Status; 19 import org.eclipse.jface.resource.JFaceResources; 20 import org.eclipse.swt.events.DisposeEvent; 21 import org.eclipse.swt.events.DisposeListener; 22 import org.eclipse.swt.widgets.Display; 23 24 32 public abstract class SafeRunnable implements ISafeRunnable { 33 34 private static boolean ignoreErrors = false; 35 36 private static ISafeRunnableRunner runner; 37 38 private String message; 39 40 private static SafeRunnableDialog dialog; 41 42 45 public SafeRunnable() { 46 } 48 49 55 public SafeRunnable(String message) { 56 this.message = message; 57 } 58 59 60 63 public void handleException(Throwable e) { 64 if (!ignoreErrors) { 67 if (message == null) 68 message = JFaceResources.getString("SafeRunnable.errorMessage"); 70 final IStatus status = new Status(IStatus.ERROR, Policy.JFACE, message,e); 71 72 Runnable runnable = new Runnable () { 73 public void run() { 74 if (dialog == null || dialog.getShell().isDisposed()) { 75 dialog = new SafeRunnableDialog(status); 76 dialog.create(); 77 dialog.getShell().addDisposeListener( 78 new DisposeListener() { 79 public void widgetDisposed(DisposeEvent e) { 80 dialog = null; 81 } 82 }); 83 dialog.open(); 84 } else { 85 dialog.addStatus(status); 86 dialog.refresh(); 87 } 88 } 89 }; 90 if (Display.getCurrent() != null) { 91 runnable.run(); 92 } else { 93 Display.getDefault().asyncExec(runnable); 94 } 95 } 96 } 97 98 105 public static boolean getIgnoreErrors(boolean flag) { 106 return ignoreErrors; 107 } 108 109 116 public static boolean getIgnoreErrors() { 117 return ignoreErrors; 118 } 119 120 126 public static void setIgnoreErrors(boolean flag) { 127 ignoreErrors = flag; 128 } 129 130 137 public static ISafeRunnableRunner getRunner() { 138 if (runner == null) { 139 runner = createDefaultRunner(); 140 } 141 return runner; 142 } 143 144 150 private static ISafeRunnableRunner createDefaultRunner() { 151 return new ISafeRunnableRunner() { 152 public void run(ISafeRunnable code) { 153 try { 154 code.run(); 155 } catch (Exception e) { 156 handleException(code, e); 157 } catch (LinkageError e) { 158 handleException(code, e); 159 } 160 } 161 162 private void handleException(ISafeRunnable code, Throwable e) { 163 if (!(e instanceof OperationCanceledException)) { 164 try { 165 Policy.getLog().log( 166 new Status(IStatus.ERROR, Policy.JFACE, 167 IStatus.ERROR, "Exception occurred", e)); } catch (Exception ex) { 169 e.printStackTrace(); 170 } 171 } 172 code.handleException(e); 173 } 174 }; 175 } 176 177 185 public static void setRunner(ISafeRunnableRunner runner) { 186 SafeRunnable.runner = runner; 187 } 188 189 198 public static void run(ISafeRunnable runnable) { 199 getRunner().run(runnable); 200 } 201 202 } 203 | Popular Tags |