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

at.spardat.xma.page.NotificationBox Maven / Gradle / Ivy

/*******************************************************************************
 * 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
 *******************************************************************************/

// @(#) $Id: NotificationBox.java 5839 2010-05-28 14:15:55Z gub $
package at.spardat.xma.page;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import at.spardat.enterprise.exc.*;
import at.spardat.xma.page.messagebox.AbstractMessagebox;
import at.spardat.xma.page.messagebox.DetailedMessageBox;
import at.spardat.xma.page.messagebox.LocalizedMessageBox;
import at.spardat.xma.security.XMAContext;
import at.spardat.xma.session.XMASessionClient;

/**
 * Utility methods to display INotification- or Throwable-objects in a modal
 * message box.
 *
 * @author YSD, 23.05.2003 20:19:30
 */
public class NotificationBox {

    /**
     * Shows an INotification or a java.lang.Throwable in a modal message box.
     *
     * @param o      the object to show, either a INotification or a Throwable.
     * @param parent the parent shell. This parameter should not be null as this might
     *                not be supported in future versions of SWT.
     * @param ctx XMAContext to provide environmental information
     * @param session the client session or null if not known.
     * @return A reaction constant of the class INotification, this is one of the constants
     *          starting with praefix R_ and indicates the pressed push button on the
     *          message dialogue.
     */
    public static int show (Object o, Shell parent, XMAContext ctx, XMASessionClient session) {
        return show(o,parent,ctx,session,null);
    }

    /**
     * Shows an INotification or a java.lang.Throwable in a modal message box.
     *
     * @param o      the object to show, either a INotification or a Throwable.
     * @param parent the parent shell. This parameter should not be null as this might
     *                not be supported in future versions of SWT.
     * @param ctx XMAContext to provide environmental information
     * @param session the client session or null if not known.
     * @param page the page where the exception occurred or null if not known.
     * @return A reaction constant of the class INotification, this is one of the constants
     *          starting with praefix R_ and indicates the pressed push button on the
     *          message dialogue.
     * @since 2.1.2
     */
    public static int show (Object o, Shell parent, XMAContext ctx, XMASessionClient session, PageClient page) {

        if (!(o instanceof INotification || o instanceof Throwable)) throw new IllegalArgumentException();
        if (o instanceof NotificationList) return 0; // todo implement display of NotificationList

        /**
         * compute icon and push buttons
         */
        int swtStyle = SWT.OK;
        if (o instanceof INotification) {
            INotification noti = (INotification)o;
            swtStyle = reaction2swtStyles (noti.getReaction());
            switch (noti.getType()) {
                case INotification.T_ERROR:       swtStyle |= SWT.ICON_ERROR; break;
                case INotification.T_INFORMATION: swtStyle |= SWT.ICON_INFORMATION; break;
                case INotification.T_QUESTION:    swtStyle |= SWT.ICON_QUESTION; break;
                case INotification.T_WARNING:     swtStyle |= SWT.ICON_WARNING;
            }
        } else {
            swtStyle |= SWT.ICON_ERROR;
        }

        /**
         * compute title and message text
         */
        StringBuffer        text = new StringBuffer();
        String              title = "";
        boolean showExceptionDetail = "true".equalsIgnoreCase(session.getRuntimeProperty("showExceptionDetail"));
        if (o instanceof INotification) {
            INotification   noti = (INotification)o;
            title = noti.getShortMessage();
            String          message = noti.getMessage();
            if (noti instanceof SysException) {
                /**
                 * if the notification is a SysException, due to nesting of exceptions, the top level
                 * exceptions might have a message that is empty. In this case, we search for a
                 * non-empty message.
                 */
                boolean showExcClassName = "true".equals (session.getRuntimeProperty("showExceptionClassName", "true"));
                message = ((SysException)noti).getFirstNonEmptyMessage(showExcClassName);
            }
            text.append (message);

            if (o instanceof BaseException) {
                /**
                 * An application exception has more; a code and a stacktrace
                 */
                BaseException appEx = (BaseException)o;
                if (appEx.getCode() != 0) {
                    text.append(" [");
                    text.append(appEx.getCode());
                    text.append("]");
                }
                /**
                 * decide if the stacktrace of the BaseException should be shown
                 */
                boolean verboseOutput = ctx.getEnvironment().equals (XMAContext.local) || ctx.getEnvironment().equals(XMAContext.devel);
                /**
                 * verboseOutput may be suppressed by a session property
                 */
                if (session != null && "false".equals (session.getRuntimeProperty("verboseNotificationBox", "true"))) verboseOutput = false;
                if (verboseOutput&&!showExceptionDetail) {
                    text.append("\n\nException Stacktrace (not displayed in non-development environments):\n");
                    appendStacktraceOf(appEx, text);
                }
            }
        } else if (o instanceof Throwable) {
            if(showExceptionDetail) {
                text.append(o.toString());
            } else {
                /**
                 * the object to be displayed is not an INotification, but a Throwable.
                 * all we can do here is to output the stacktrace
                 */
                appendStacktraceOf((Throwable)o, text);
            }
        }

        /**
         * construct MessageBox and show
         */
        int modal = SWT.PRIMARY_MODAL;
        if (session!=null && "true".equals(session.getRuntimeProperty("messagesApplicationModal", "false"))) {
            modal = SWT.APPLICATION_MODAL;
        }
        int ret;
        if(showExceptionDetail) {
            if(title==null||"".equals(title)) {
                title=parent.getText();
            }
            AbstractMessagebox mb;
            if(o instanceof Exception && !(o instanceof AppException)) {
                mb = new DetailedMessageBox(parent,ctx.getLocale(),(Exception)o,swtStyle|modal|SWT.RESIZE);
                if(page!=null) page.addContextInformation((DetailedMessageBox)mb);
            } else {
                mb = new LocalizedMessageBox(parent, ctx.getLocale(), swtStyle | modal | SWT.RESIZE);
            }
            mb.setText (title);
            mb.setMessage(text.toString());
            ret = mb.open();
        } else {
            MessageBox mb = new MessageBox(parent, swtStyle | modal);
            mb.setText (title);
            mb.setMessage(text.toString());
            ret = mb.open();
        }
        return swtStyle2reaction(ret);
    }


