KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > ErrorHandler


1 package jimm.datavision;
2 import jimm.util.StringUtils;
3 import jimm.util.I18N;
4 import java.sql.SQLException JavaDoc;
5 import javax.swing.JOptionPane JavaDoc;
6
7 /**
8  * This class provides static methods for displaying error messages.
9  * It displays error messages to System.err. When the GUI is being used
10  * it displays error messages in dialog boxes as well.
11  * <p>
12  * The only shortcoming is that you have to explicitly tell this class
13  * whether to use the GUI or not.
14  * <p>
15  * This class is also used by other parts of the system to determine
16  * if the report is being run with or without a GUI.
17  *
18  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
19  */

20 public class ErrorHandler {
21
22 protected static final int MAX_MESSAGE_WIDTH = 66;
23
24 protected static boolean useGUI = false;
25
26 /**
27  * Tells the error handler routines whether to display error messages
28  * in dialog boxes or not.
29  *
30  * @param b if <code>true</code>, all error messages will be displayed
31  * using a dialog box in addition to being printed on System.err
32  */

33 public static void useGUI(boolean b) { useGUI = b; }
34
35 /**
36  * Returns <code>true</code> if we've been told to use the GUI.
37  *
38  * @return <code>true</code> if we've been told to use the GUI
39  */

40 public static boolean usingGUI() { return useGUI; }
41
42 /**
43  * Displays an error message.
44  *
45  * @param message the error message; may be <code>null</code>,
46  * but that would be rather silly
47  */

48 public static void error(String JavaDoc message) {
49     error(message, null, null);
50 }
51
52 /**
53  * Displays an error message and an exception (actually a
54  * <code>Throwable</code>) Both arguments are optionally <code>null</code>.
55  *
56  * @param message the error message; may be <code>null</code>
57  * @param t a throwable; may be <code>null</code>
58  */

59 public static void error(String JavaDoc message, Throwable JavaDoc t) {
60     error(message, t, null);
61 }
62
63 /**
64  * Displays an error message with the given window title. Both
65  * arguments are optionally <code>null</code>.
66  *
67  * @param message the error message; may be <code>null</code>
68  * @param windowTitle a string to use as the dialog title; may be
69  * <code>null</code>
70  */

71 public static void error(String JavaDoc message, String JavaDoc windowTitle) {
72     error(message, null, windowTitle);
73 }
74
75 /**
76  * Displays an exception (actually a <code>Throwable</code>). It may
77  * be <code>null</code>, but that would be rather silly.
78  *
79  * @param t a throwable; may be <code>null</code>
80  */

81 public static void error(Throwable JavaDoc t) {
82     error(null, t, null);
83 }
84
85 /**
86  * Displays an error message and an exception (actually a
87  * <code>Throwable</code>) with the given window title. All three
88  * arguments are optionally <code>null</code>.
89  *
90  * @param t a throwable; may be <code>null</code>
91  * @param windowTitle a string to use as the dialog title; may be
92  * <code>null</code>
93  */

94 public static void error(Throwable JavaDoc t, String JavaDoc windowTitle) {
95     error(null, t, windowTitle);
96 }
97
98 /**
99  * Displays an error message and an exception (actually a
100  * <code>Throwable</code>) with the given window title. All three
101  * arguments are optionally <code>null</code>.
102  *
103  * @param message the error message; may be <code>null</code>
104  * @param t a throwable; may be <code>null</code>
105  * @param windowTitle a string to use as the dialog title; may be
106  * <code>null</code>
107  */

108 public static void error(String JavaDoc message, Throwable JavaDoc t, String JavaDoc windowTitle) {
109     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
110     if (message != null) StringUtils.splitUp(buf, message, MAX_MESSAGE_WIDTH);
111     if (t != null) {
112     if (message != null) buf.append("\n");
113     StringUtils.splitUp(buf, t.toString(), MAX_MESSAGE_WIDTH);
114     if (t instanceof SQLException JavaDoc) {
115         SQLException JavaDoc ex = (SQLException JavaDoc)t;
116         ex = ex.getNextException();
117         while (ex != null) {
118         buf.append("\n");
119         StringUtils.splitUp(buf, ex.toString(), MAX_MESSAGE_WIDTH);
120         ex = ex.getNextException();
121         }
122     }
123     }
124     String JavaDoc errorMessage = buf.toString();
125
126     System.err.println("DataVision v" + info.Version);
127     if (windowTitle != null)
128     System.err.print(windowTitle + ": ");
129     System.err.println(errorMessage);
130
131     if (t != null)
132     t.printStackTrace();
133
134     if (useGUI) {
135     if (windowTitle == null)
136         windowTitle = I18N.get("ErrorHandler.default_win_title");
137     JOptionPane.showMessageDialog(null, errorMessage, windowTitle,
138                       JOptionPane.ERROR_MESSAGE);
139     }
140 }
141
142 }
143
Popular Tags