001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: package de.uka.ilkd.key.gui;
010:
011: import java.awt.Component;
012: import java.awt.Dialog;
013: import java.awt.Dimension;
014: import java.awt.Font;
015: import java.awt.event.ActionEvent;
016: import java.awt.event.ActionListener;
017: import java.awt.event.ItemEvent;
018: import java.awt.event.ItemListener;
019: import java.io.PrintWriter;
020: import java.io.StringWriter;
021:
022: import javax.swing.*;
023: import javax.swing.border.TitledBorder;
024: import javax.swing.event.ListSelectionEvent;
025: import javax.swing.event.ListSelectionListener;
026:
027: import de.uka.ilkd.key.parser.Location;
028: import de.uka.ilkd.key.parser.ParserException;
029: import de.uka.ilkd.key.proof.SVInstantiationExceptionWithPosition;
030: import de.uka.ilkd.key.util.ExtList;
031:
032: public class ExceptionDialog extends JDialog {
033:
034: private JPanel buttonPanel;
035: private JScrollPane listScroll, stScroll;
036: private StringWriter sw;
037: private PrintWriter pw;
038: private Object[] exceptionArray;
039: private JTextArea stTextArea;
040: private boolean withList = false;
041:
042: private Location getLocation(Object exc) {
043: Location location = null;
044: if (exc instanceof antlr.RecognitionException) {
045: location = new Location(((antlr.RecognitionException) exc)
046: .getFilename(), ((antlr.RecognitionException) exc)
047: .getLine(), ((antlr.RecognitionException) exc)
048: .getColumn());
049: } else if (exc instanceof ParserException) {
050: location = ((ParserException) exc).getLocation();
051: } else {
052: if (exc instanceof SVInstantiationExceptionWithPosition) {
053: location = new Location(null,
054: ((SVInstantiationExceptionWithPosition) exc)
055: .getRow(),
056: ((SVInstantiationExceptionWithPosition) exc)
057: .getColumn());
058: }
059: }
060: return location;
061: }
062:
063: private JPanel createButtonPanel() {
064: JButton closeButton = new JButton("Close");
065: JCheckBox detailsBox = new JCheckBox("Show Details");
066: detailsBox.setSelected(false);
067: JPanel bPanel = new JPanel();
068: bPanel.add(closeButton);
069: bPanel.add(detailsBox);
070:
071: ActionListener closeListener = new ActionListener() {
072: public void actionPerformed(ActionEvent e) {
073: setVisible(false);
074: dispose();
075: }
076: };
077:
078: ItemListener detailsBoxListener = new ItemListener() {
079: public void itemStateChanged(ItemEvent e) {
080: if (e.getStateChange() == ItemEvent.SELECTED) {
081: if (withList)
082: getContentPane().add(listScroll);
083: getContentPane().add(stScroll);
084: } else {
085: if (withList)
086: getContentPane().remove(listScroll);
087: getContentPane().remove(stScroll);
088: }
089: pack();
090: setLocationRelativeTo(null);
091: getContentPane().repaint();
092: }
093: };
094: closeButton.addActionListener(closeListener);
095: detailsBox.addItemListener(detailsBoxListener);
096: return bPanel;
097: }
098:
099: private JScrollPane createJListScroll(Object[] excArray) {
100: String[] excMessages = new String[excArray.length];
101: for (int i = 0; i < excArray.length; i++) {
102: if (!(getLocation(exceptionArray[i]) == null))
103: excMessages[i] = (i + 1) + ".) Location: "
104: + getLocation(excArray[i]) + "\n"
105: + (((Throwable) excArray[i]).getMessage());
106: else
107: excMessages[i] = (i + 1) + ".) "
108: + (((Throwable) excArray[i]).getMessage());
109: }
110: final JList list = new JList(excMessages);
111: list.setCellRenderer(new TextAreaRenderer());
112: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
113: list.setSelectedIndex(0);
114: JScrollPane elistScroll = new JScrollPane(
115: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
116: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
117: elistScroll.getViewport().setView(list);
118: elistScroll.setBorder(new TitledBorder("Exceptions/Errors"));
119: Dimension paneDim = new Dimension(500, 100);
120: elistScroll.setPreferredSize(paneDim);
121: ListSelectionListener listListener = new ListSelectionListener() {
122: public void valueChanged(ListSelectionEvent e) {
123: sw = new StringWriter();
124: pw = new PrintWriter(sw);
125: ((Throwable) exceptionArray[list.getSelectedIndex()])
126: .printStackTrace(pw);
127: stTextArea.setText("("
128: + exceptionArray[list.getSelectedIndex()]
129: .getClass() + ") \n" + sw.toString());
130: }
131: };
132: list.addListSelectionListener(listListener);
133: return elistScroll;
134:
135: }
136:
137: private JScrollPane createTextAreaScroll(Object[] excArray) {
138: stTextArea = new JTextArea();
139: stTextArea.setEditable(false);
140: Dimension textPaneDim = new Dimension(500, 300);
141: JScrollPane Scroll = new JScrollPane(stTextArea);
142: Scroll.setBorder(new TitledBorder("Stack Trace"));
143: Scroll.setPreferredSize(textPaneDim);
144: sw = new StringWriter();
145: pw = new PrintWriter(sw);
146: ((Throwable) excArray[0]).printStackTrace(pw);
147: stTextArea.setText("(" + excArray[0].getClass() + ") \n"
148: + sw.toString());
149: return Scroll;
150: }
151:
152: private JScrollPane createExcTextAreaScroll(Object[] excArray) {
153: JTextArea exTextArea = new JTextArea();
154: exTextArea.setEditable(false);
155: Dimension textPaneDim = new Dimension(500, 100);
156: exTextArea.setColumns(120);
157: exTextArea.setLineWrap(true);
158: exTextArea.setWrapStyleWord(true);
159: JScrollPane Scroll = new JScrollPane(exTextArea);
160: Scroll.setBorder(new TitledBorder("" + excArray[0].getClass()));
161: Scroll.setPreferredSize(textPaneDim);
162: exTextArea.setText(((Throwable) excArray[0]).getMessage());
163: return Scroll;
164: }
165:
166: private JPanel createLocationPanel(Object[] excArray) {
167: Location loc = getLocation(excArray[0]);
168: JPanel lPanel = new JPanel();
169: JTextField fTextField, lTextField, cTextField;
170: fTextField = new JTextField();
171: lTextField = new JTextField();
172: cTextField = new JTextField();
173: fTextField.setEditable(false);
174: lTextField.setEditable(false);
175: cTextField.setEditable(false);
176: if (!(loc == null)) {
177: if (!((loc.getFilename() == null) || (loc.getFilename() == ""))) {
178: fTextField.setText("File: " + loc.getFilename());
179: lPanel.add(fTextField);
180: }
181: if (excArray[0] instanceof SVInstantiationExceptionWithPosition)
182: lTextField.setText("Row: " + +loc.getLine());
183: else
184: lTextField.setText("Line: " + loc.getLine());
185: lPanel.add(lTextField);
186: cTextField.setText("Column: " + loc.getColumn());
187: lPanel.add(cTextField);
188: }
189: return lPanel;
190: }
191:
192: public ExceptionDialog(Dialog parent, ExtList excList) {
193: super (parent, "Parser Messages", true);
194: init(excList);
195: }
196:
197: public ExceptionDialog(JFrame parent, ExtList excList) {
198: super (parent, "Parser Messages", true);
199: init(excList);
200: }
201:
202: private void init(ExtList excList) {
203: buttonPanel = createButtonPanel();
204: if (excList.size() != 0) {
205: if (excList.size() > 1)
206: withList = true;
207: exceptionArray = excList.toArray();
208: getContentPane().setLayout(
209: new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
210: stScroll = createTextAreaScroll(exceptionArray);
211: listScroll = createJListScroll(exceptionArray);
212: getContentPane().add(
213: createExcTextAreaScroll(exceptionArray));
214: getContentPane().add(createLocationPanel(exceptionArray));
215: getContentPane().add(buttonPanel);
216: setDefaultCloseOperation(DISPOSE_ON_CLOSE);
217: pack();
218: setLocationRelativeTo(null);
219: setVisible(true);
220: } else {
221: dispose();
222: }
223: }
224:
225: class TextAreaRenderer extends JTextArea implements
226: ListCellRenderer {
227: public TextAreaRenderer() {
228: setLineWrap(true);
229: setWrapStyleWord(true);
230: setRows(3);
231: }
232:
233: public Component getListCellRendererComponent(JList list,
234: Object value, int index, boolean isSelected,
235: boolean cellHasFocus) {
236: if (index == 0)
237: setFont(getFont().deriveFont(Font.BOLD, 12));
238: else
239: setFont(getFont().deriveFont(Font.PLAIN, 12));
240: setText(value.toString());
241: setBackground(isSelected ? list.getSelectionBackground()
242: : null);
243: setForeground(isSelected ? list.getSelectionForeground()
244: : null);
245: return new JScrollPane(this);
246: }
247: }
248:
249: }
|