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

📄 exceptiondialog.java~1~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~1~
字号:
/*
 * 12/28/2004
 *
 * ExceptionDialog.java - A dialog for displaying program Exceptions to the
 *                        user.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * 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 2
 * of the License, or 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.ui.app;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;

import org.fife.ui.RButton;
import org.fife.ui.RScrollPane;
import org.fife.ui.UIUtilities;


/**
 * The dialog displayed to the user when an <code>Exception</code> is
 * caught.
 *
 * @author Robert Futrell
 * @version 0.1
 */
public class ExceptionDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2235383842738423727L;

	// Don't use a tab as tab==8 space by default => too much space!
	private static final String TRACE_STEP_BEGINNING	= "     at ";


/*****************************************************************************/


	/**
	 * Constructor.
	 *
	 * @param owner The parent window.
	 * @param t The exception that was thrown.
	 */
	public ExceptionDialog(Frame owner, Throwable t) {
		super(owner);
		init(t);
	}


/*****************************************************************************/


	/**
	 * Constructor.
	 *
	 * @param owner The parent dialog.
	 * @param t The exception that was thrown.
	 */
	public ExceptionDialog(Dialog owner, Throwable t) {
		super(owner);
		init(t);
	}


/*****************************************************************************/


	/**
	 * Returns the text component to use to display the exception.  By
	 * default, this method returns an instance of <code>JTextArea</code>.
	 * You can override this method to return a different type of text
	 * component (such as an instance of
	 * <code>org.fife.ui.rsyntaxtextarea.RSyntaxTextArea</code> set to syntax
	 * highlight Java exceptions).
	 *
	 * @return The text component.
	 */
	protected JTextComponent createTextComponent() {
		return new JTextArea(12, 25);
	}


/*****************************************************************************/


	/**
	 * Returns a string representing the stack trace for this throwable.
	 *
	 * @param t The throwable.
	 * @return The stack trace.
	 */
	private static final String getStackTraceText(Throwable t) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(t.toString()).append("\n");
		StackTraceElement[] ste = t.getStackTrace();
		int count = ste.length;
		for (int i=0; i<count; i++) {
			buffer.append(TRACE_STEP_BEGINNING).append(ste[i].toString()).
													append("\n");
		}
		return buffer.toString();
	}


/*****************************************************************************/


	/**
	 * Initializes the dialog.
	 *
	 * @param t The <code>Throwable</code> to display.
	 */
	private void init(Throwable t) {
		setTitle("Exception Caught");
		JPanel contentPane = new JPanel(new BorderLayout());
		JTextComponent textComponent = createTextComponent();
		String stackTraceText = getStackTraceText(t);
		try {
			textComponent.setText(stackTraceText);
		} catch (Throwable t2) {
			// I'm paranoid about custom text areas (such as RSyntaxTextArea)
			// trying to do stuff like syntax highlight the stack trace and
			// failing during the attempt...
			// Can't call createTextComponent as it was likely overridden.
			textComponent = new JTextArea(15, 50);
			textComponent.setText(stackTraceText);
		}
		textComponent.setCaretPosition(0);
		textComponent.setEditable(false);
		JPanel temp = new JPanel(new GridLayout(1,1));
		temp.setBorder(UIUtilities.getEmpty5Border());
		temp.add(new RScrollPane(textComponent));
		contentPane.add(temp);
		JPanel buttonPanel = new JPanel();
		RButton okButton = new RButton("Close");
		okButton.setMnemonic('c');
		okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
		});
		buttonPanel.add(okButton);
		contentPane.add(buttonPanel, BorderLayout.SOUTH);
		setContentPane(contentPane);
		getRootPane().setDefaultButton(okButton);
		pack();
		setModal(true);
	}


/*****************************************************************************/

}

⌨️ 快捷键说明

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