KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBErrorWin


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4 import javax.swing.border.TitledBorder JavaDoc;
5 import java.awt.*;
6 import java.awt.event.ActionEvent JavaDoc;
7 import java.awt.event.ActionListener JavaDoc;
8 import java.io.PrintWriter JavaDoc;
9 import java.io.StringWriter JavaDoc;
10
11 /**
12  * Provides a small multi-line error reporting window, with an
13  * option to pull down a detailed error message.
14  */

15
16 public class CBErrorWin extends JDialog
17 {
18
19     String JavaDoc msg;
20     String JavaDoc error;
21     String JavaDoc title;
22     boolean haveErrorMsg;
23     CBButton OK, Details;
24     CBPanel display;
25     Exception JavaDoc exception;
26
27     /**
28      * Constructor - take a parent frame, a message,
29      * and a detailed error message (may be null).
30      * <p/>
31      * This creates a short multi-line text displayed, with a
32      * 'details' button which expands the display to show the
33      * full error message as well.
34      *
35      * @param owner the component (from which the parent Frame will be derived)
36      * @param Msg a short one line message to display to the user
37      * @param e the exception to log
38      */

39
40     public CBErrorWin(Dialog owner, String JavaDoc Msg, Exception JavaDoc e)
41     {
42         super(owner);
43         title = CBIntText.get("Error Encountered");
44         commonConstructor(Msg, e);
45     }
46
47     /**
48      * Constructor - take a parent frame, a message,
49      * and a detailed error message (may be null).
50      * <p/>
51      * This creates a short multi-line text displayed, with a
52      * 'details' button which expands the display to show the
53      * full error message as well.
54      *
55      * @param owner the component (from which the parent Frame will be derived)
56      * @param Msg a short one line message to display to the user
57      * @param e the exception to log
58      */

59
60     public CBErrorWin(Frame owner, String JavaDoc Msg, Exception JavaDoc e)
61     {
62         super(owner);
63         title = CBIntText.get("Error Encountered");
64         commonConstructor(Msg, e);
65     }
66
67
68     /**
69      * Constructor - takes a parent frame, a confirmation message,
70      * and creates a short multi-line text display.
71      *
72      * @param owner the component (from which the parent Frame will be derived)
73      * @param Msg a short one line confirmation message to display to the user.
74      */

75
76     public CBErrorWin(Frame owner, String JavaDoc Msg, String JavaDoc msgTitle)
77     {
78         super(owner);
79         title = msgTitle;
80         commonConstructor(Msg, null);
81     }
82
83
84     /**
85      * This creates a short multi-line text displayed, with a
86      * 'details' button which expands the display to show the
87      * full error message as well.
88      *
89      * @param Msg a short one line message to display to the user.
90      * @param e the exception to log.
91      */

92
93     public void commonConstructor(String JavaDoc Msg, Exception JavaDoc e)
94     {
95         setModal(true);
96
97         exception = e;
98
99         setTitle(CBIntText.get(title));
100
101         msg = (Msg == null) ? CBIntText.get("No Message Given") : Msg;
102
103         haveErrorMsg = (e != null);
104         error = (haveErrorMsg) ? e.toString() : CBIntText.get("No specific information");
105
106         JScrollPane scrollPane = new JScrollPane(makeTextArea(msg));
107         scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); //TE: no horizontal scroll bar.
108

109         display = new CBPanel();
110         display.makeHeavy();
111
112         display.addLine(scrollPane);
113
114         display.makeLight();
115         JPanel buttons = new JPanel();
116
117         buttons.add(OK = new CBButton(CBIntText.get("OK"), CBIntText.get("Click to close error window.")));
118
119         if (haveErrorMsg != false) //TE: The 'Details' button is only added if there is a message to diplay
120
buttons.add(Details = new CBButton(CBIntText.get("Details"), CBIntText.get("Click to display the full error message.")));
121
122         display.add(buttons);
123
124         OK.addActionListener(new ActionListener JavaDoc()
125         {
126             public void actionPerformed(ActionEvent JavaDoc e)
127             {
128                 setVisible(false);
129                 dispose();
130             }
131         });
132
133         if (haveErrorMsg != false) //TE: Only add the listener if there is a message (otherwise the 'Details' button is not added).
134
{
135             Details.addActionListener(new ActionListener JavaDoc()
136             {
137                 public void actionPerformed(ActionEvent JavaDoc e)
138                 {
139                     JScrollPane scrollPane2 = new JScrollPane(makeTextArea(msg));
140                     final JScrollPane newPane = new JScrollPane(makeTextArea(error));
141
142                     TitledBorder JavaDoc border = new TitledBorder JavaDoc(CBIntText.get("error details"));
143                     newPane.setBorder(border);
144
145                     CBErrorWin.this.getContentPane().remove(display);
146
147                     display = new CBPanel();
148
149                     display.makeHeavy();
150
151                     display.addLine(scrollPane2);
152                     display.addLine(newPane);
153
154                     display.makeLight();
155
156                     /*
157                      * Add a 'print stack' button
158                      */

159
160                     JPanel buttons2 = new JPanel();
161
162                     if (exception != null)
163                     {
164                         JButton StackTrace = new JButton(CBIntText.get("Print Stack"));
165                         StackTrace.setToolTipText(CBIntText.get("Prints a stack trace to the console (if active)"));
166                         StackTrace.addActionListener(new ActionListener JavaDoc()
167                         {
168                             public void actionPerformed(ActionEvent JavaDoc e)
169                             {
170                                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
171                                 exception.printStackTrace(new PrintWriter JavaDoc(sw));
172                                 String JavaDoc trace = sw.toString();
173                                 newPane.setViewportView(makeTextArea(trace));
174                                 exception.printStackTrace(); // echo to console.
175
}
176                         });
177                         buttons2.add(StackTrace);
178                     }
179
180                     buttons2.add(OK);
181                     display.add(buttons2);
182
183                     CBErrorWin.this.setContentPane(display);
184                     CBErrorWin.this.setSize(new Dimension(getWidth(), getHeight() + 100));
185                     CBErrorWin.this.setVisible(true);
186                 }
187             });
188         }
189
190         //TE: better way to implement keystroke listening...
191

192         display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), "enter");
193         display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escape");
194         display.getActionMap().put("enter", new MyAction(CBAction.ENTER));
195         display.getActionMap().put("escape", new MyAction(CBAction.ESCAPE));
196
197         setContentPane(display);
198         setSize(new Dimension(400, 150));
199         CBUtility.center(this, getOwner());
200         setVisible(true);
201     }
202
203
204     /**
205      * Apparently it is better to use key bindings rather than adding a KeyListener...
206      * "for reacting in a special way to particular keys, you usually should use key
207      * bindings instead of a key listener".
208      * This class lets the user set the key as an int. If a key is pressed and it
209      * matches the assigned int, a check is done for if it is an escape or enter key.
210      * (27 or 10). If escape or enter is pressed, the window is closed and the dialog
211      * disposed.
212      * Bug 4646.
213      *
214      * @author Trudi.
215      */

