1 package com.explosion.utilities.dialog;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Dimension;
24 import java.awt.Font;
25 import java.awt.GridBagConstraints;
26 import java.awt.Image;
27 import java.awt.Insets;
28 import java.awt.Toolkit;
29
30 import javax.swing.BorderFactory;
31 import javax.swing.ImageIcon;
32 import javax.swing.JButton;
33 import javax.swing.JDialog;
34 import javax.swing.JLabel;
35 import javax.swing.JPanel;
36 import javax.swing.JScrollPane;
37 import javax.swing.JTextArea;
38
39 import org.apache.log4j.LogManager;
40 import org.apache.log4j.Logger;
41
42 import com.explosion.utilities.FileSystemUtils;
43 import com.explosion.utilities.GeneralConstants;
44 import com.explosion.utilities.exception.EnhancedException;
45
46 /***
47 * @author stephen cowx
48 */
49 public class GenericExceptionDisplayDialog extends JDialog
50 {
51
52 private static Logger log = LogManager.getLogger(GenericExceptionDisplayDialog.class);
53
54 private JTextArea detailsTextArea;
55
56 private JScrollPane detailsScrollPane;
57
58 private JScrollPane messageScrollPane;
59
60 private JPanel buttonPanel;
61
62 private JButton okButton;
63
64 private JPanel descriptionPanel;
65
66 private JTextArea shortMessageTextArea;
67
68 private JPanel detailsPanel;
69
70 private JButton detailsButton;
71
72 private JPanel iconPanel;
73
74 private JPanel mainPanel;
75
76 private JLabel iconLabel;
77
78 private String exceptionImageLocation;
79
80 private Font font = GeneralConstants.DEFAULT_WINDOW_FONT;
81
82 private Throwable exceptionToReport;
83
84 private String messageFromException;
85
86 private boolean isDisplayAdvanced = false;
87
88 /*** Creates new form GenericExceptionDisplayDialog */
89 public GenericExceptionDisplayDialog(java.awt.Frame parent, boolean modal, Throwable exceptionToReport, String messageFromException, String exceptionImageLocation, Font font)
90 {
91 super(parent, modal);
92 try
93 {
94 this.exceptionToReport = exceptionToReport;
95 this.messageFromException = messageFromException;
96 this.exceptionImageLocation = exceptionImageLocation;
97 if (font != null) this.font = font;
98 init();
99 } catch (Exception e)
100 {
101 reportDoubleException(exceptionToReport, e);
102 }
103 }
104
105 private void init() throws Exception
106 {
107 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
108 this.setSize(600, 157);
109 this.setLocation((d.width - this.getSize().width) / 2, (d.height - this.getSize().height) / 2);
110 this.setTitle("Error !");
111
112 mainPanel = new JPanel();
113
114 detailsTextArea = new JTextArea();
115 detailsTextArea.setFont(font);
116 detailsTextArea.setText(EnhancedException.getStackTrace(exceptionToReport));
117 detailsTextArea.setCaretPosition(0);
118 detailsTextArea.setEditable(false);
119 detailsScrollPane = new JScrollPane();
120 detailsScrollPane.setViewportView(detailsTextArea);
121 detailsPanel = new JPanel();
122 detailsPanel.setLayout(new java.awt.BorderLayout());
123 detailsPanel.add(detailsScrollPane, java.awt.BorderLayout.CENTER);
124
125 Image exceptionIcon = FileSystemUtils.loadImage(exceptionImageLocation, this);
126 iconPanel = new JPanel();
127 iconLabel = new JLabel();
128 iconLabel.setIcon(new ImageIcon(exceptionIcon));
129 iconPanel.add(iconLabel);
130
131 shortMessageTextArea = new JTextArea();
132 shortMessageTextArea.setFont(font);
133 shortMessageTextArea.setText(messageFromException);
134 shortMessageTextArea.setBackground(iconPanel.getBackground());
135 shortMessageTextArea.setCaretPosition(0);
136 shortMessageTextArea.setCaretColor(iconPanel.getBackground());
137 shortMessageTextArea.setLineWrap(true);
138 shortMessageTextArea.setEditable(false);
139 shortMessageTextArea.setWrapStyleWord(true);
140 messageScrollPane = new JScrollPane();
141 messageScrollPane.getViewport().add(shortMessageTextArea, null);
142 messageScrollPane.setBorder(BorderFactory.createEmptyBorder());
143
144 descriptionPanel = new JPanel();
145 descriptionPanel.setLayout(new java.awt.GridBagLayout());
146 descriptionPanel.setBorder(BorderFactory.createEtchedBorder());
147 descriptionPanel.add(iconPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(12, 12, 12, 12), 0, 0));
148 descriptionPanel.add(messageScrollPane, new GridBagConstraints(1, 0, 1, 1, 0.1, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
149
150 mainPanel.setLayout(new java.awt.GridBagLayout());
151 mainPanel.add(descriptionPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(4, 4, 4, 4), 0, 0));
152
153 buttonPanel = new JPanel();
154 okButton = new JButton("Ok");
155 detailsButton = new JButton("Show Details");
156 buttonPanel.add(okButton);
157 buttonPanel.add(detailsButton);
158
159 getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
160 getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);
161 validate();
162
163 addWindowListener(new java.awt.event.WindowAdapter()
164 {
165
166 public void windowClosing(java.awt.event.WindowEvent evt)
167 {
168 closeDialog(evt);
169 }
170 });
171 okButton.addActionListener(new java.awt.event.ActionListener()
172 {
173
174 public void actionPerformed(java.awt.event.ActionEvent evt)
175 {
176 okButtonActionPerformed(evt);
177 }
178 });
179 detailsButton.addActionListener(new java.awt.event.ActionListener()
180 {
181
182 public void actionPerformed(java.awt.event.ActionEvent evt)
183 {
184 detailsButtonActionPerformed(evt);
185 }
186 });
187 Toolkit.getDefaultToolkit().beep();
188 setVisible(true);
189 }
190
191 private void detailsButtonActionPerformed(java.awt.event.ActionEvent evt)
192 {
193 try
194 {
195 if (isDisplayAdvanced)
196 {
197 this.setSize(this.getWidth(), 157);
198 mainPanel.remove(descriptionPanel);
199 mainPanel.remove(detailsPanel);
200 mainPanel.add(descriptionPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(4, 4, 4, 4), 0, 0));
201 detailsButton.setText("Show Details");
202 isDisplayAdvanced = false;
203 validate();
204 repaint();
205 } else
206 {
207 this.setSize(this.getWidth(), 350);
208 mainPanel.remove(descriptionPanel);
209 mainPanel.remove(detailsPanel);
210 mainPanel.add(descriptionPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(4, 4, 4, 4), 0, 0));
211 mainPanel.add(detailsPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.9, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(4, 4, 4, 4), 0, 0));
212 detailsButton.setText("Hide Details");
213
214 isDisplayAdvanced = true;
215 validate();
216 repaint();
217 }
218 } catch (Exception e)
219 {
220 reportDoubleException(exceptionToReport, e);
221 }
222 }
223
224 private void okButtonActionPerformed(java.awt.event.ActionEvent evt)
225 {
226 closeDialog(null);
227 }
228
229 /*** Closes the dialog */
230 private void closeDialog(java.awt.event.WindowEvent evt)
231 {
232 setVisible(false);
233 dispose();
234 }
235
236 public boolean isDisplayAdvanced()
237 {
238 return isDisplayAdvanced;
239 }
240
241 private void reportDoubleException(Throwable exception, Exception e)
242 {
243 try
244 {
245 log.debug("Experienced an exception while trying to initialise the exception display dialog.");
246 log.debug("The exception being reported originally was:");
247 if (exception != null)
248 exception.printStackTrace();
249 else
250 log.debug(" Unable to determine cause of origianl exception.");
251 log.debug("New exception just experienced was:");
252 e.printStackTrace();
253 } catch (Exception e1)
254 {
255 log.debug("Experienced another exception while trying to report an exception which occurred while trying to initialise the exception display dialog.");
256 log.debug("If the program is not completely dead I am be suprised. Here's as much information as is available.");
257 try
258 {
259 exception.printStackTrace();
260 } catch (Exception e2)
261 {}
262 try
263 {
264 e.printStackTrace();
265 } catch (Exception e2)
266 {}
267 try
268 {
269 e1.printStackTrace();
270 } catch (Exception e2)
271 {}
272
273 }
274 }
275
276 }