1 11 package org.eclipse.ltk.internal.ui.refactoring; 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 20 import org.eclipse.swt.widgets.Shell; 21 22 import org.eclipse.jface.dialogs.ErrorDialog; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 25 import org.eclipse.ltk.ui.refactoring.IRefactoringUIStatusCodes; 26 27 34 public class ExceptionHandler { 35 36 private static ExceptionHandler fgInstance= new ExceptionHandler(); 37 38 45 public static void log(Throwable t, String message) { 46 RefactoringUIPlugin.log(new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(), 47 IRefactoringUIStatusCodes.INTERNAL_ERROR, message, t)); 48 } 49 50 58 public static void handle(CoreException e, Shell parent, String title, String message) { 59 fgInstance.perform(e, parent, title, message); 60 } 61 62 70 public static void handle(InvocationTargetException e, Shell parent, String title, String message) { 71 fgInstance.perform(e, parent, title, message); 72 } 73 74 76 protected void perform(CoreException e, Shell shell, String title, String message) { 77 RefactoringUIPlugin.log(e); 78 IStatus status= e.getStatus(); 79 if (status != null) { 80 ErrorDialog.openError(shell, title, message, status); 81 } else { 82 displayMessageDialog(e, e.getMessage(), shell, title, message); 83 } 84 } 85 86 protected void perform(InvocationTargetException e, Shell shell, String title, String message) { 87 Throwable target= e.getTargetException(); 88 if (target instanceof CoreException) { 89 perform((CoreException)target, shell, title, message); 90 } else { 91 RefactoringUIPlugin.log(e); 92 if (e.getMessage() != null && e.getMessage().length() > 0) { 93 displayMessageDialog(e, e.getMessage(), shell, title, message); 94 } else { 95 displayMessageDialog(e, target.getMessage(), shell, title, message); 96 } 97 } 98 } 99 100 102 private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) { 103 StringWriter msg= new StringWriter (); 104 if (message != null) { 105 msg.write(message); 106 msg.write("\n\n"); } 108 if (exceptionMessage == null || exceptionMessage.length() == 0) 109 msg.write(RefactoringUIMessages.ExceptionHandler_seeErrorLogMessage); 110 else 111 msg.write(exceptionMessage); 112 MessageDialog.openError(shell, title, msg.toString()); 113 } 114 } 115 | Popular Tags |