KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MessagesDialog.java,v 1.3.2.1 2003/02/25 15:25:15 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.io.*;
54 import java.awt.*;
55 import javax.swing.*;
56
57 /**
58  * Die Klasse <code>MessagesDialog</code> dient der Anzeige von Meldungen.
59  * Die Klasse erweitert <code>JOptionPane</code> um die Möglichkeit, auf Knopfdruck
60  * eine Detailanzeige einzublenden, in der z.B. bei Fehlern der StackTrace ausgegeben
61  * werden kann.
62  *
63  * @author Juergen.Verwohlt@jCatalog.com
64  * @version 1.0 09.06.99
65  */

66 public class MessagesDialog extends JOptionPane {
67
68     static Translator res;
69
70     public static void setTranslator(Translator aRes) {
71         res = aRes;
72         iniConstants();
73     }
74
75
76     static String JavaDoc DETAIL_OPTION;
77     static String JavaDoc YES_OPTION;
78     static String JavaDoc NO_OPTION;
79     static String JavaDoc CANCEL_OPTION;
80     static String JavaDoc OK_OPTION;
81
82     static String JavaDoc[] defaultDetailOption;
83     static String JavaDoc[] yesNoDetailOption;
84     static String JavaDoc[] yesNoCancelDetailOption;
85     static String JavaDoc[] okCancelDetailOption;
86
87     static String JavaDoc[] defaultOption;
88     static String JavaDoc[] yesNoOption;
89     static String JavaDoc[] yesNoCancelOption;
90     static String JavaDoc[] okCancelOption;
91
92
93
94     private static void iniConstants() {
95         DETAIL_OPTION = res.getString("Details");
96         YES_OPTION = res.getString("Yes");
97         NO_OPTION = res.getString("No");
98         CANCEL_OPTION = res.getString("Cancel");
99         OK_OPTION = res.getString("Ok");
100
101         defaultDetailOption = new String JavaDoc[] {
102             OK_OPTION, DETAIL_OPTION
103         };
104         yesNoDetailOption = new String JavaDoc[] {
105             YES_OPTION, NO_OPTION, DETAIL_OPTION
106         };
107         yesNoCancelDetailOption = new String JavaDoc[] {
108             YES_OPTION, NO_OPTION, CANCEL_OPTION, DETAIL_OPTION
109         };
110         okCancelDetailOption = new String JavaDoc[] {
111             OK_OPTION, CANCEL_OPTION, DETAIL_OPTION
112         };
113
114         defaultOption = new String JavaDoc[] {
115             OK_OPTION
116         };
117         yesNoOption = new String JavaDoc[] {
118             YES_OPTION, NO_OPTION
119         };
120         yesNoCancelOption = new String JavaDoc[] {
121             YES_OPTION, NO_OPTION, CANCEL_OPTION
122         };
123         okCancelOption = new String JavaDoc[] {
124             OK_OPTION, CANCEL_OPTION
125         };
126     }
127
128
129     protected String JavaDoc detailInformation = null;
130     protected JDialog dialog = null;
131     protected boolean showsDetails = false;
132
133     // MessagesDialog.showConfirmDialog(null,preparedMes,title,
134
// optionTypeIndex,messageTypeIndex);
135

136     public MessagesDialog(Object JavaDoc message, int messageType, int optionType,
137                           Icon icon, Object JavaDoc[] options, Object JavaDoc initialValue) {
138         super(message, messageType, optionType, icon, options, initialValue);
139         setMinimumSize(new Dimension(240, 96));
140     }
141
142     public static int showConfirmDialog(Component parentComponent,
143                                         Object JavaDoc message, String JavaDoc title,
144                                         int optionType, int messageType) {
145         Object JavaDoc[] options;
146
147         switch (optionType) {
148         case JOptionPane.YES_NO_OPTION:
149             options = yesNoOption;
150             break;
151         case JOptionPane.YES_NO_CANCEL_OPTION:
152             options = yesNoCancelOption;
153             break;
154         case JOptionPane.OK_CANCEL_OPTION:
155             options = okCancelOption;
156             break;
157         default:
158             options = defaultOption;
159         }
160
161         MessagesDialog pane = new MessagesDialog(message, messageType,
162                                                  JOptionPane.DEFAULT_OPTION,
163                                                  null, options, options[0]);
164
165         pane.setInitialValue(options[0]);
166
167         JDialog dialog = pane.createDialog(parentComponent, title);
168
169         pane.setDialog(dialog);
170         pane.selectInitialValue();
171
172         dialog.show();
173
174         Object JavaDoc selectedValue = pane.getValue();
175
176         if (selectedValue == null)
177             return CLOSED_OPTION;
178
179         if (selectedValue.equals(OK_OPTION))
180             return JOptionPane.OK_OPTION;
181         if (selectedValue.equals(CANCEL_OPTION))
182             return JOptionPane.CANCEL_OPTION;
183         if (selectedValue.equals(YES_OPTION))
184             return JOptionPane.YES_OPTION;
185         if (selectedValue.equals(NO_OPTION))
186             return JOptionPane.NO_OPTION;
187
188         return CLOSED_OPTION;
189     }
190
191     /**
192      * Öffnet ein Dialogfenster, bei dem zusätzlich zu den spez. Buttons noch ein
193      * 'Detail'-Button erscheint. Wird dieser Knopf vom Benutzer betätigt, erscheint
194      * die übergebene Detailinformation in einem scrollbaren Bereich des Dialogs.
195      */

196     public static int showDetailDialog(Component parentComponent,
197                                        Object JavaDoc message, String JavaDoc title,
198                                        int optionType, int messageType,
199                                        Icon icon,
200                                        String JavaDoc newDetailInformation) {
201         Object JavaDoc[] options;
202
203         switch (optionType) {
204         case JOptionPane.YES_NO_OPTION:
205             options = yesNoDetailOption;
206             break;
207         case JOptionPane.YES_NO_CANCEL_OPTION:
208             options = yesNoCancelDetailOption;
209             break;
210         case JOptionPane.OK_CANCEL_OPTION:
211             options = okCancelDetailOption;
212             break;
213         default:
214             options = defaultDetailOption;
215         }
216
217         MessagesDialog pane = new MessagesDialog(message, messageType,
218                                                  JOptionPane.DEFAULT_OPTION,
219                                                  icon, options, options[0]);
220
221         pane.setDetailInformation(newDetailInformation);
222         pane.setInitialValue(options[0]);
223
224         JDialog dialog = pane.createDialog(parentComponent, title);
225
226         pane.setDialog(dialog);
227         pane.selectInitialValue();
228
229         dialog.show();
230
231         Object JavaDoc selectedValue = pane.getValue();
232
233         if (selectedValue == null)
234             return CLOSED_OPTION;
235
236         if (((String JavaDoc)selectedValue).equals(DETAIL_OPTION))
237             return CLOSED_OPTION;
238
239         if (selectedValue.equals(OK_OPTION))
240             return JOptionPane.OK_OPTION;
241         if (selectedValue.equals(CANCEL_OPTION))
242             return JOptionPane.CANCEL_OPTION;
243         if (selectedValue.equals(YES_OPTION))
244             return JOptionPane.YES_OPTION;
245         if (selectedValue.equals(NO_OPTION))
246             return JOptionPane.NO_OPTION;
247
248         return CLOSED_OPTION;
249     }
250
251     /**
252      * Die Methode fügt in den übergebenen Dialog eine scrollbare Textkomponente ein,
253      * in der die Detailinformation angezeigt wird.
254      *
255      * @param JDialog dialog Der Dialog, in den die Textkomponente eingefügt werden soll
256      */

257     protected void displayDetails(JDialog dialog) {
258         if (getDetailInformation() != null && dialog != null
259                 && showsDetails == false) {
260             showsDetails = true;
261             JScrollPane aScrollPane = new JScrollPane();
262             JTextArea aTextArea = new JTextArea();
263             StringWriter aStringWriter = new StringWriter();
264
265             aTextArea.setText(getDetailInformation());
266             aTextArea.setEditable(false);
267
268             aScrollPane.getViewport().add(aTextArea, null);
269             dialog.getContentPane().add(aScrollPane, BorderLayout.SOUTH);
270             aScrollPane.setPreferredSize(new Dimension(320, 240));
271             dialog.pack();
272         }
273     }
274
275     // Zugriff
276

277     public void setValue(Object JavaDoc aValue) {
278         if (aValue != null && DETAIL_OPTION.equals(aValue))
279             displayDetails(getDialog());
280         else
281             super.setValue(aValue);
282     }
283
284     public String JavaDoc getDetailInformation() {
285         return detailInformation;
286     }
287
288     public void setDetailInformation(String JavaDoc aValue) {
289         detailInformation = aValue;
290     }
291
292     public JDialog getDialog() {
293         return dialog;
294     }
295
296     public void setDialog(JDialog aValue) {
297         dialog = aValue;
298     }
299
300 }
301
Popular Tags