    /**
     * maps INotification reaction constants to SWT constants
     */
    private static final int [][]   reactionSwtMap = {
        { INotification.R_OK, SWT.OK },
        { INotification.R_CANCEL , SWT.CANCEL },
        { INotification.R_YES , SWT.YES },
        { INotification.R_NO , SWT.NO },
        { INotification.R_RETRY , SWT.RETRY },
        { INotification.R_ABORT , SWT.ABORT },
        { INotification.R_IGNORE , SWT.IGNORE },
    };

    /**
     * Maps a bit-or combination of reaction styles of the INotification class
     * to a bit-or combination of SWT-styles.
     */
    private static int reaction2swtStyles (int reaction) {
        int             result = 0;
        for (int i=0; iMessageBox.open to the corresponding
     * reaction constant of INotification.
     */
    private static int swtStyle2reaction (int swtStyle) {
        for (int i=0; ithrowable.printStackTrace to the
     * provided StringBuffer. Lines are separated with '\n'.
     *
     * @param throwable the Throwable whose stack trace is to be appended.
     * @param b the StringBuffer where to append to.
     */
    private static void appendStacktraceOf (Throwable throwable, StringBuffer b) {
        String [] lines = stackTraceLinesOf (throwable);
        for (int i=0; iprintStackTrace on the provided Throwable,
     * extracts the lines and returns an String array of lines.
     */
    private static String[] stackTraceLinesOf (Throwable th) {
        ArrayList           lines = new ArrayList();
        StringWriter        sw = new StringWriter();
        PrintWriter         pw = new PrintWriter(sw);
        th.printStackTrace(pw);
        pw.close();
        String stackTrace = sw.toString();
        StringTokenizer     tokizer = new StringTokenizer (stackTrace, System.getProperty("line.separator"));
        while (tokizer.hasMoreTokens()) {
            lines.add(tokizer.nextToken());
        }
        String [] toReturn = new String[lines.size()];
        lines.toArray(toReturn);
        return toReturn;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy