1 /* Copyright (C) 2005-2011 Fabio Riccardi */
2 
3 package com.lightcrafts.ui;
4 
5 import javax.swing.*;
6 import java.awt.*;
7 import java.io.PrintWriter;
8 import java.io.StringWriter;
9 
10 /** Display a Throwable as a text area.
11   */
12 
13 public class ExceptionDisplay extends Box implements Scrollable {
14 
15     private JTextArea trace;
16 
ExceptionDisplay(Throwable t)17     public ExceptionDisplay(Throwable t) {
18         super(BoxLayout.Y_AXIS);
19 
20         trace = new JTextArea();
21         trace.setRows(20);
22         trace.setColumns(100);
23         trace.setEditable(true);
24         trace.setLineWrap(false);
25         trace.setEditable(false);
26         Font font = new Font("Monospaced", Font.PLAIN, 12);
27         trace.setFont(font);
28 
29         StringWriter buffer = new StringWriter();
30         PrintWriter writer = new PrintWriter(buffer);
31         t.printStackTrace(writer);
32         trace.setText(buffer.toString());
33         add(trace);
34 
35         t.printStackTrace();
36 
37         add(Box.createVerticalGlue());
38     }
39 
40     // Passthrough Scrollable implementation from DirectoryStack and JTextArea:
41 
getScrollableTracksViewportHeight()42     public boolean getScrollableTracksViewportHeight() {
43         return trace.getScrollableTracksViewportHeight();
44     }
45 
getScrollableTracksViewportWidth()46     public boolean getScrollableTracksViewportWidth() {
47         return trace.getScrollableTracksViewportWidth();
48     }
49 
getPreferredScrollableViewportSize()50     public Dimension getPreferredScrollableViewportSize() {
51         return trace.getPreferredSize();
52     }
53 
getScrollableBlockIncrement( Rectangle visibleRect, int orientation, int direction )54     public int getScrollableBlockIncrement(
55         Rectangle visibleRect, int orientation, int direction
56     ) {
57         return trace.getScrollableBlockIncrement(
58             visibleRect, orientation, direction
59         );
60     }
61 
getScrollableUnitIncrement( Rectangle visibleRect, int orientation, int direction )62     public int getScrollableUnitIncrement(
63         Rectangle visibleRect, int orientation, int direction
64     ) {
65         return trace.getScrollableUnitIncrement(
66             visibleRect, orientation, direction
67         );
68     }
69 }
70