⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 errordialog.java

📁 java插件系统源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
 * Java Plug-in Framework (JPF)
 * Copyright (C) 2004-2006 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/
package org.java.plugin.boot;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

import org.java.plugin.util.ResourceManager;

/**
 * Helper class to display detailed message about application error.
 * 
 * @version $Id: ErrorDialog.java,v 1.4 2006/09/05 17:48:51 ddimon Exp $
 */
public class ErrorDialog extends JDialog {
    private static final long serialVersionUID = 7142861251076530780L;

    /**
     * Displays error dialogue to the user.
     * @param parentComponent parent component, may be <code>null</code>
     * @param title window title
     * @param message error message
     */
    public static void showError(final Component parentComponent,
            final String title, final String message) {
        showError(parentComponent, title, message, null, null);
    }

    /**
     * Displays error dialogue to the user.
     * @param parentComponent parent component, may be <code>null</code>
     * @param title window title
     * @param message error message
     * @param data error data, {@link Collection collections} and arrays are
     *             handled specially, all other objects are shown using
     *             <code>toString()</code> method
     */
    public static void showError(final Component parentComponent,
            final String title, final String message, final Object data) {
        showError(parentComponent, title, message, data, null);
    }

    /**
     * Displays error dialogue to the user.
     * @param parentComponent parent component, may be <code>null</code>
     * @param title window title
     * @param data error data, {@link Collection collections} and arrays are
     *             handled specially, all other objects are shown using
     *             <code>toString()</code> method
     * @param error an error to be shown in details section
     */
    public static void showError(final Component parentComponent,
            final String title, final Object data, final Throwable error) {
        String message = error.getMessage();
        if ((message == null) || (message.trim().length() == 0)) {
            message = error.toString();
        }
        showError(parentComponent, title, message, data, error);
    }

    /**
     * Displays error dialogue to the user.
     * @param parentComponent parent component, may be <code>null</code>
     * @param title window title
     * @param error an error to be shown in details section
     */
    public static void showError(final Component parentComponent,
            final String title, final Throwable error) {
        String message = error.getMessage();
        if ((message == null) || (message.trim().length() == 0)) {
            message = error.toString();
        }
        showError(parentComponent, title, message, error);
    }

    /**
     * Displays error dialogue to the user.
     * @param parentComponent parent component, may be <code>null</code>
     * @param title window title
     * @param message error message
     * @param error an error to be shown in details section
     */
    public static void showError(final Component parentComponent,
            final String title, final String message, final Throwable error) {
        showError(parentComponent, title, message, null, error);
    }

    /**
     * Displays error dialogue to the user.
     * @param parentComponent parent component, may be <code>null</code>
     * @param title window title
     * @param message error message
     * @param data error data, {@link Collection collections} and arrays are
     *             handled specially, all other objects are shown using
     *             <code>toString()</code> method
     * @param error an error to be shown in details section
     */
    public static void showError(final Component parentComponent,
            final String title, final String message, final Object data,
            final Throwable error) {
        Frame frame = (parentComponent != null) ? JOptionPane
                .getFrameForComponent(parentComponent) : JOptionPane
                .getRootFrame();
        new ErrorDialog(frame, title, message, data, error, false).show();
    }

    /**
     * Displays error dialogue to the user and lets him to make a decision with
     * "Yes" and "No" buttons. The question should be in the given message.
     * @param parentComponent parent component, may be <code>null</code>
     * @param title window title
     * @param message error message
     * @return <code>true</code> if user chooses "Yes" answer
     */
    public static boolean showWarning(final Component parentComponent,
            final String title, final String message) {
        return showWarning(parentComponent, title, message, null, null);
    }

    /**
     * Displays error dialogue to the user and lets him to make a decision with
     * "Yes" and "No" buttons. The question should be in the given message.
     * @param parentComponent parent component, may be <code>null</code>
     * @param title window title
     * @param message error message
     * @param data error data, {@link Collection collections} and arrays are
     *             handled specially, all other objects are shown using
     *             <code>toString()</code> method
     * @return <code>true</code> if user chooses "Yes" answer
     */
    public static boolean showWarning(final Component parentComponent,
            final String title, final String message, final Object data) {
        return showWarning(parentComponent, title, message, data, null);
    }

