1 11 package org.eclipse.compare.internal; 12 13 import java.io.StringWriter ; 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.swt.widgets.Shell; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 22 import org.eclipse.jface.dialogs.ErrorDialog; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 25 32 public class ExceptionHandler { 33 34 private static ExceptionHandler fgInstance= new ExceptionHandler(); 35 36 40 public static void log(Throwable t, String message) { 41 CompareUIPlugin.log(new Status(IStatus.ERROR, CompareUIPlugin.getPluginId(), 42 CompareUIPlugin.INTERNAL_ERROR, message, t)); 43 } 44 45 53 public static void handle(CoreException e, String title, String message) { 54 handle(e, CompareUIPlugin.getShell(), title, message); 55 } 56 57 65 public static void handle(CoreException e, Shell parent, String title, String message) { 66 fgInstance.perform(e, parent, title, message); 67 } 68 69 77 public static void handle(InvocationTargetException e, String title, String message) { 78 handle(e, CompareUIPlugin.getShell(), title, message); 79 } 80 81 89 public static void handle(InvocationTargetException e, Shell parent, String title, String message) { 90 fgInstance.perform(e, parent, title, message); 91 } 92 93 95 protected void perform(CoreException e, Shell shell, String title, String message) { 96 CompareUIPlugin.log(e); 97 IStatus status= e.getStatus(); 98 if (status != null) { 99 ErrorDialog.openError(shell, title, message, status); 100 } else { 101 displayMessageDialog(e, e.getMessage(), shell, title, message); 102 } 103 } 104 105 protected void perform(InvocationTargetException e, Shell shell, String title, String message) { 106 Throwable target= e.getTargetException(); 107 if (target instanceof CoreException) { 108 perform((CoreException)target, shell, title, message); 109 } else { 110 CompareUIPlugin.log(e); 111 if (e.getMessage() != null && e.getMessage().length() > 0) { 112 displayMessageDialog(e, e.getMessage(), shell, title, message); 113 } else { 114 displayMessageDialog(e, target.getMessage(), shell, title, message); 115 } 116 } 117 } 118 119 121 private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) { 122 StringWriter msg= new StringWriter (); 123 if (message != null) { 124 msg.write(message); 125 msg.write("\n\n"); } 127 if (exceptionMessage == null || exceptionMessage.length() == 0) 128 msg.write(CompareMessages.ExceptionDialog_seeErrorLogMessage); 129 else 130 msg.write(exceptionMessage); 131 MessageDialog.openError(shell, title, msg.toString()); 132 } 133 } 134 | Popular Tags |