Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2dba34a82fef5076debcb0fbf0eecd02fd0d07bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*******************************************************************************
 * Copyright (c) 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 *  $RCSfile: ExceptionHandler.java,v $
 *  $Revision: 1.2 $  $Date: 2005/12/14 19:06:13 $ 
 */
package org.eclipse.jem.internal.beaninfo.ui;

import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.jem.internal.ui.core.JEMUIPlugin;
 
/**
 * 
 * 
 * @since 1.2.0
 */
public class ExceptionHandler {

	/**
	 * 
	 * @param e
	 * @param shell
	 * @param title
	 * @param message
	 * 
	 * @since 1.2.0
	 */
	public static void handle(InvocationTargetException e, Shell shell, String title, String message) {
		Throwable target= e.getTargetException();
		if (target instanceof CoreException) {
			handle((CoreException)target, shell, title, message);
		} else {
			JEMUIPlugin.getPlugin().getLogger().log(e);
			if (e.getMessage() != null && e.getMessage().length() > 0) {
				displayMessageDialog(e, e.getMessage(), shell, title, message);
			} else {
				displayMessageDialog(e, target.getMessage(), shell, title, message);
			}
		}
	}
	
	public static void handle(CoreException e, Shell shell, String title, String message) {
		JEMUIPlugin.getPlugin().getLogger().log(e);
		IStatus status= e.getStatus();
		if (status != null) {
			ErrorDialog.openError(shell, title, message, status);
		} else {
			displayMessageDialog(e, e.getMessage(), shell, title, message);
		}
	}
	
	private static void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) {
		StringWriter msg= new StringWriter();
		if (message != null) {
			msg.write(message);
			msg.write("\n\n"); //$NON-NLS-1$
		}
		if (exceptionMessage == null || exceptionMessage.length() == 0)
			msg.write(BeanInfoUIMessages.ExceptionHandler_errordialog_msg_seelog); 
		else
			msg.write(exceptionMessage);
		MessageDialog.openError(shell, title, msg.toString());			
	}	

}

Back to the top