1 51 package org.apache.fop.viewer; 52 53 import java.awt.*; 54 import org.apache.fop.messaging.MessageHandler; 55 import java.io.*; 56 import javax.swing.*; 57 58 78 public class UserMessage { 79 80 private static Translator res = null; 81 82 public static void setTranslator(Translator aRes) { 83 res = aRes; 84 if (res == null) { 85 MessageHandler.logln("UserMessage: setTranslator(null) !"); 86 res = new SecureResourceBundle(null); 87 } 88 89 MessagesDialog.setTranslator(res); 90 } 91 92 93 95 98 private static final int INFO = JOptionPane.PLAIN_MESSAGE; 99 100 103 private static final int WARNING = JOptionPane.WARNING_MESSAGE; 104 105 108 private static final int QUESTION = JOptionPane.QUESTION_MESSAGE; 109 110 113 private static final int ERROR = JOptionPane.ERROR_MESSAGE; 114 115 118 private static final int SYS_ERROR = JOptionPane.ERROR_MESSAGE; 119 120 121 122 126 127 130 public static final int STYLE_NOBUTTON = -2; 131 132 135 public static final int STYLE_Y = JOptionPane.DEFAULT_OPTION; 136 138 141 public static final int STYLE_Y_N = JOptionPane.YES_NO_OPTION; 142 144 147 public static final int STYLE_Y_N_C = JOptionPane.YES_NO_CANCEL_OPTION; 148 150 151 private static final String PARAMETER_TAG = "&&&"; 153 154 155 159 160 163 public static final int YES = JOptionPane.YES_OPTION; 164 166 169 public static final int NO = JOptionPane.NO_OPTION; 170 172 175 public static final int CANCEL = JOptionPane.CANCEL_OPTION; 176 178 179 private static int buttonType = STYLE_Y; 181 private static int iconType = WARNING; 182 private static String currentIconName = ""; 183 private static String actMessId = null; 184 186 187 190 static String prepareMessage(String rawText, String [] par) { 191 MessageHandler.logln("prepareMessage(): " + rawText + ", parameter: " 192 + par); 193 int index = rawText.indexOf(PARAMETER_TAG); 194 String composedMess = ""; 195 if ((index == -1) && (par == null)) 196 return rawText; 197 if ((index != -1) && (par == null)) { 198 MessageHandler.logln("Message " + actMessId 199 + " erwartet Parameter. Aufgerufen ohne Parameter"); 200 return rawText; 201 } 202 if ((index == -1) && (par != null)) { 203 MessageHandler.logln("Message " + actMessId 204 + " erwartet keine Parameter. Aufgerufen mit folgenden Parametern:"); 205 for (int i = 0; i < par.length; ++i) 206 MessageHandler.logln(par[i].toString()); 207 return rawText; 208 } 209 int tagCount = 0; 210 while (rawText.indexOf(PARAMETER_TAG) != -1) { 211 index = rawText.indexOf(PARAMETER_TAG); 212 try { 213 composedMess += rawText.substring(0, index) + par[tagCount]; 214 } catch (ArrayIndexOutOfBoundsException ex) { 215 MessageHandler.logln("Anzahl der übergebenen Parameter zu der Meldung " 216 + actMessId 217 + " ist weniger als erwartet."); 218 ex.printStackTrace(); 219 return composedMess + rawText; 220 } 221 rawText = rawText.substring(index + PARAMETER_TAG.length()); 222 tagCount++; 223 } 224 composedMess += rawText; 225 if (tagCount != par.length) 226 MessageHandler.logln("Die zu der Meldung " + actMessId 227 + " übergebenen Parameter sind mehr als die Meldung vorsieht."); 228 return composedMess; 229 } 230 231 236 static String getTitle(String strVal) { 237 String title = null; 238 int choice = getValue(strVal); 239 switch (choice) { 240 case INFO: 241 title = "Info"; 242 currentIconName = "info.gif"; 243 break; 244 case WARNING: 245 title = "Warning"; 246 currentIconName = "warn.gif"; 247 break; 248 case QUESTION: 249 title = "Question"; 250 currentIconName = "quest.gif"; 251 break; 252 case ERROR: 253 title = "Error"; 254 currentIconName = "error.gif"; 255 break; 256 default: 257 title = 258 "Ungültiger IonType für diese Meldung. Prüfen in Übersetzungsdatei."; 259 } 260 return title; 261 } 262 263 266 static int getValue(String fieldName) { 267 268 int val = -1; 269 if (fieldName.equals("INFO")) 270 return INFO; 271 else if (fieldName.equals("WARNING")) 272 return WARNING; 273 else if (fieldName.equals("ERROR")) 274 return ERROR; 275 else if (fieldName.equals("SYS_ERROR")) 276 return SYS_ERROR; 277 else if (fieldName.equals("QUESTION")) 278 return QUESTION; 279 else if (fieldName.equals("STYLE_NOBUTTON")) 280 return STYLE_NOBUTTON; 281 else if (fieldName.equals("STYLE_Y")) 282 return STYLE_Y; 283 else if (fieldName.equals("STYLE_Y_N")) 284 return STYLE_Y_N; 285 else if (fieldName.equals("STYLE_Y_N_C")) 286 return STYLE_Y_N_C; 287 return val; 288 } 289 290 private static String getStackTrace(Exception exception) { 291 292 if (exception == null) 293 return "null"; 294 295 String stack = ""; 296 297 StringWriter strWriter = new StringWriter(); 298 exception.printStackTrace(new PrintWriter(strWriter)); 299 stack = strWriter.toString(); 300 stack = stack.replace('\r', ' '); 301 stack = stack.replace('\t', ' '); 302 return stack; 303 } 304 305 private static int display(String textID, String [] param, 306 Exception exception, Frame frame) { 307 308 String translatedMes = ""; 309 String preparedMes = ""; 310 String messageType = ""; 311 String optionType = ""; 312 String title = ""; 313 MessagesDialog dialog = null; 314 actMessId = textID; 315 316 boolean messageFound = res.contains(textID); 317 translatedMes = res.getString(textID); 318 319 if (messageFound) { 320 try { 321 messageType = 322 translatedMes.substring(0, translatedMes.indexOf(':')); 323 translatedMes = 324 translatedMes.substring(translatedMes.indexOf(':') + 1); 325 326 optionType = 327 translatedMes.substring(0, translatedMes.indexOf(':')); 328 translatedMes = 329 translatedMes.substring(translatedMes.indexOf(':') + 1); 330 331 } catch (Exception ex) { 332 MessageHandler.logln("FALSCHES FORMAT: MESSAGE: " + textID); 333 } 334 } else { MessageHandler.logln("UserMessage: textID '" + textID 336 + "' not found. Return " 337 + "value 'CANCEL' = " + CANCEL); 338 339 341 messageType = "ERROR"; 342 optionType = "STYLE_Y"; 343 translatedMes = "textID '" + textID + "' not found." 344 + "\nIt is possible the message file not found."; 345 } 346 347 preparedMes = prepareMessage(translatedMes, param); 348 350 title = res.getString(getTitle(messageType)); 352 353 int messageTypeIndex = getValue(messageType); 355 356 int optionTypeIndex = getValue(optionType); 358 359 int result = CANCEL; 360 361 if (exception != null) { 362 String str = getStackTrace(exception); 363 if (exception instanceof MessageException) { 364 MessageException ex = (MessageException)exception; 365 if (ex.getException() != null) 366 str += "\n" + getStackTrace(ex.getException()); 367 } 368 result = MessagesDialog.showDetailDialog(null, preparedMes, 369 title, optionTypeIndex, 370 messageTypeIndex, null, 371 str); 372 } else { 373 if (optionTypeIndex == STYLE_NOBUTTON) { 374 MessageHandler.logln("UserMessage: STYLE_NOBUTTON wird nicht unterstützt"); 376 return result; 377 } else { 378 result = MessagesDialog.showConfirmDialog(null, preparedMes, 379 title, 380 optionTypeIndex, 381 messageTypeIndex); 382 } 383 } 384 return result; 385 } 386 387 404 public static int show(String messageId, String [] parameterList, 405 Exception anException, Frame parentFrame) { 406 return display(messageId, parameterList, anException, parentFrame); 407 } 408 409 public static int show(String messageId, String [] parameterList, 410 Exception anException) { 411 return display(messageId, parameterList, anException, (Frame)null); 412 } 413 414 public static int show(String messageId, String [] parameterList, 415 Frame parentFrame) { 416 return display(messageId, parameterList, (Exception )null, 417 parentFrame); 418 } 419 420 public static int show(String messageId, String [] parameterList) { 421 return display(messageId, parameterList, (Exception )null, 422 (Frame)null); 423 } 424 425 public static int show(String messageId, String parameter, 426 Frame parentFrame) { 427 return display(messageId, new String [] { 428 parameter 429 }, (Exception )null, parentFrame); 430 } 431 432 public static int show(String messageId, String parameter) { 433 return display(messageId, new String [] { 434 parameter 435 }, (Exception )null, (Frame)null); 436 } 437 438 public static int show(String messageId, Frame parentFrame) { 439 return display(messageId, (String [])null, (Exception )null, 440 parentFrame); 441 } 442 443 public static int show(String messageId) { 444 return display(messageId, (String [])null, (Exception )null, 445 (Frame)null); 446 } 447 448 public static int show(String messageId, Exception anException, 449 Frame aFrame) { 450 return display(messageId, (String [])null, anException, aFrame); 451 } 452 453 public static int show(String messageId, Exception anException) { 454 return display(messageId, (String [])null, anException, (Frame)null); 455 } 456 457 public static int show(Exception anException, Frame aFrame) { 458 if (anException instanceof MessageException) { 459 MessageException ex = (MessageException)anException; 460 return show(ex.getMessageId(), ex.getParameterList(), ex, aFrame); 461 } else 462 return show("UNHANDLED_EXCEPTION", (String [])null, anException, 463 aFrame); 464 } 465 466 } 467 468 | Popular Tags |