    /**
     * Displays error dialogue to the user and lets him to make a decision with
     * "Yes" and "No" buttons. The question should be in the given message.
     * @param parentComponent parent component, may be <code>null</code>
     * @param title window title
     * @param message error message
     * @param error an error to be shown in details section
     * @return <code>true</code> if user chooses "Yes" answer
     */
    public static boolean showWarning(final Component parentComponent,
            final String title, final String message, final Throwable error) {
        return showWarning(parentComponent, title, message, null, error);
    }

    /**
     * Displays error dialogue to the user and lets him to make a decision with
     * "Yes" and "No" buttons. The question should be in the given message.
     * @param parentComponent parent component, may be <code>null</code>
     * @param title window title
     * @param message error message
     * @param data error data, {@link Collection collections} and arrays are
     *             handled specially, all other objects are shown using
     *             <code>toString()</code> method
     * @param error an error to be shown in details section
     * @return <code>true</code> if user chooses "Yes" answer
     */
    public static boolean showWarning(final Component parentComponent,
            final String title, final String message, final Object data,
            final Throwable error) {
        Frame frame = (parentComponent != null) ? JOptionPane
                .getFrameForComponent(parentComponent) : JOptionPane
                .getRootFrame();
        ErrorDialog dialog = new ErrorDialog(frame, title, message, data, error,
                true);
        dialog.show();
        return dialog.yesBtnPressed;
    }
    
    /**
     * Utility method to get detailed error report.
     * @param t exception instance, may be <code>null</code>
     * @return detailed error message with most important system information
     *         included
     */
    public static String getErrorDetails(final Throwable t) {
        StringBuffer sb = new StringBuffer();
        String nl = System.getProperty("line.separator"); //$NON-NLS-1$
        sb.append(new Date()).append(nl);
        if (t != null) {
            // Print exception details.
            sb.append(nl).append(
                    "-----------------------------------------------") //$NON-NLS-1$
                    .append(nl);
            sb.append("Exception details.").append(nl).append(nl); //$NON-NLS-1$
            sb.append("Class: ").append(t.getClass().getName()).append(nl); //$NON-NLS-1$
            sb.append("Message: ").append(t.getMessage()).append(nl); //$NON-NLS-1$
            printError(t, "Stack trace:", sb); //$NON-NLS-1$
        }
        // Print system properties.
        sb.append(nl).append("-----------------------------------------------") //$NON-NLS-1$
            .append(nl);
        sb.append("System properties:").append(nl).append(nl); //$NON-NLS-1$
        for (Iterator it = new TreeMap(System.getProperties()).entrySet()
                .iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            sb.append(entry.getKey()).append("=") //$NON-NLS-1$
                .append(entry.getValue()).append(nl);
        }
        // Print runtime info.
        sb.append(nl).append("-----------------------------------------------") //$NON-NLS-1$
            .append(nl);
        sb.append("Runtime info:").append(nl).append(nl); //$NON-NLS-1$
        Runtime rt = Runtime.getRuntime();
        sb.append("Memory TOTAL / FREE / MAX: ") //$NON-NLS-1$
            .append(rt.totalMemory()).append(" / ") //$NON-NLS-1$
            .append(rt.freeMemory()).append(" / ") //$NON-NLS-1$
            .append(rt.maxMemory()).append(nl);
        sb.append("Available processors: ") //$NON-NLS-1$
            .append(rt.availableProcessors()).append(nl);
        sb.append("System class loader: ").append("" //$NON-NLS-1$ //$NON-NLS-2$
                + ClassLoader.getSystemClassLoader()).append(nl);
        sb.append("Thread context class loader: ").append("" //$NON-NLS-1$ //$NON-NLS-2$
                + Thread.currentThread().getContextClassLoader()).append(nl);
        sb.append("Security manager: ").append("" //$NON-NLS-1$ //$NON-NLS-2$
                + System.getSecurityManager()).append(nl);
        return sb.toString();
    }

    /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -