📄 errordialog.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * web: http://yale.cs.uni-dortmund.de/ * * 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 (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, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package edu.udo.cs.yale.gui;import edu.udo.cs.yale.operator.UserError;import edu.udo.cs.yale.tools.Tools;import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;/** Error message dialog. * * @version $Id: ErrorDialog.java,v 2.3 2003/06/17 19:45:34 fischer Exp $ */public class ErrorDialog extends JDialog { JButton editButton = new JButton("Edit"); private class FormattedStackTraceElement { private StackTraceElement ste; private FormattedStackTraceElement(StackTraceElement ste) { this.ste = ste; } public String toString() { return " "+ste; } } private class StackTraceList extends JList { public StackTraceList(Throwable t) { super(new DefaultListModel()); setFont(getFont().deriveFont(Font.PLAIN)); setSelectionMode(ListSelectionModel.SINGLE_SELECTION); appendAllStackTraces(t); addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (getSelectedIndex() >= 0) { if (!(getSelectedValue() instanceof FormattedStackTraceElement)) { editButton.setEnabled(false); } else { editButton.setEnabled(true); } } else { editButton.setEnabled(true); } } }); } private DefaultListModel model() { return (DefaultListModel)getModel(); } private void appendAllStackTraces(Throwable throwable) { while (throwable != null) { appendStackTrace(throwable); throwable = throwable.getCause(); if (throwable != null) { model().addElement(""); model().addElement("Cause"); } } } private void appendStackTrace(Throwable throwable) { model().addElement("Exception: "+ throwable.getClass().getName()); model().addElement("Message: "+throwable.getMessage()); model().addElement("Stack trace:\n"); for (int i = 0; i < throwable.getStackTrace().length; i++) { model().addElement(new FormattedStackTraceElement(throwable.getStackTrace()[i])); } } private void edit() { try { Object o = getSelectedValue(); if (o == null) return; if (!(o instanceof FormattedStackTraceElement)) return; StackTraceElement ste = ((FormattedStackTraceElement)o).ste; java.io.File file = Tools.findSourceFile(ste); if (file != null) Tools.launchFileEditor(file); } catch (Throwable t) { SwingTools.showSimpleErrorMessage("Cannot launch editor.", t); } } } private ErrorDialog(String title, String mainText, final Throwable error, boolean bugReport) { super(YaleGUI.getMainFrame(), title); final JPanel box = new JPanel(); final GridBagLayout gbl = new GridBagLayout(); box.setLayout(gbl); final GridBagConstraints c = new GridBagConstraints(); c.fill = c.BOTH; c.gridwidth = c.REMAINDER; c.weightx = 1.0; c.weighty = 0.0; c.insets = new Insets(5,11,5,11); // message JLabel text = new JLabel(mainText); text.setFont(text.getFont().deriveFont(java.awt.Font.PLAIN)); text.setPreferredSize(new Dimension(YaleGUI.getMainFrame().getWidth()*2/3, YaleGUI.getMainFrame().getHeight()/4)); gbl.setConstraints(text, c); box.add(text); // details final Box detailBox = new Box(BoxLayout.Y_AXIS); final StackTraceList stl = new StackTraceList(error); JScrollPane detailPane = new JScrollPane(stl); detailPane.setPreferredSize(new Dimension(YaleGUI.getMainFrame().getWidth()*2/3, YaleGUI.getMainFrame().getHeight()/4)); detailBox.add(detailPane); editButton.setEnabled(false); editButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { stl.edit(); } }); JPanel detailButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); detailButtonPanel.add(editButton); detailBox.add(detailButtonPanel); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); final JButton detailButton = new JButton() { private boolean more = true; // anonymous class constructor { setText("Details >>"); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (more) { c.weighty = 1.0; gbl.setConstraints(detailBox, c); box.add(detailBox, 1); setText("<< Hide"); } else { box.remove(detailBox); setText("Details >>"); } pack(); more = !more; } }); } }; buttonPanel.add(detailButton); if (bugReport) { JButton report = new JButton("Send bug report..."); report.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new BugAssistant(error).show(); } }); buttonPanel.add(report); } JButton close = new JButton("Close"); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); buttonPanel.add(close); c.weighty = 0.0; gbl.setConstraints(buttonPanel, c); box.add(buttonPanel); getContentPane().add(box); pack(); } public static ErrorDialog create(String message, Throwable t) { if (t instanceof UserError) { UserError ue = (UserError)t; return new ErrorDialog("Error "+ue.getCode()+": "+ue.getErrorName(), "<html>Error in: <b>"+ue.getOperator()+"</b><br>"+ ue.getMessage()+"<hr>"+ ue.getDetails()+"</html>", t, false); } else { return new ErrorDialog("Exception occured", "<html><b>"+Tools.classNameWOPackage(t.getClass())+"</b><br>"+ message + "<br>"+ "Message: "+t.getMessage()+"</html>", t, true); } } private static void appendAllStackTraces(JTextArea text, Throwable throwable) { while (throwable != null) { appendStackTrace(text, throwable); throwable = throwable.getCause(); if (throwable != null) text.append("\nCause:\n"); } } private static void appendStackTrace(JTextArea text, Throwable throwable) { text.append("Exception:\t"+ throwable.getClass().getName()+"\n"); text.append("Message:\t"+throwable.getMessage()+"\n"); text.append("Stack trace:\n"); for (int i = 0; i < throwable.getStackTrace().length; i++) { text.append(" "+throwable.getStackTrace()[i]+"\n"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -