00001 00002 // // 00003 // Dialogs.java // 00004 // // 00005 // util.Dialogs -- generally useful stuff // 00006 // Last edited: January 14, 2002 at 6:31 pm // 00007 // // 00008 // (c) Copyright 2002 Rice University. All rights reserved. // 00009 // // 00011 00012 00013 00014 00015 package edu.rice.cs.hpc.data.util; 00016 00017 00018 00019 00020 00022 // CLASS DIALOGS // 00024 00032 public class Dialogs 00033 { 00034 00035 00037 //private static JFrame parent; 00038 00040 private static boolean warnTempMethods = false; 00041 00042 00043 00044 00045 /************************************************************************* 00046 * Sets the frame to be used as parent for dialogs. 00047 ************************************************************************/ 00048 /* 00049 public static void setParent(JFrame p) 00050 { 00051 Dialogs.parent = p; 00052 } 00053 00054 */ 00055 00056 00057 /************************************************************************* 00058 * Runs an error dialog if an Assertion fails. 00059 * 00060 * @param predicate The result of evaluating the Asserted predicate. 00061 * 00062 ************************************************************************/ 00063 00064 public static void Assert(boolean predicate) 00065 { 00066 if( ! predicate ) Dialogs.fail("Assertion failed"); 00067 } 00068 00069 public static void Assert(boolean predicate, String msg) 00070 { 00071 if( ! predicate ) Dialogs.fail("Assertion failed: " + msg ); 00072 } 00073 00074 00075 00076 /************************************************************************* 00077 * Runs a standard file chooser dialog. 00078 * 00079 * @return The chosen file, or <code>null</code> if the user cancels. 00080 * 00081 ************************************************************************/ 00082 /* 00083 public static File chooseFile() 00084 { 00085 File file; 00086 00087 JFileChooser chooser = new JFileChooser(); 00088 chooser.setCurrentDirectory(Dialogs.lastDirectory); 00089 00090 int result = chooser.showOpenDialog(null); 00091 Dialogs.lastDirectory = chooser.getCurrentDirectory(); 00092 00093 if( result == JFileChooser.APPROVE_OPTION ) 00094 file = chooser.getSelectedFile(); 00095 else 00096 file = null; 00097 00098 return file; 00099 } 00100 00101 */ 00102 00103 00104 /************************************************************************* 00105 * Runs a dialog for a notification message. 00106 * 00107 * @param message The notification message text. 00108 * 00109 ************************************************************************/ 00110 00111 public static void notify(String message) 00112 { 00113 String[] lines = new String[] { message }; 00114 Dialogs.message(lines, false, false); 00115 } 00116 00117 00118 00119 00120 /************************************************************************* 00121 * Runs a dialog for a notification message. 00122 * 00123 * @param message The notification message text. 00124 * @param what A string to be appended to the message text. 00125 * 00126 ************************************************************************/ 00127 00128 public static void notify(String message, String what) 00129 { 00130 String[] lines = new String[] { message, what }; 00131 Dialogs.message(lines, false, false); 00132 } 00133 00134 00135 00136 00137 /************************************************************************* 00138 * Runs a dialog for a nonfatal exception. 00139 * 00140 * @param message The error message text. 00141 * @param what A string to be appended to the message text. 00142 * @param ex The exception to be described by the dialog. 00143 * 00144 ************************************************************************/ 00145 00146 public static void exception(String message, String what, Exception e) 00147 { 00148 Dialogs.exceptionMessage(message, what, e, false); 00149 } 00150 00151 00152 00153 00154 /************************************************************************* 00155 * Runs a dialog for a fatal exception. 00156 * 00157 * @param message The error message text. 00158 * @param what A string to be appended to the message text. 00159 * @param ex The exception to be described by the dialog. 00160 * 00161 ************************************************************************/ 00162 00163 public static void fatalException(String message, String what, Exception e) 00164 { 00165 Dialogs.exceptionMessage(message, what, e, true); 00166 } 00167 00168 00169 00170 00171 /************************************************************************* 00172 * Runs an info dialog for a method whose implementation is temporary. 00173 * <p> 00174 * 00175 * The dialog is only shown if debugging variable <code>warnTempMethods</code> 00176 * is set to true. 00177 * 00178 * @param method The name of the unimplemented method. 00179 * 00180 ************************************************************************/ 00181 00182 public static void temporary(String method) 00183 { 00184 if( Dialogs.warnTempMethods ) 00185 { 00186 String[] lines = new String[] { Strings.TEMPORARY_METHOD + ":", method }; 00187 Dialogs.message(lines, false, true); 00188 } 00189 } 00190 00191 00192 00193 00194 /************************************************************************* 00195 * Runs an error dialog for an unimplemented method. 00196 * 00197 * @param method The name of the unimplemented method. 00198 * 00199 ************************************************************************/ 00200 00201 public static void notImplemented(String method) 00202 { 00203 String[] lines = new String[] { Strings.NOT_IMPLEMENTED + ":", method }; 00204 Dialogs.message(lines, true, true); 00205 } 00206 00207 00208 00209 00210 /************************************************************************* 00211 * Runs a warning-only dialog for an unimplemented method. 00212 * 00213 * @param method The name of the unimplemented method. 00214 * 00215 ************************************************************************/ 00216 00217 public static void notImplementedWarning(String method) 00218 { 00219 String[] lines = new String[] { Strings.NOT_IMPLEMENTED + ":", method }; 00220 Dialogs.message(lines, false, true); 00221 } 00222 00223 00224 00225 00226 /************************************************************************* 00227 * Runs an error dialog for a method that must be overridden by subclasses. 00228 * 00229 * @param method The name of the method that should have been overridden. 00230 * 00231 ************************************************************************/ 00232 00233 public static void subclassResponsibility(String method) 00234 { 00235 String[] lines = new String[] { Strings.SUBCLASS_RESPONSIBILITY + ":", method }; 00236 Dialogs.message(lines, true, true); 00237 } 00238 00239 00240 00241 00242 /************************************************************************* 00243 * Runs an error dialog for a method that must not be called. 00244 * 00245 * @param method The name of the method that should not have been called. 00246 * 00247 ************************************************************************/ 00248 00249 public static void notCalled(String method) 00250 { 00251 String[] lines = new String[] { Strings.NOT_CALLED + ":", method }; 00252 Dialogs.message(lines, true, true); 00253 } 00254 00255 00256 00257 00258 /************************************************************************* 00259 * Runs a fatal error dialog and aborts the application. 00260 * 00261 * @param why The explanation of what failure has occurred. 00262 * 00263 ************************************************************************/ 00264 00265 public static void fail(String why) 00266 { 00267 String[] lines = new String[] { Strings.FATAL_ERROR + ":", why }; 00268 Dialogs.message(lines, true, true); 00269 } 00270 00271 00272 00273 00274 /************************************************************************* 00275 * Runs a fatal error dialog and aborts the application. 00276 * 00277 * @param why The explanation of what failure has occurred. 00278 * @param what A string to be appended to the message text. 00279 * 00280 ************************************************************************/ 00281 00282 public static void fail2(String why, String what) 00283 { 00284 String[] lines = new String[] { Strings.FATAL_ERROR + ":", why + " (" + what + ")" }; 00285 Dialogs.message(lines, true, true); 00286 } 00287 00288 00289 00290 00291 /************************************************************************* 00292 * Runs a dialog for an exception. 00293 * 00294 * @param message The error message text. 00295 * @param what A string to be appended to the message text. 00296 * @param ex The exception to be described by the dialog. 00297 * @param fatal Whether execution should be aborted after this dialog. 00298 * 00299 ************************************************************************/ 00300 00301 private static void exceptionMessage(String message, String what, Exception e, boolean fatal) 00302 { 00303 e.printStackTrace(); 00304 00305 String errorKind = e.getClass() + ""; 00306 String errorWhat = e.getMessage(); 00307 String[] lines = new String[] { message, what, " ", errorKind, errorWhat }; 00308 Dialogs.message(lines, true, fatal); 00309 } 00310 00311 00312 00313 00314 /************************************************************************* 00315 * Runs a warning or fatal dialog and aborts if requested. 00316 ************************************************************************/ 00317 00318 private static void message(String[] lines, boolean error, boolean bad) 00319 { 00320 StringBuffer sb = new StringBuffer(); 00321 for (int i=0; i<lines.length; i++) { 00322 sb.append(lines[i]); 00323 sb.append(" "); 00324 } 00325 System.err.println(sb.toString()); 00326 // Laks: it is better not to have any "costly" statement here 00327 // String title = Strings.APPNAME + " " + (error ? Strings.ERROR : (bad ? Strings.WARNING : Strings.MESSAGE)); 00328 // int kind = (error ? JOptionPane.ERROR_MESSAGE : (bad ? JOptionPane.WARNING_MESSAGE : JOptionPane.PLAIN_MESSAGE)); 00329 00330 // JOptionPane.showMessageDialog(Dialogs.parent, lines, title, kind); 00331 00332 // if( (error & bad) ) 00333 // HPCViewerApplication.getCurrentApp().abort(); 00334 } 00335 00336 00337 00338 00339 }