216     private class MyAction extends CBAction
217     {
218         /**
219          * Calls super constructor.
220          *
221          * @param key
222          */

223         public MyAction(int key)
224         {
225             super(key);
226         }
227
228         /**
229          * Closes the window and disposes the dialog.
230          *
231          * @param e never used.
232          */

233         public void actionPerformed(ActionEvent JavaDoc e)
234         {
235             setVisible(false);
236             dispose();
237         }
238     }
239
240
241     /**
242      * Constructs a text area with its initial text set. Sets the back ground colour,
243      * disables the text area, sets the text colour to black and allows line wrapping by word.
244      *
245      * @param text the text to be automatically set in the text field.
246      * @return the JTextArea component.
247      */

248
249     protected JTextArea makeTextArea(String JavaDoc text)
250     {
251         JTextArea a = new JTextArea(text);
252
253         a.setBackground(CBErrorWin.this.getBackground());
254         a.setEnabled(true); // CB: make 'true' to allow copy and paste!
255
a.setDisabledTextColor(Color.black); //TE: sets the disabled text colour to black.
256
a.setLineWrap(true); //TE: allows line wrapping.
257
a.setWrapStyleWord(true); //TE: sets line wrapping by word.
258

259
260         /*
261          * Initially component was disabled, since we wanted hitting 'enter' to close the window (and we don't want
262          * the users editing the message). However, we also want them to be able to copy and paste... so I've
263          * enabled the window but added a specific key listener to over-ride the default text area behaviour.
264          */

265         a.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"), "enter"); // nb - use specific 'WHEN_FOCUSED', rather than more general 'WHEN_IN_FOCUSED_WINDOW', otherwise Swing default focus behaviour will get priority.
266
a.getActionMap().put("enter", new MyAction(CBAction.ENTER));
267
268         return a;
269     }
270 }
Popular Tags