123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- /*
- * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
- * For details on use and redistribution please refer to the
- * LICENSE file included with these sources.
- */
-
- package org.apache.fop.viewer;
-
- import java.awt.*;
- import java.io.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.JComponent.*;
-
-
-
- /**
- * Klasse <code>UserMessage</code> ist ein utility zum Abfragen oder zum Informieren des Benutzers.<br>
- * Eine Meldung besteht aus dem Identifikator (Suchschlüssel im Meldungspool), einem Dialogtitel, einem Buttonset und
- * dem Meldungstext mit eventuellen Platzhaltern für die Parameter.
- *
- * @author S. Gorkhover
- * @version 18.03.1999
- *
- * @changed 23.04.99 Juergen.Verwohlt@jCatalog.com
- * @subject Weitere Ausgabemethoden: show(String, String) und show(String,String,Frame)
- *
- * @changed 28.05.99 Juergen.Verwohlt@jCatalog.com
- * @subject MessageException unterstützen
- *
- * @changed 09.06.99 Juergen.Verwohlt@jCatalog.com
- * @subject Neue Klasse MessagesException zur Anzeige verwenden
- *
- * @changed 10.12.00 gears@apache.org
- * @subject Unicode
- */
- public class UserMessage {
-
- private static Translator res = null;
-
- public static void setTranslator(Translator aRes) {
- res = aRes;
- if (res == null) {
- //log.debug("UserMessage: setTranslator(null) !");
- res = new SecureResourceBundle(null);
- }
-
- MessagesDialog.setTranslator(res);
- }
-
-
- // Zulässige Werte für Dialogart:
-
- /**
- * Möglicher Wert des Meldungstypes. Fenster-Title "Info" (de).
- */
- private static final int INFO = JOptionPane.PLAIN_MESSAGE;
-
- /**
- * Möglicher Wert des Meldungstypes. Fenster-Title "Warnung" (de).
- */
- private static final int WARNING = JOptionPane.WARNING_MESSAGE;
-
- /**
- * Möglicher Wert des Meldungstypes. Fenster-Title "Frage" (de).
- */
- private static final int QUESTION = JOptionPane.QUESTION_MESSAGE;
-
- /**
- * Möglicher Wert des Meldungstypes. Fenster-Title "Fehler" (de).
- */
- private static final int ERROR = JOptionPane.ERROR_MESSAGE;
-
- /**
- * Möglicher Wert des Meldungstypes. Fenster-Title "Systemfehler" (de).
- */
- private static final int SYS_ERROR = JOptionPane.ERROR_MESSAGE;
-
-
-
- /*
- * Style Constanten orientieren sich auf die
- * Constanten der ButtonDialog-Klasse und legen das Dialog-ButtonSet fest.
- */
-
- /**
- * Wert für Setzten keines Buttons
- */
- public static final int STYLE_NOBUTTON = -2;
-
- /**
- * Wert für Setzten von nur Ja-Button.
- */
- public static final int STYLE_Y = JOptionPane.DEFAULT_OPTION;
- // ButtonDialog.YES; // = 1
-
- /**
- * Wert für Setzten von Ja- und Nein-Buttons.
- */
- public static final int STYLE_Y_N = JOptionPane.YES_NO_OPTION;
- // ButtonDialog.YES_NO; // = 2;
-
- /**
- * Wert für Setzten von Ja-, Nein-und Abbruch Buttons.
- */
- public static final int STYLE_Y_N_C = JOptionPane.YES_NO_CANCEL_OPTION;
- // ButtonDialog.YES_NO_CANCEL; // = 3;
-
-
- // Platzhalter für Parameter in der Properties-Datei
- private static final String PARAMETER_TAG = "&&&";
-
-
- /**
- * Benutzer-Antwort-Constanten orientieren sich auf die
- * Constanten der ButtonDialog-Klasse
- */
-
- /**
- * Möglicher Rückgabewert @see ButtonDialog
- */
- public static final int YES = JOptionPane.YES_OPTION;
- // ButtonDialog.YES; // = 2
-
- /**
- * Möglicher Rückgabewert @see ButtonDialog
- */
- public static final int NO = JOptionPane.NO_OPTION;
- // ButtonDialog.NO; // = 4;
-
- /**
- * Möglicher Rückgabewert @see ButtonDialog
- */
- public static final int CANCEL = JOptionPane.CANCEL_OPTION;
- // ButtonDialog.CANCEL; // = 8;
-
-
- // Default-Values
- private static int buttonType = STYLE_Y;
- private static int iconType = WARNING;
- private static String currentIconName = "";
- private static String actMessId = null;
- // private static MessagesDialog nobuttonDialog = null;
-
-
- /**
- * Ersetzt die eventuellen Platzhalter durch die übergebenen Parameter
- */
- static String prepareMessage(String rawText, String[] par) {
- //log.debug("prepareMessage(): " + rawText + ", parameter: "
- // + par);
- int index = rawText.indexOf(PARAMETER_TAG);
- String composedMess = "";
- if ((index == -1) && (par == null))
- return rawText;
- if ((index != -1) && (par == null)) {
- //log.debug("Message " + actMessId
- // + " erwartet Parameter. Aufgerufen ohne Parameter");
- return rawText;
- }
- if ((index == -1) && (par != null)) {
- //log.debug("Message " + actMessId
- // + " erwartet keine Parameter. Aufgerufen mit folgenden Parametern:");
- for (int i = 0; i < par.length; ++i) {
- //log.debug(par[i].toString());
- }
- return rawText;
- }
- int tagCount = 0;
- while (rawText.indexOf(PARAMETER_TAG) != -1) {
- index = rawText.indexOf(PARAMETER_TAG);
- try {
- composedMess += rawText.substring(0, index) + par[tagCount];
- } catch (ArrayIndexOutOfBoundsException ex) {
- //log.error("Anzahl der übergebenen Parameter zu der Meldung "
- // + actMessId
- // + " ist weniger als erwartet.", ex);
- return composedMess + rawText;
- }
- rawText = rawText.substring(index + PARAMETER_TAG.length());
- tagCount++;
- }
- composedMess += rawText;
- if (tagCount != par.length) {
- //log.debug("Die zu der Meldung " + actMessId
- // + " übergebenen Parameter sind mehr als die Meldung vorsieht.");
- }
- return composedMess;
- }
-
- /**
- * Gibt den Title fürs Dialogfenster.
- * Dieser wird durch die Dialogart festgelegt
- * (erster Teil in der MeldungsDefinition in der Properties-Datei).
- */
- static String getTitle(String strVal) {
- String title = null;
- int choice = getValue(strVal);
- switch (choice) {
- case INFO:
- title = "Info";
- currentIconName = "info.gif";
- break;
- case WARNING:
- title = "Warning";
- currentIconName = "warn.gif";
- break;
- case QUESTION:
- title = "Question";
- currentIconName = "quest.gif";
- break;
- case ERROR:
- title = "Error";
- currentIconName = "error.gif";
- break;
- default:
- title =
- "Ungültiger IonType für diese Meldung. Prüfen in Übersetzungsdatei.";
- }
- return title;
- }
-
- /**
- * Liefert den Wert der über den Nemen übergebenen Klassenvariablen.
- */
- static int getValue(String fieldName) {
-
- int val = -1;
- if (fieldName.equals("INFO"))
- return INFO;
- else if (fieldName.equals("WARNING"))
- return WARNING;
- else if (fieldName.equals("ERROR"))
- return ERROR;
- else if (fieldName.equals("SYS_ERROR"))
- return SYS_ERROR;
- else if (fieldName.equals("QUESTION"))
- return QUESTION;
- else if (fieldName.equals("STYLE_NOBUTTON"))
- return STYLE_NOBUTTON;
- else if (fieldName.equals("STYLE_Y"))
- return STYLE_Y;
- else if (fieldName.equals("STYLE_Y_N"))
- return STYLE_Y_N;
- else if (fieldName.equals("STYLE_Y_N_C"))
- return STYLE_Y_N_C;
- return val;
- }
-
- private static String getStackTrace(Exception exception) {
-
- if (exception == null)
- return "null";
-
- String stack = "";
-
- StringWriter strWriter = new StringWriter();
- exception.printStackTrace(new PrintWriter(strWriter));
- stack = strWriter.toString();
- stack = stack.replace('\r', ' ');
- stack = stack.replace('\t', ' ');
- return stack;
- }
-
- private static int display(String textID, String[] param,
- Exception exception, Frame frame) {
-
- String translatedMes = "";
- String preparedMes = "";
- String messageType = "";
- String optionType = "";
- String title = "";
- MessagesDialog dialog = null;
- actMessId = textID;
-
- boolean messageFound = res.contains(textID);
- translatedMes = res.getString(textID);
-
- if (messageFound) {
- try {
- messageType =
- translatedMes.substring(0, translatedMes.indexOf(':'));
- translatedMes =
- translatedMes.substring(translatedMes.indexOf(':') + 1);
-
- optionType =
- translatedMes.substring(0, translatedMes.indexOf(':'));
- translatedMes =
- translatedMes.substring(translatedMes.indexOf(':') + 1);
-
- } catch (Exception ex) {
- //log.debug("FALSCHES FORMAT: MESSAGE: " + textID);
- }
- } else { // Message not found
- //log.debug("UserMessage: textID '" + textID
- // + "' not found. Return "
- // + "value 'CANCEL' = " + CANCEL);
-
- // return CANCEL;
-
- messageType = "ERROR";
- optionType = "STYLE_Y";
- translatedMes = "textID '" + textID + "' not found."
- + "\nIt is possible the message file not found.";
- }
-
- preparedMes = prepareMessage(translatedMes, param);
- // Exception exception = getException(param);
-
- // WARNING -> nach Warnung übersetzen
- title = res.getString(getTitle(messageType));
-
- // WARNING -> JOptionPane.WARNING_MESSAGE
- int messageTypeIndex = getValue(messageType);
-
- // Button Set idetifizieren
- int optionTypeIndex = getValue(optionType);
-
- int result = CANCEL;
-
- if (exception != null) {
- String str = getStackTrace(exception);
- if (exception instanceof MessageException) {
- MessageException ex = (MessageException)exception;
- if (ex.getException() != null)
- str += "\n" + getStackTrace(ex.getException());
- }
- result = MessagesDialog.showDetailDialog(null, preparedMes,
- title, optionTypeIndex,
- messageTypeIndex, null,
- str);
- } else {
- if (optionTypeIndex == STYLE_NOBUTTON) {
- // Wird nicht mehr unterstützt
- //log.debug("UserMessage: STYLE_NOBUTTON wird nicht unterstützt");
- return result;
- } else {
- result = MessagesDialog.showConfirmDialog(null, preparedMes,
- title,
- optionTypeIndex,
- messageTypeIndex);
- }
- }
- return result;
- }
-
- /**
- * Öffnet das Dialogfenster mit der Übersetzung der per Suchschlüssel übergebenen Meldung
- * mit eingesetzten Parametern. Für die Übersetzung der Parameter trägt die aufrufende Stelle die Sorge.
- * Der Dialog ist modal zum Frame <code>frame</code>.
- * @param <UL>
- * <LI> textID - Suchschlüssel der Meldung im Meldungspool,
- * <LI> param - Array der in die Meldung einzusetztenden Parameter,
- * <LI> frame - das Fenster, zu dem der Dialog modal ist.
- * </UL>
- * @return <UL>
- * <LI> -1 wenn keine Rückgabe möglich ist oder Dialog NICHT MODAL ist.
- * <LI> ButtonDialog.YES = 2
- * <LI> ButtonDialog.NO = 4
- * <LI> ButtonDialog.CANCEL = 8
- * <LI> Wird das Dialog-Fenster ohne Buttonklick geschlossen (Kreuzchen oben rechts), so ist die Rückgabe gleich ButtonDialog.CANCEL.
- * </UL>
- */
- public static int show(String messageId, String[] parameterList,
- Exception anException, Frame parentFrame) {
- return display(messageId, parameterList, anException, parentFrame);
- }
-
- public static int show(String messageId, String[] parameterList,
- Exception anException) {
- return display(messageId, parameterList, anException, (Frame)null);
- }
-
- public static int show(String messageId, String[] parameterList,
- Frame parentFrame) {
- return display(messageId, parameterList, (Exception)null,
- parentFrame);
- }
-
- public static int show(String messageId, String[] parameterList) {
- return display(messageId, parameterList, (Exception)null,
- (Frame)null);
- }
-
- public static int show(String messageId, String parameter,
- Frame parentFrame) {
- return display(messageId, new String[] {
- parameter
- }, (Exception)null, parentFrame);
- }
-
- public static int show(String messageId, String parameter) {
- return display(messageId, new String[] {
- parameter
- }, (Exception)null, (Frame)null);
- }
-
- public static int show(String messageId, Frame parentFrame) {
- return display(messageId, (String[])null, (Exception)null,
- parentFrame);
- }
-
- public static int show(String messageId) {
- return display(messageId, (String[])null, (Exception)null,
- (Frame)null);
- }
-
- public static int show(String messageId, Exception anException,
- Frame aFrame) {
- return display(messageId, (String[])null, anException, aFrame);
- }
-
- public static int show(String messageId, Exception anException) {
- return display(messageId, (String[])null, anException, (Frame)null);
- }
-
- public static int show(Exception anException, Frame aFrame) {
- if (anException instanceof MessageException) {
- MessageException ex = (MessageException)anException;
- return show(ex.getMessageId(), ex.getParameterList(), ex, aFrame);
- } else
- return show("UNHANDLED_EXCEPTION", (String[])null, anException,
- aFrame);
- }
-
- }
|