1 11 package org.eclipse.search.internal.ui.util; 12 13 import java.io.StringWriter ; 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Status; 19 import org.eclipse.jface.dialogs.ErrorDialog; 20 import org.eclipse.jface.dialogs.MessageDialog; 21 import org.eclipse.search.internal.ui.SearchMessages; 22 import org.eclipse.search.internal.ui.SearchPlugin; 23 import org.eclipse.search.ui.NewSearchUI; 24 import org.eclipse.swt.widgets.Shell; 25 26 33 public class ExceptionHandler { 34 35 private static ExceptionHandler fgInstance= new ExceptionHandler(); 36 37 43 public static void log(Throwable t, String message) { 44 SearchPlugin.log(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, t)); 45 } 46 47 55 public static void handle(CoreException e, String title, String message) { 56 handle(e, SearchPlugin.getActiveWorkbenchShell(), title, message); 57 } 58 59 67 public static void handle(CoreException e, Shell parent, String title, String message) { 68 fgInstance.perform(e, parent, title, message); 69 } 70 71 79 public static void handle(InvocationTargetException e, String title, String message) { 80 handle(e, SearchPlugin.getActiveWorkbenchShell(), title, message); 81 } 82 83 91 public static void handle(InvocationTargetException e, Shell parent, String title, String message) { 92 fgInstance.perform(e, parent, title, message); 93 } 94 95 97 protected void perform(CoreException e, Shell shell, String title, String message) { 98 SearchPlugin.log(e); 99 IStatus status= e.getStatus(); 100 if (status != null) { 101 ErrorDialog.openError(shell, title, message, status); 102 } else { 103 displayMessageDialog(e, e.getMessage(), shell, title, message); 104 } 105 } 106 107 protected void perform(InvocationTargetException e, Shell shell, String title, String message) { 108 Throwable target= e.getTargetException(); 109 if (target instanceof CoreException) { 110 perform((CoreException)target, shell, title, message); 111 } else { 112 SearchPlugin.log(e); 113 if (e.getMessage() != null && e.getMessage().length() > 0) { 114 displayMessageDialog(e, e.getMessage(), shell, title, message); 115 } else { 116 displayMessageDialog(e, target.getMessage(), shell, title, message); 117 } 118 } 119 } 120 121 123 public static void displayMessageDialog(Throwable t, Shell shell, String title, String message) { 124 fgInstance.displayMessageDialog(t, t.getMessage(), shell, title, message); 125 } 126 127 public static void displayMessageDialog(Throwable t, String title, String message) { 128 displayMessageDialog(t, SearchPlugin.getActiveWorkbenchShell(), title, message); 129 } 130 131 private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) { 132 StringWriter msg= new StringWriter (); 133 if (message != null) { 134 msg.write(message); 135 msg.write("\n\n"); } 137 if (exceptionMessage == null || exceptionMessage.length() == 0) 138 msg.write(SearchMessages.ExceptionDialog_seeErrorLogMessage); 139 else 140 msg.write(exceptionMessage); 141 MessageDialog.openError(shell, title, msg.toString()); 142 } 143 } 144 | Popular Tags |