at.spardat.xma.boot.BRNotificationBox Maven / Gradle / Ivy
// @(#) $Id: BRNotificationBox.java 10021 2012-11-12 11:45:34Z hoenninger $
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* 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:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
package at.spardat.xma.boot;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.TreeMap;
import javax.swing.JOptionPane;
import at.spardat.xma.boot.comp.CCLoader;
import at.spardat.xma.boot.exc.BRTCodes;
import at.spardat.xma.boot.exc.BootRuntimeException;
import at.spardat.xma.boot.logger.LogManager;
/**
* Utility methods to display boot runtime exceptions
*
* @author CGS
*/
public class BRNotificationBox {
/**
* Shows a java.lang.Throwable in a modal message box.
*
* @param throwable a Throwable to show
*/
public static void show (Throwable throwable) {
if (throwable==null) return;
try {
String title = BRTCodes.getText( BRTCodes.APPLICATION_ERROR_TITLE );
StringBuffer text = new StringBuffer();
if (throwable instanceof BootRuntimeException) {
BootRuntimeException brtEx = (BootRuntimeException)throwable;
if( brtEx.getShowToEndUser() == false ) {
return;
}
appendCode( brtEx.getCode(), text );
text.append( brtEx.getLocalizedMessage() );
/**
* decide if the stacktrace of the BaseException should be shown
*/
// boolean verboseOutput;
// if (verboseOutput) {
// text.append("\n\nException Stacktrace (not displayed in non-development environments):\n");
// appendStacktraceOf(appEx, text);
// }
} else if (throwable instanceof Throwable) {
String strGeneralAppError = BRTCodes.getText(BRTCodes.APPLICATION_ERROR);
String strReason = BRTCodes.getText(BRTCodes.ERROR_REASON);
int iCode = BRTCodes.APPLICATION_ERROR;
appendCode(iCode,text);
text.append(strGeneralAppError);
text.append("\n\n" + strReason + ": \"" + throwable.toString() + "\"");
}
// append reference to log file
try {
Properties props = Launcher.getBootRuntimeProperties();
if(props!=null && "true".equalsIgnoreCase(props.getProperty("boot.showloghint"))) {
String logFileName = LogManager.getLogManager().getLogFileName();
if(logFileName!=null&&logFileName.length()>0) {
MessageFormat format = new MessageFormat(BRTCodes.getText(BRTCodes.LOGHINT));
text.append('\n').append(format.format(new Object[]{logFileName}));
}
}
} catch (Exception e) {
e.printStackTrace();
}
/**
* construct MessageBox and show
*/
showMessageDialog(title,text.toString(),true,throwable);
} catch(Exception ex ) {
System.err.println( "exception on show error: " + ex.toString() );
}
}
/**
* Show a message dialog.
*
* @param title
* @param message
* @param showDetailsButton
* @param throwable
* @return
*/
static int showMessageDialog(String title, String message, boolean showDetailsButton, Throwable throwable) {
/* Build the options */
final Object[] options;
if (showDetailsButton) {
options = new String[2];
options[1] = "Details...";
} else {
options = new String[1];
}
options[0] = "OK";
/* Show the message dialog */
int result = JOptionPane.showOptionDialog(null, message,
title, JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
null, options, null);
/* Determine the details and show them in the detail dialog */
if (result == 1) {
showDetailsDialog(title, throwable);
}
return result;
}
public static void showDetailsDialog(String title, Throwable throwable) {
/* Fill in details */
StringWriter sw = new StringWriter();
appendStacktrace(sw,throwable);
appendBootruntimeProperties(sw);
appendDirectoriesInfo(sw);
appendScreenInfo(sw);
appendSWTInfo(sw);
appendSystemProperties(sw);
/* Check the availability of details */
String details = sw.toString();
if (details.length() == 0) {
details = "No details available";
}
/* Show the dialog */
showDetailsDialog(title,details);
}
/**
* Show a details dialog
*
* @param title
* @param details
*/
public static void showDetailsDialog(String title, String details) {
DetailsDialog detailsDialog = new DetailsDialog(title,details);
detailsDialog.setVisible(true);
}
/**
* Fill in bootruntime properties
*
* @param sw
*/
static void appendBootruntimeProperties(StringWriter sw) {
Properties props = BootRuntime.getInstance().getConfigProperties();
sw.append("Bootruntime properties:\n");
sw.append("-----------------------\n\n");
for (Entry