KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > dialog > ErrorDialog


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 package org.columba.core.gui.dialog;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Container JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Frame JavaDoc;
26 import java.awt.GridLayout JavaDoc;
27 import java.awt.Window JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.net.MalformedURLException JavaDoc;
33 import java.net.URL JavaDoc;
34
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.JDialog JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JScrollPane JavaDoc;
40 import javax.swing.JTextArea JavaDoc;
41 import javax.swing.JToggleButton JavaDoc;
42 import javax.swing.SwingConstants JavaDoc;
43 import javax.swing.UIManager JavaDoc;
44
45 import org.columba.core.desktop.ColumbaDesktop;
46 import org.columba.core.gui.base.ButtonWithMnemonic;
47 import org.columba.core.gui.base.MultiLineLabel;
48 import org.columba.core.gui.base.SingleSideEtchedBorder;
49 import org.columba.core.resourceloader.GlobalResourceLoader;
50 import org.frapuccino.swing.ActiveWindowTracker;
51
52 import com.jgoodies.forms.layout.CellConstraints;
53 import com.jgoodies.forms.layout.FormLayout;
54
55 /**
56  * Dialog showing an error message and the exception's stack trace on request.
57  * <p>
58  * TODO (@author fdietz): I've currently replaced MultiLineLabel with JLabel,
59  * because it totally destroys the layout. Somehow the MultiLineLabel doesn't
60  * respect the JDialog size.
61  *
62  * @author fdietz
63  */

