All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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 entry : new TreeMap(props).entrySet()) {
            sw.append(entry.getKey()+" = "+escapeToString(entry.getValue())+"\n");
        }
        sw.append("\n\n");
    }
    
    static void appendDirectoriesInfo(StringWriter sw) {
        sw.append("Directories info:\n");
        sw.append("-----------------\n\n");
        
        File dataDirectory = BootRuntime.getInstance().getDataDirectory();
        sw.append("data directory - absolute path = "+dataDirectory.getAbsolutePath()+"\n");
        sw.append("data directory - exists = "+dataDirectory.exists()+"\n");
        sw.append("data directory - canRead = "+dataDirectory.canRead()+"\n");
        sw.append("data directory - canWrite = "+dataDirectory.canWrite()+"\n");
        sw.append("data directory - free space = "+dataDirectory.getFreeSpace()+"\n");
        
        File installDirectory = BootRuntime.getInstance().getInstallDirectory();
        sw.append("install directory - absolute path = "+installDirectory.getAbsolutePath()+"\n");
        sw.append("install directory - exists = "+installDirectory.exists()+"\n");
        sw.append("install directory - canRead = "+installDirectory.canRead()+"\n");
        sw.append("install directory - canWrite = "+installDirectory.canWrite()+"\n");
        sw.append("install directory - canExecute = "+installDirectory.canExecute()+"\n");
        sw.append("install directory - free space = "+installDirectory.getFreeSpace()+"\n");

        sw.append("\n\n");
    }
    
    static void appendScreenInfo(StringWriter sw) {
        sw.append("Screen info:\n");
        sw.append("------------\n\n");
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        sw.append("screen size - width = "+screenSize.width+"\n");
        sw.append("screen size - height = "+screenSize.height+"\n");
                
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        for (int i=0;i entry : new TreeMap(props).entrySet()) {
            sw.append(entry.getKey()+" = "+escapeToString(entry.getValue())+"\n");
        }
        sw.append('\n');
        sw.append("default locale = "+Locale.getDefault()+"\n");
        sw.append("\n\n");
    }
    
    private static String escapeToString(Object value) {
        if (value == null) {
            return null;
        }
        return value.toString().replace("\n", "\\n").replace("\r", "\\r");
    }

    /**
     * Fill in stacktrace
     * 
     * @param sw
     * @param throwable
     */
    static void appendStacktrace(StringWriter sw, Throwable throwable) {
        if (throwable != null) {
            sw.append("Exception:\n");
            sw.append("----------\n\n");
            throwable.printStackTrace(new PrintWriter(sw));
            sw.append("\n\n");
        }
    }

    private static void appendCode( int iCode, StringBuffer buffer ){
        if ( iCode != 0 && buffer!=null) {
            buffer.append("[");
            buffer.append( iCode );
            buffer.append("]: ");
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy