KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > viewer > UserMessage


1 /*
2  * $Id: UserMessage.java,v 1.6.2.1 2003/02/25 15:25:16 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

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 /**
59  * Klasse <code>UserMessage</code> ist ein utility zum Abfragen oder zum Informieren des Benutzers.<br>
60  * Eine Meldung besteht aus dem Identifikator (Suchschlüssel im Meldungspool), einem Dialogtitel, einem Buttonset und
61  * dem Meldungstext mit eventuellen Platzhaltern für die Parameter.
62  *
63  * @author S. Gorkhover
64  * @version 18.03.1999
65  *
66  * @changed 23.04.99 Juergen.Verwohlt@jCatalog.com
67  * @subject Weitere Ausgabemethoden: show(String, String) und show(String,String,Frame)
68  *
69  * @changed 28.05.99 Juergen.Verwohlt@jCatalog.com
70  * @subject MessageException unterstützen
71  *
72  * @changed 09.06.99 Juergen.Verwohlt@jCatalog.com
73  * @subject Neue Klasse MessagesException zur Anzeige verwenden
74  *
75  * @changed 10.12.00 gears@apache.org
76  * @subject Unicode
77  */

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     // Zulässige Werte für Dialogart:
94

95     /**
96      * Möglicher Wert des Meldungstypes. Fenster-Title "Info" (de).
97      */

98     private static final int INFO = JOptionPane.PLAIN_MESSAGE;
99
100     /**
101      * Möglicher Wert des Meldungstypes. Fenster-Title "Warnung" (de).
102      */

103     private static final int WARNING = JOptionPane.WARNING_MESSAGE;
104
105     /**
106      * Möglicher Wert des Meldungstypes. Fenster-Title "Frage" (de).
107      */

108     private static final int QUESTION = JOptionPane.QUESTION_MESSAGE;
109
110     /**
111      * Möglicher Wert des Meldungstypes. Fenster-Title "Fehler" (de).
112      */

113     private static final int ERROR = JOptionPane.ERROR_MESSAGE;
114
115     /**
116      * Möglicher Wert des Meldungstypes. Fenster-Title "Systemfehler" (de).
117      */

118     private static final int SYS_ERROR = JOptionPane.ERROR_MESSAGE;
119
120
121
122     /*
123      * Style Constanten orientieren sich auf die
124      * Constanten der ButtonDialog-Klasse und legen das Dialog-ButtonSet fest.
125      */

126
127     /**
128      * Wert für Setzten keines Buttons
129      */

130     public static final int STYLE_NOBUTTON = -2;
131
132     /**
133      * Wert für Setzten von nur Ja-Button.
134      */

135     public static final int STYLE_Y = JOptionPane.DEFAULT_OPTION;
136     // ButtonDialog.YES; // = 1
137

138     /**
139      * Wert für Setzten von Ja- und Nein-Buttons.
140      */

141     public static final int STYLE_Y_N = JOptionPane.YES_NO_OPTION;
142     // ButtonDialog.YES_NO; // = 2;
143

144     /**
145      * Wert für Setzten von Ja-, Nein-und Abbruch Buttons.
146      */

147     public static final int STYLE_Y_N_C = JOptionPane.YES_NO_CANCEL_OPTION;
148     // ButtonDialog.YES_NO_CANCEL; // = 3;
149

150
151     // Platzhalter für Parameter in der Properties-Datei
152
private static final String JavaDoc PARAMETER_TAG = "&&&";
153
154
155     /**
156      * Benutzer-Antwort-Constanten orientieren sich auf die
157      * Constanten der ButtonDialog-Klasse
158      */

159
160     /**
161      * Möglicher Rückgabewert @see ButtonDialog
162      */

163     public static final int YES = JOptionPane.YES_OPTION;
164     // ButtonDialog.YES; // = 2
165

166     /**
167      * Möglicher Rückgabewert @see ButtonDialog
168      */

169     public static final int NO = JOptionPane.NO_OPTION;
170     // ButtonDialog.NO; // = 4;
171

172     /**
173      * Möglicher Rückgabewert @see ButtonDialog
174      */

175     public static final int CANCEL = JOptionPane.CANCEL_OPTION;
176     // ButtonDialog.CANCEL; // = 8;
177

178
179     // Default-Values
180
private static int buttonType = STYLE_Y;
181     private static int iconType = WARNING;
182     private static String JavaDoc currentIconName = "";
183     private static String JavaDoc actMessId = null;
184     // private static MessagesDialog nobuttonDialog = null;
185

186
187     /**
188      * Ersetzt die eventuellen Platzhalter durch die übergebenen Parameter
189      */

190     static String JavaDoc prepareMessage(String JavaDoc rawText, String JavaDoc[] par) {
191         MessageHandler.logln("prepareMessage(): " + rawText + ", parameter: "
192                              + par);
193         int index = rawText.indexOf(PARAMETER_TAG);
194         String JavaDoc 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 JavaDoc 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     /**
232      * Gibt den Title fürs Dialogfenster.
233      * Dieser wird durch die Dialogart festgelegt
234      * (erster Teil in der MeldungsDefinition in der Properties-Datei).
235      */

236     static String JavaDoc getTitle(String JavaDoc strVal) {
237         String JavaDoc 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     /**
264      * Liefert den Wert der über den Nemen übergebenen Klassenvariablen.
265      */

266     static int getValue(String JavaDoc 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 JavaDoc getStackTrace(Exception JavaDoc exception) {
291
292         if (exception == null)
293             return "null";
294
295         String JavaDoc 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 JavaDoc textID, String JavaDoc[] param,
306                                Exception JavaDoc exception, Frame frame) {
307
308         String JavaDoc translatedMes = "";
309         String JavaDoc preparedMes = "";
310         String JavaDoc messageType = "";
311         String JavaDoc optionType = "";
312         String JavaDoc 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 JavaDoc ex) {
332                 MessageHandler.logln("FALSCHES FORMAT: MESSAGE: " + textID);
333             }
334         } else { // Message not found
335
MessageHandler.logln("UserMessage: textID '" + textID
336                                  + "' not found. Return "
337                                  + "value 'CANCEL' = " + CANCEL);
338
339             // return CANCEL;
340

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         // Exception exception = getException(param);
349

350         // WARNING -> nach Warnung übersetzen
351
title = res.getString(getTitle(messageType));
352
353         // WARNING -> JOptionPane.WARNING_MESSAGE
354
int messageTypeIndex = getValue(messageType);
355
356         // Button Set idetifizieren
357
int optionTypeIndex = getValue(optionType);
358
359         int result = CANCEL;
360
361         if (exception != null) {
362             String JavaDoc 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                 // Wird nicht mehr unterstützt
375
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     /**
388      * Öffnet das Dialogfenster mit der Übersetzung der per Suchschlüssel übergebenen Meldung
389      * mit eingesetzten Parametern. Für die Übersetzung der Parameter trägt die aufrufende Stelle die Sorge.
390      * Der Dialog ist modal zum Frame <code>frame</code>.
391      * @param <UL>
392      * <LI> textID - Suchschlüssel der Meldung im Meldungspool,
393      * <LI> param - Array der in die Meldung einzusetztenden Parameter,
394      * <LI> frame - das Fenster, zu dem der Dialog modal ist.
395      * </UL>
396      * @return <UL>
397      * <LI> -1 wenn keine Rückgabe möglich ist oder Dialog NICHT MODAL ist.
398      * <LI> ButtonDialog.YES = 2
399      * <LI> ButtonDialog.NO = 4
400      * <LI> ButtonDialog.CANCEL = 8
401      * <LI> Wird das Dialog-Fenster ohne Buttonklick geschlossen (Kreuzchen oben rechts), so ist die Rückgabe gleich ButtonDialog.CANCEL.
402      * </UL>
403      */

404     public static int show(String JavaDoc messageId, String JavaDoc[] parameterList,
405                            Exception JavaDoc anException, Frame parentFrame) {
406         return display(messageId, parameterList, anException, parentFrame);
407     }
408
409     public static int show(String JavaDoc messageId, String JavaDoc[] parameterList,
410                            Exception JavaDoc anException) {
411         return display(messageId, parameterList, anException, (Frame)null);
412     }
413
414     public static int show(String JavaDoc messageId, String JavaDoc[] parameterList,
415                            Frame parentFrame) {
416         return display(messageId, parameterList, (Exception JavaDoc)null,
417                        parentFrame);
418     }
419
420     public static int show(String JavaDoc messageId, String JavaDoc[] parameterList) {
421         return display(messageId, parameterList, (Exception JavaDoc)null,
422                        (Frame)null);
423     }
424
425     public static int show(String JavaDoc messageId, String JavaDoc parameter,
426                            Frame parentFrame) {
427         return display(messageId, new String JavaDoc[] {
428             parameter
429         }, (Exception JavaDoc)null, parentFrame);
430     }
431
432     public static int show(String JavaDoc messageId, String JavaDoc parameter) {
433         return display(messageId, new String JavaDoc[] {
434             parameter
435         }, (Exception JavaDoc)null, (Frame)null);
436     }
437
438     public static int show(String JavaDoc messageId, Frame parentFrame) {
439         return display(messageId, (String JavaDoc[])null, (Exception JavaDoc)null,
440                        parentFrame);
441     }
442
443     public static int show(String JavaDoc messageId) {
444         return display(messageId, (String JavaDoc[])null, (Exception JavaDoc)null,
445                        (Frame)null);
446     }
447
448     public static int show(String JavaDoc messageId, Exception JavaDoc anException,
449                            Frame aFrame) {
450         return display(messageId, (String JavaDoc[])null, anException, aFrame);
451     }
452
453     public static int show(String JavaDoc messageId, Exception JavaDoc anException) {
454         return display(messageId, (String JavaDoc[])null, anException, (Frame)null);
455     }
456
457     public static int show(Exception JavaDoc 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 JavaDoc[])null, anException,
463                         aFrame);
464     }
465
466 }
467
468
Popular Tags