001: package jimm.datavision;
002:
003: import jimm.util.StringUtils;
004: import jimm.util.I18N;
005: import java.sql.SQLException;
006: import javax.swing.JOptionPane;
007:
008: /**
009: * This class provides static methods for displaying error messages.
010: * It displays error messages to System.err. When the GUI is being used
011: * it displays error messages in dialog boxes as well.
012: * <p>
013: * The only shortcoming is that you have to explicitly tell this class
014: * whether to use the GUI or not.
015: * <p>
016: * This class is also used by other parts of the system to determine
017: * if the report is being run with or without a GUI.
018: *
019: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
020: */
021: public class ErrorHandler {
022:
023: protected static final int MAX_MESSAGE_WIDTH = 66;
024:
025: protected static boolean useGUI = false;
026:
027: /**
028: * Tells the error handler routines whether to display error messages
029: * in dialog boxes or not.
030: *
031: * @param b if <code>true</code>, all error messages will be displayed
032: * using a dialog box in addition to being printed on System.err
033: */
034: public static void useGUI(boolean b) {
035: useGUI = b;
036: }
037:
038: /**
039: * Returns <code>true</code> if we've been told to use the GUI.
040: *
041: * @return <code>true</code> if we've been told to use the GUI
042: */
043: public static boolean usingGUI() {
044: return useGUI;
045: }
046:
047: /**
048: * Displays an error message.
049: *
050: * @param message the error message; may be <code>null</code>,
051: * but that would be rather silly
052: */
053: public static void error(String message) {
054: error(message, null, null);
055: }
056:
057: /**
058: * Displays an error message and an exception (actually a
059: * <code>Throwable</code>) Both arguments are optionally <code>null</code>.
060: *
061: * @param message the error message; may be <code>null</code>
062: * @param t a throwable; may be <code>null</code>
063: */
064: public static void error(String message, Throwable t) {
065: error(message, t, null);
066: }
067:
068: /**
069: * Displays an error message with the given window title. Both
070: * arguments are optionally <code>null</code>.
071: *
072: * @param message the error message; may be <code>null</code>
073: * @param windowTitle a string to use as the dialog title; may be
074: * <code>null</code>
075: */
076: public static void error(String message, String windowTitle) {
077: error(message, null, windowTitle);
078: }
079:
080: /**
081: * Displays an exception (actually a <code>Throwable</code>). It may
082: * be <code>null</code>, but that would be rather silly.
083: *
084: * @param t a throwable; may be <code>null</code>
085: */
086: public static void error(Throwable t) {
087: error(null, t, null);
088: }
089:
090: /**
091: * Displays an error message and an exception (actually a
092: * <code>Throwable</code>) with the given window title. All three
093: * arguments are optionally <code>null</code>.
094: *
095: * @param t a throwable; may be <code>null</code>
096: * @param windowTitle a string to use as the dialog title; may be
097: * <code>null</code>
098: */
099: public static void error(Throwable t, String windowTitle) {
100: error(null, t, windowTitle);
101: }
102:
103: /**
104: * Displays an error message and an exception (actually a
105: * <code>Throwable</code>) with the given window title. All three
106: * arguments are optionally <code>null</code>.
107: *
108: * @param message the error message; may be <code>null</code>
109: * @param t a throwable; may be <code>null</code>
110: * @param windowTitle a string to use as the dialog title; may be
111: * <code>null</code>
112: */
113: public static void error(String message, Throwable t,
114: String windowTitle) {
115: StringBuffer buf = new StringBuffer();
116: if (message != null)
117: StringUtils.splitUp(buf, message, MAX_MESSAGE_WIDTH);
118: if (t != null) {
119: if (message != null)
120: buf.append("\n");
121: StringUtils.splitUp(buf, t.toString(), MAX_MESSAGE_WIDTH);
122: if (t instanceof SQLException) {
123: SQLException ex = (SQLException) t;
124: ex = ex.getNextException();
125: while (ex != null) {
126: buf.append("\n");
127: StringUtils.splitUp(buf, ex.toString(),
128: MAX_MESSAGE_WIDTH);
129: ex = ex.getNextException();
130: }
131: }
132: }
133: String errorMessage = buf.toString();
134:
135: System.err.println("DataVision v" + info.Version);
136: if (windowTitle != null)
137: System.err.print(windowTitle + ": ");
138: System.err.println(errorMessage);
139:
140: if (t != null)
141: t.printStackTrace();
142:
143: if (useGUI) {
144: if (windowTitle == null)
145: windowTitle = I18N
146: .get("ErrorHandler.default_win_title");
147: JOptionPane.showMessageDialog(null, errorMessage,
148: windowTitle, JOptionPane.ERROR_MESSAGE);
149: }
150: }
151:
152: }
|