64
65 public class ErrorDialog extends JDialog JavaDoc implements ActionListener JavaDoc {
66     private static final String JavaDoc URL_FORUM = "http://columba.sourceforge.net/phpBB2/viewforum.php?f=15";
67
68     public static final String JavaDoc CMD_CLOSE = "CLOSE";
69
70     public static final String JavaDoc CMD_REPORT_BUG = "REPORT_BUG";
71
72     private static final String JavaDoc RESOURCE_BUNDLE_PATH = "org.columba.core.i18n.dialog";
73
74     private boolean bool = false;
75
76     private JLabel JavaDoc imageLabel;
77
78     private JTextArea JavaDoc stacktraceTextArea;
79
80     private ButtonWithMnemonic closeButton;
81
82     private ButtonWithMnemonic reportBugButton;
83
84     private MultiLineLabel messageMultiLineLabel;
85
86     private JLabel JavaDoc label;
87
88     private String JavaDoc message;
89
90     private JToggleButton JavaDoc detailsButton;
91
92     private Throwable JavaDoc ex;
93
94     public static ErrorDialog createDialog(String JavaDoc message, Throwable JavaDoc ex) {
95         Window JavaDoc w = ActiveWindowTracker.findActiveWindow();
96         if (w instanceof Frame JavaDoc)
97             return new ErrorDialog((Frame JavaDoc) w, message, ex);
98         else if (w instanceof Dialog JavaDoc)
99             return new ErrorDialog((Dialog JavaDoc) w, message, ex);
100         else
101             return new ErrorDialog(message, ex);
102     }
103
104     private ErrorDialog(String JavaDoc message, Throwable JavaDoc ex) {
105         super();
106
107         init(message, ex);
108     }
109
110     private ErrorDialog(Dialog JavaDoc frame, String JavaDoc message, Throwable JavaDoc ex) {
111         super(frame, true);
112
113         init(message, ex);
114     }
115
116     private ErrorDialog(Frame JavaDoc frame, String JavaDoc message, Throwable JavaDoc ex) {
117         super(frame, true);
118
119         init(message, ex);
120     }
121
122     private void init(String JavaDoc message, Throwable JavaDoc ex) {
123         this.message = message;
124         this.ex = ex;
125
126         setTitle(GlobalResourceLoader.getString("org.columba.core.i18n.dialog",
127                 "error", "error_title"));
128
129         initComponents();
130         layoutComponents();
131         pack();
132
133         setLocationRelativeTo(null);
134         setVisible(true);
135     }
136
137     private JPanel JavaDoc createCenterPanel(boolean showDetails) {
138         FormLayout layout = new FormLayout(
139                 "default, 3dlu, fill:default:grow, 3dlu",
140                 "default, 8dlu, default 2dlu, fill:default:grow");
141
142         JPanel JavaDoc centerPanel = new JPanel JavaDoc(layout);
143         centerPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
144
145         CellConstraints cc = new CellConstraints();
146         centerPanel.add(imageLabel, cc.xy(1, 1));
147
148         centerPanel.add(messageMultiLineLabel, cc.xywh(3, 1, 1, 1));
149         // centerPanel.add(label, cc.xywh(3, 1, 1, 1));
150

151         centerPanel.add(detailsButton, cc.xywh(1, 3, 1, 1));
152         if (showDetails) {
153             centerPanel.add(new JScrollPane JavaDoc(stacktraceTextArea), cc.xywh(1, 5,
154                     3, 1));
155
156         }
157
158         return centerPanel;
159     }
160
161     private void initComponents() {
162         imageLabel = new JLabel JavaDoc(UIManager.getIcon("OptionPane.errorIcon"),
163                 SwingConstants.LEFT);
164
165         messageMultiLineLabel = new MultiLineLabel(message);
166
167         messageMultiLineLabel.setFont(messageMultiLineLabel.getFont()
168                 .deriveFont(Font.BOLD));
169         label = new JLabel JavaDoc(message);
170         label.setFont(label.getFont().deriveFont(Font.BOLD));
171
172         stacktraceTextArea = new JTextArea JavaDoc();
173
174         if ( ex != null ) {
175             StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
176             ex.printStackTrace(new PrintWriter JavaDoc(stringWriter));
177     
178             stacktraceTextArea.append(stringWriter.toString());
179             stacktraceTextArea.setEditable(false);
180         }
181         
182         // TODO (@author fdietz): i18n
183
detailsButton = new JToggleButton JavaDoc("Details >>");
184         detailsButton.setSelected(false);
185         detailsButton.setActionCommand("DETAILS");
186         detailsButton.addActionListener(this);
187         
188         if ( ex == null ) {
189             detailsButton.setEnabled(false);
190         }
191         
192         closeButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(
193                 "global", "global", "close"));
194         closeButton.setActionCommand(CMD_CLOSE);
195         closeButton.addActionListener(this);
196
197         reportBugButton = new ButtonWithMnemonic(GlobalResourceLoader
198                 .getString(RESOURCE_BUNDLE_PATH, "exception", "report_bug"));
199         reportBugButton.setActionCommand(CMD_REPORT_BUG);
200         reportBugButton.addActionListener(this);
201     }
202
203     private void layoutComponents() {
204         if (getContentPane().getComponentCount() > 0) {
205             getContentPane().removeAll();
206         }
207
208         getRootPane().setDefaultButton(closeButton);
209
210         JPanel JavaDoc bottomPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(0, 0));
211         bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP));
212
213         JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new GridLayout JavaDoc(1, 3, 6, 0));
214         buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
215
216         // buttonPanel.add(reportBugButton);
217
buttonPanel.add(closeButton);
218
219         bottomPanel.add(buttonPanel, BorderLayout.EAST);
220
221         Container JavaDoc c = getContentPane();
222         c.setLayout(new BorderLayout JavaDoc());
223
224         c.add(createCenterPanel(detailsButton.isSelected()),
225                 BorderLayout.CENTER);
226         c.add(bottomPanel, BorderLayout.SOUTH);
227
228         /*
229          * FormLayout layout = new FormLayout("default", "default, default");
230          * IContainer c = getContentPane(); c.setLayout(layout);
231          *
232          * CellConstraints cc = new CellConstraints();
233          * c.add(createCenterPanel(detailsButton.isSelected()), cc.xy(1, 1));
234          * c.add(bottomPanel, cc.xy(1, 2));
235          */

236
237     }
238
239     public boolean success() {
240         return bool;
241     }
242
243     public void actionPerformed(ActionEvent JavaDoc e) {
244         String JavaDoc command = e.getActionCommand();
245         if (CMD_CLOSE.equals(command)) {
246             bool = true;
247             dispose();
248         } else if (CMD_REPORT_BUG.equals(command)) {
249             bool = false;
250
251             try {
252                 ColumbaDesktop.getInstance().browse(new URL JavaDoc(URL_FORUM));
253             } catch (MalformedURLException JavaDoc e1) {
254             }
255
256         } else if (command.equals("DETAILS")) {
257             layoutComponents();
258             pack();
259             setLocationRelativeTo(null);
260         }
261     }
262 }
Popular Tags