1 11 package org.eclipse.ui.internal.ide; 12 13 import com.ibm.icu.text.MessageFormat; 14 15 import org.eclipse.jface.dialogs.IDialogConstants; 16 import org.eclipse.jface.dialogs.MessageDialog; 17 import org.eclipse.jface.window.Window; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.SWTError; 21 import org.eclipse.swt.widgets.MessageBox; 22 import org.eclipse.swt.widgets.Shell; 23 import org.eclipse.ui.application.IWorkbenchConfigurer; 24 import org.eclipse.ui.internal.ide.dialogs.InternalErrorDialog; 25 26 36 public final class IDEExceptionHandler { 37 38 private int exceptionCount = 0; 39 40 private InternalErrorDialog dialog; 41 42 private Shell defaultParent = new Shell(); 43 44 private boolean closing = false; 45 46 private IWorkbenchConfigurer workbenchConfigurer; 47 48 private static String MSG_OutOfMemoryError = IDEWorkbenchMessages.FatalError_OutOfMemoryError; 50 51 private static String MSG_StackOverflowError = IDEWorkbenchMessages.FatalError_StackOverflowError; 52 53 private static String MSG_VirtualMachineError = IDEWorkbenchMessages.FatalError_VirtualMachineError; 54 55 private static String MSG_SWTError = IDEWorkbenchMessages.FatalError_SWTError; 56 57 private static String MSG_FATAL_ERROR = IDEWorkbenchMessages.FatalError; 58 59 private static String MSG_FATAL_ERROR_Recursive = IDEWorkbenchMessages.FatalError_RecursiveError; 60 61 private static String MSG_FATAL_ERROR_RecursiveTitle = IDEWorkbenchMessages.Internal_error; 62 63 68 public IDEExceptionHandler(IWorkbenchConfigurer configurer) { 69 super(); 70 workbenchConfigurer = configurer; 71 } 72 73 78 public void handleException(Throwable t) { 79 try { 80 exceptionCount++; 81 if (exceptionCount > 1) { 82 if (closing) { 83 return; 84 } 85 Shell parent = defaultParent; 86 if (dialog != null && dialog.getShell() != null 87 && !dialog.getShell().isDisposed()) { 88 parent = dialog.getShell(); 89 } 90 MessageBox box = new MessageBox(parent, SWT.ICON_ERROR 91 | SWT.YES | SWT.NO | SWT.SYSTEM_MODAL); 92 box.setText(MSG_FATAL_ERROR_RecursiveTitle); 93 box.setMessage(MessageFormat.format(MSG_FATAL_ERROR, 94 new Object [] { MSG_FATAL_ERROR_Recursive })); 95 int result = box.open(); 96 if (result == SWT.YES) { 97 closeWorkbench(); 98 } 99 } else { 100 if (openQuestionDialog(t)) { 101 closeWorkbench(); 102 } 103 } 104 } finally { 105 exceptionCount--; 106 } 107 } 108 109 112 private void closeWorkbench() { 113 if (closing) { 114 return; 115 } 116 117 try { 118 closing = true; 119 if (dialog != null && dialog.getShell() != null 120 && !dialog.getShell().isDisposed()) { 121 dialog.close(); 122 } 123 workbenchConfigurer.emergencyClose(); 124 } catch (RuntimeException re) { 125 System.err 128 .println("Fatal runtime error happened during workbench emergency close."); re.printStackTrace(); 130 throw re; 131 } catch (Error e) { 132 System.err 135 .println("Fatal error happened during workbench emergency close."); e.printStackTrace(); 137 throw e; 138 } 139 } 140 141 145 private boolean openQuestionDialog(Throwable internalError) { 146 try { 147 String msg = null; 148 if (internalError instanceof OutOfMemoryError ) { 149 msg = MSG_OutOfMemoryError; 150 } else if (internalError instanceof StackOverflowError ) { 151 msg = MSG_StackOverflowError; 152 } else if (internalError instanceof VirtualMachineError ) { 153 msg = MSG_VirtualMachineError; 154 } else if (internalError instanceof SWTError) { 155 msg = MSG_SWTError; 156 } else { 157 if (internalError.getMessage() == null) { 158 msg = IDEWorkbenchMessages.InternalErrorNoArg; 159 } else { 160 msg = NLS.bind(IDEWorkbenchMessages.InternalErrorOneArg, internalError.getMessage()); 161 } 162 if (Policy.DEBUG_OPEN_ERROR_DIALOG) { 163 return openQuestion(null, IDEWorkbenchMessages.Internal_error, msg, internalError, 1); 164 } 165 return false; 166 } 167 Throwable detail = internalError; 170 if (!Policy.DEBUG_OPEN_ERROR_DIALOG) { 171 detail = null; 172 } 173 return InternalErrorDialog 174 .openQuestion(null, IDEWorkbenchMessages.Internal_error, 175 MessageFormat.format(MSG_FATAL_ERROR, 176 new Object [] { msg }), detail, 1); 177 } catch (Throwable th) { 178 System.err 181 .println("Error while informing user about event loop exception:"); internalError.printStackTrace(); 183 System.err.println("Dialog open exception:"); th.printStackTrace(); 185 return true; 186 } 187 } 188 189 private boolean openQuestion(Shell parent, String title, String message, 190 Throwable detail, int defaultIndex) { 191 String [] labels; 192 if (detail == null) { 193 labels = new String [] { IDialogConstants.YES_LABEL, 194 IDialogConstants.NO_LABEL }; 195 } else { 196 labels = new String [] { IDialogConstants.YES_LABEL, 197 IDialogConstants.NO_LABEL, 198 IDialogConstants.SHOW_DETAILS_LABEL }; 199 } 200 201 dialog = new InternalErrorDialog(parent, title, null, message, detail, 202 MessageDialog.QUESTION, labels, defaultIndex); 203 204 if (detail != null) { 205 dialog.setDetailButton(2); 206 } 207 boolean result = dialog.open() == Window.OK; 208 dialog = null; 209 return result; 210 } 211 } 212 | Popular Tags |