Java JOptionPane Error showError(Exception e)

Here you can find the source of showError(Exception e)

Description

show Error

License

Open Source License

Declaration

public static void showError(Exception e) 

Method Source Code

//package com.java2s;
/*/* www  . j  a  v  a  2s .  c  om*/
 * This file is part of Giswater
 * Copyright (C) 2013 Tecnics Associats
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author:
 *   David Erill <derill@giswater.org>
 */

import java.awt.Component;

import java.io.PrintWriter;

import java.io.StringWriter;

import java.util.ResourceBundle;

import java.util.logging.Logger;

import javax.swing.JOptionPane;

public class Main {
    private static final int NUM_LEVELS = 10;
    private static Logger logger;
    private static ResourceBundle bundleText;

    public static void showError(String msg) {
        showError(msg, "");
    }

    public static void showError(Exception e) {
        showError(e, "");
    }

    public static void showError(String msg, String param) {
        showError(null, msg, param);
    }

    public static void showError(Component comp, String msg) {
        showError(null, msg, "");
    }

    public static void showError(Component comp, String msg, String param) {

        String userMsg = getBundleString(msg);
        if (!param.equals("")) {
            userMsg += "\n" + param;
        }
        logError(msg, param);
        JOptionPane.showMessageDialog(comp, userMsg, getBundleString("inp_descr"), JOptionPane.WARNING_MESSAGE);

    }

    public static void showError(Exception e, String param) {
        logError(e, param);
        JOptionPane.showMessageDialog(null, e.getMessage(), getBundleString("inp_descr"),
                JOptionPane.WARNING_MESSAGE);
    }

    public static String getBundleString(String key) {
        return getBundleString(bundleText, key);
    }

    public static String getBundleString(ResourceBundle bundle, String key) {
        try {
            return bundle.getString(key);
        } catch (Exception e) {
            return key;
        }
    }

    public static void logError(String msg) {
        logError(msg, "");
    }

    public static void logError(Exception e) {
        logError(e, "");
    }

    public static void logError(String msg, String param) {
        if (logger != null) {
            String errorMsg = getBundleString(msg);
            if (!param.equals("")) {
                errorMsg += "\nParameter: " + param;
            }
            logger.warning(errorMsg);
        }
    }

    public static void logError(Exception e, String param) {

        if (logger != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw, true);
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
            String fullStack = sw.toString();
            String[] lines = fullStack.split(System.getProperty("line.separator"));
            String shortStack = "";
            for (int i = 0; i < NUM_LEVELS; i++) {
                shortStack += lines[i] + "\n";
            }
            shortStack += "Parameter: " + param;
            logger.warning(shortStack);
        }

    }
}

Related

  1. showError(Component parent, String message)
  2. showError(Component parentComponent, Exception e)
  3. showError(Component where, String msg)
  4. showError(Exception e)
  5. showError(Exception e)
  6. showError(final Component component, final String title, final String message)
  7. showError(final Component rootComponent, final String message, final String title)
  8. showError(JComponent parent, Throwable e)
  9. showError(JFrame owner, ImageIcon image, String title, String message, String text)