1 package jimm.datavision; 2 import jimm.util.StringUtils; 3 import jimm.util.I18N; 4 import java.sql.SQLException ; 5 import javax.swing.JOptionPane ; 6 7 20 public class ErrorHandler { 21 22 protected static final int MAX_MESSAGE_WIDTH = 66; 23 24 protected static boolean useGUI = false; 25 26 33 public static void useGUI(boolean b) { useGUI = b; } 34 35 40 public static boolean usingGUI() { return useGUI; } 41 42 48 public static void error(String message) { 49 error(message, null, null); 50 } 51 52 59 public static void error(String message, Throwable t) { 60 error(message, t, null); 61 } 62 63 71 public static void error(String message, String windowTitle) { 72 error(message, null, windowTitle); 73 } 74 75 81 public static void error(Throwable t) { 82 error(null, t, null); 83 } 84 85 94 public static void error(Throwable t, String windowTitle) { 95 error(null, t, windowTitle); 96 } 97 98 108 public static void error(String message, Throwable t, String windowTitle) { 109 StringBuffer buf = new StringBuffer (); 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 ) { 115 SQLException ex = (SQLException )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 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 |