File Source: FatalErrorDialog.java
/*
P/P * Method: com.dmdirc.ui.FatalErrorDialog__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22 package com.dmdirc.ui;
23
24 import com.dmdirc.logger.ErrorListener;
25 import com.dmdirc.logger.ErrorManager;
26 import com.dmdirc.logger.ErrorReportStatus;
27 import com.dmdirc.logger.ProgramError;
28
29 import java.awt.BorderLayout;
30 import java.awt.Dialog;
31 import java.awt.Dimension;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.WindowAdapter;
35 import java.awt.event.WindowEvent;
36
37 import java.util.concurrent.Semaphore;
38 import javax.swing.BorderFactory;
39 import javax.swing.Box;
40 import javax.swing.BoxLayout;
41 import javax.swing.ImageIcon;
42 import javax.swing.JButton;
43 import javax.swing.JDialog;
44 import javax.swing.JLabel;
45 import javax.swing.JPanel;
46 import javax.swing.JScrollPane;
47 import javax.swing.JTextArea;
48 import javax.swing.JTextPane;
49 import javax.swing.SwingUtilities;
50 import javax.swing.SwingWorker;
51 import javax.swing.WindowConstants;
52 import javax.swing.text.DefaultStyledDocument;
53 import javax.swing.text.SimpleAttributeSet;
54 import javax.swing.text.StyleConstants;
55 import javax.swing.text.StyledDocument;
56
57 /**
58 * The fatal error dialog is used to inform the user that a fatal error has
59 * occured.
60 */
/*
P/P * Method: void com.dmdirc.ui.FatalErrorDialog(ProgramError, FatalErrorDialog$1)
*
* Postconditions:
* this.error == x0
* init'ed(this.error)
* init'ed(this.icon)
* init'ed(this.infoLabel)
* init'ed(this.messageLabel)
* init'ed(this.okButton)
* init'ed(this.scrollPane)
* init'ed(this.sendButton)
*/
61 public final class FatalErrorDialog extends JDialog implements ActionListener,
62 ErrorListener {
63
64 /**
65 * A version number for this class. It should be changed whenever the class
66 * structure is changed (or anything else that would prevent serialized
67 * objects being unserialized with the new class).
68 */
69 private static final long serialVersionUID = 3;
70 /** error. */
71 private final ProgramError error;
72 /** button. */
73 private JButton okButton;
74 /** button. */
75 private JButton sendButton;
76 /** info label. */
77 private JTextPane infoLabel;
78 /** message label. */
79 private JTextPane messageLabel;
80 /** Icon. */
81 private ImageIcon icon;
82 /** stack trace scroll pane. */
83 private JScrollPane scrollPane;
84
85 /**
86 * Creates a new fatal error dialog.
87 *
88 * @param error Error
89 */
90 private FatalErrorDialog(final ProgramError error) {
/*
P/P * Method: void com.dmdirc.ui.FatalErrorDialog(ProgramError)
*
* Presumptions:
* getErrorManager(...)@100 init'ed
* init'ed(java.awt.Dialog$ModalityType.TOOLKIT_MODAL)
*
* Postconditions:
* this.error == error
* init'ed(this.error)
* init'ed(this.icon)
* init'ed(this.infoLabel)
* init'ed(this.messageLabel)
* init'ed(this.okButton)
* init'ed(this.scrollPane)
* init'ed(this.sendButton)
*/
91 super(null, Dialog.ModalityType.TOOLKIT_MODAL);
92
93 setModal(true);
94
95 this.error = error;
96
97 initComponents();
98 layoutComponents();
99
100 ErrorManager.getErrorManager().addErrorListener(this);
101
102 setResizable(false);
103 CoreUIUtils.centreWindow(this);
104 }
105
106 /**
107 * Initialises the components for this dialog.
108 */
109 private void initComponents() {
/*
P/P * Method: void initComponents()
*/
110 final JTextArea stacktraceField = new JTextArea();
111
112 infoLabel = new JTextPane(new DefaultStyledDocument());
113 infoLabel.setOpaque(false);
114 infoLabel.setEditable(false);
115 infoLabel.setHighlighter(null);
116 messageLabel = new JTextPane(new DefaultStyledDocument());
117 messageLabel.setOpaque(false);
118 messageLabel.setEditable(false);
119 messageLabel.setHighlighter(null);
120 final SimpleAttributeSet sas = new SimpleAttributeSet();
121 StyleConstants.setAlignment(sas, StyleConstants.ALIGN_JUSTIFIED);
122
123 scrollPane = new JScrollPane();
124 okButton = new JButton();
125 sendButton = new JButton();
126
127 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
128 setTitle("DMDirc: Fatal Error");
129 setIconImage(IconManager.getIconManager().getImage("icon"));
130
131 infoLabel.setText("DMDirc has encountered a fatal error, and is " +
132 "not able to recover. \nThe application will now terminate.");
133 messageLabel.setText("Description: " + error.getMessage());
134 ((StyledDocument) infoLabel.getDocument()).setParagraphAttributes(0,
135 infoLabel.getText().length(), sas, false);
136 ((StyledDocument) messageLabel.getDocument()).setParagraphAttributes(0,
137 messageLabel.getText().length(), sas, false);
138
139 icon = new ImageIcon(IconManager.getIconManager().getImage("error"));
140
141 stacktraceField.setEditable(false);
142
143 final String[] trace = error.getTrace();
144 if (trace.length > 0) {
145 for (String line : trace) {
146 stacktraceField.append(line + "\n");
147 }
148 stacktraceField.setCaretPosition(0);
149 }
150
151 scrollPane.setViewportView(stacktraceField);
152
153 okButton.setText("OK");
154 sendButton.setText("Send");
155
156 final ErrorReportStatus status = error.getReportStatus();
157 okButton.setEnabled(status.isTerminal());
158 updateSendButtonText(status);
159
160 okButton.addActionListener(this);
161 sendButton.addActionListener(this);
162 }
163
164 /**
165 * lays the components out in the dialog.
166 */
167 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.icon)
* init'ed(this.infoLabel)
* init'ed(this.messageLabel)
* init'ed(this.okButton)
* init'ed(this.scrollPane)
* init'ed(this.sendButton)
*
* Presumptions:
* com.dmdirc.ui.FatalErrorDialog:getContentPane(...)@193 != null
*/
168 final JPanel panel = new JPanel();
169 final JPanel blurb = new JPanel();
170 final JPanel info = new JPanel();
171 final JPanel buttons = new JPanel();
172 blurb.setLayout(new BorderLayout(5, 5));
173 info.setLayout(new BorderLayout(5, 5));
174 buttons.setLayout(new BoxLayout(buttons, BoxLayout.LINE_AXIS));
175
176 blurb.add(new JLabel(icon), BorderLayout.LINE_START);
177 blurb.add(infoLabel, BorderLayout.CENTER);
178
179 info.add(messageLabel, BorderLayout.NORTH);
180 info.add(scrollPane, BorderLayout.CENTER);
181
182 buttons.add(Box.createHorizontalGlue());
183 buttons.add(sendButton);
184 buttons.add(Box.createHorizontalStrut(5));
185 buttons.add(okButton);
186
187 panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
188 panel.setLayout(new BorderLayout(5, 5));
189 panel.add(blurb, BorderLayout.NORTH);
190 panel.add(info, BorderLayout.CENTER);
191 panel.add(buttons, BorderLayout.SOUTH);
192
193 getContentPane().add(panel);
194
195 setSize(new Dimension(550, 260));
196 }
197
198 /**
199 * Exits the program. {@inheritDoc}
200 *
201 * @param actionEvent Action event.
202 */
203 @Override
204 public void actionPerformed(final ActionEvent actionEvent) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* actionEvent != null
* (soft) this.okButton != null
* (soft) this.sendButton != null
*/
205 if (actionEvent.getSource() == sendButton) {
206 sendButton.setText("Sending...");
207 okButton.setEnabled(false);
208 sendButton.setEnabled(false);
/*
P/P * Method: void com.dmdirc.ui.FatalErrorDialog$1(FatalErrorDialog)
*/
209 new SwingWorker() {
210
211 /** {@inheritDoc} */
212 @Override
213 protected Object doInBackground() throws Exception {
/*
P/P * Method: Object doInBackground()
*
* Preconditions:
* init'ed(this.error.reportStatus)
* this.error != null
* (soft) init'ed(com.dmdirc.ui.FatalErrorDialog$4__static_init.new int[](FatalErrorDialog$4__static_init#1)[...])
* (soft) init'ed(com.dmdirc.logger.ErrorManager__static_init.new ErrorManager(ErrorManager__static_init#1).reportThread)
*
* Postconditions:
* return_value == null
* this.error.reportStatus == One-of{old this.error.reportStatus, &com.dmdirc.logger.ErrorReportStatus__static_init.new ErrorReportStatus(ErrorReportStatus__static_init#5)}
* init'ed(this.error.reportStatus)
* com.dmdirc.logger.ErrorManager__static_init.new ErrorManager(ErrorManager__static_init#1).reportThread == One-of{old com.dmdirc.logger.ErrorManager__static_init.new ErrorManager(ErrorManager__static_init#1).reportThread, &new ErrorReportingThread(sendError#1)}
* init'ed(com.dmdirc.logger.ErrorManager__static_init.new ErrorManager(ErrorManager__static_init#1).reportThread)
* new ErrorReportingThread(sendError#1) num objects <= 1
* new ErrorReportingThread(sendError#1).queue == &com.dmdirc.logger.ErrorManager.new LinkedBlockingQueue(ErrorManager#1)
*/
214 ErrorManager.getErrorManager().sendError(error);
215 return null;
216 }
217
218 /** {@inheritDoc} */
219 @Override
220 protected void done() {
/*
P/P * Method: void done()
*/
221 super.done();
222 }
223 }.execute();
224 } else {
225 dispose();
226 }
227 }
228
229 /**
230 * Static method to instantiate and display the dialog.
231 *
232 * @param error Program error
233 */
234 public static void display(final ProgramError error) {
/*
P/P * Method: void display(ProgramError)
*/
235 SwingUtilities.invokeLater(new Runnable() {
236
237 /** {@inheritDoc} */
238 @Override
239 public void run() {
/*
P/P * Method: void run()
*/
240 final FatalErrorDialog me = new FatalErrorDialog(error);
241 me.setVisible(true);
242 }
243 });
244 }
245
246 /**
247 * Static method to instantiate and display the dialog, blocking until it
248 * is closed.
249 *
250 * @param error Program error
251 */
252 public static void displayBlocking(final ProgramError error) {
/*
P/P * Method: void displayBlocking(ProgramError)
*/
253 final Semaphore semaphore = new Semaphore(0);
/*
P/P * Method: void com.dmdirc.ui.FatalErrorDialog$3(ProgramError, Semaphore)
*
* Postconditions:
* this.val$error == Param_1
* init'ed(this.val$error)
* this.val$semaphore == Param_2
* init'ed(this.val$semaphore)
*/
254 SwingUtilities.invokeLater(new Runnable() {
255
256 /** {@inheritDoc} */
257 @Override
258 public void run() {
/*
P/P * Method: void run()
*/
259 final FatalErrorDialog me = new FatalErrorDialog(error);
/*
P/P * Method: void com.dmdirc.ui.FatalErrorDialog$3$1(FatalErrorDialog$3)
*/
260 me.addWindowListener(new WindowAdapter() {
261
262 @Override
263 public void windowClosed(final WindowEvent e) {
/*
P/P * Method: void windowClosed(WindowEvent)
*
* Preconditions:
* this.val$semaphore != null
*/
264 semaphore.release();
265 }
266 });
267 me.setVisible(true);
268 }
269 });
270 semaphore.acquireUninterruptibly();
271 }
272
273 private void updateSendButtonText(final ErrorReportStatus status) {
/*
P/P * Method: void updateSendButtonText(ErrorReportStatus)
*
* Preconditions:
* status != null
* this.sendButton != null
* (soft) init'ed(com.dmdirc.ui.FatalErrorDialog$4__static_init.new int[](FatalErrorDialog$4__static_init#1)[...])
*
* Presumptions:
* com.dmdirc.logger.ErrorReportStatus:ordinal(...)@274 in {0..5}
*
* Test Vectors:
* com.dmdirc.ui.FatalErrorDialog$4__static_init.new int[](FatalErrorDialog$4__static_init#1)[...]: {1}, {2}, {3}, {4}, {5}, {6}, {-231..0, 7..232-1}
*/
274 switch (status) {
275 case WAITING:
276 sendButton.setText("Send");
277 sendButton.setEnabled(true);
278 break;
279 case QUEUED:
280 sendButton.setText("Queued");
281 sendButton.setEnabled(false);
282 break;
283 case SENDING:
284 sendButton.setText("Sending");
285 sendButton.setEnabled(false);
286 break;
287 case ERROR:
288 sendButton.setText("Error, resend");
289 sendButton.setEnabled(true);
290 break;
291 case FINISHED:
292 sendButton.setText("Sent");
293 sendButton.setEnabled(false);
294 break;
295 case NOT_APPLICABLE:
296 sendButton.setText("N/A");
297 sendButton.setEnabled(false);
298 break;
299 default:
300 sendButton.setText("Send");
301 sendButton.setEnabled(true);
302 break;
303 }
304 }
305
306 /** {@inheritDoc} */
307 @Override
308 public void errorAdded(final ProgramError error) {
309 //Ignore
/*
P/P * Method: void errorAdded(ProgramError)
*/
310 }
311
312 /** {@inheritDoc} */
313 @Override
314 public void errorDeleted(final ProgramError error) {
315 //Ignore
/*
P/P * Method: void errorDeleted(ProgramError)
*/
316 }
317
318 /** {@inheritDoc} */
319 @Override
320 public void errorStatusChanged(final ProgramError error) {
/*
P/P * Method: void errorStatusChanged(ProgramError)
*
* Preconditions:
* this.error != null
* (soft) init'ed(com.dmdirc.ui.FatalErrorDialog$4__static_init.new int[](FatalErrorDialog$4__static_init#1)[...])
* (soft) error init'ed
* (soft) error.reportStatus != null
* (soft) this.error.message != null
* (soft) this.okButton != null
* (soft) this.sendButton != null
*/
321 if (this.error.equals(error)) {
322 final ErrorReportStatus status = error.getReportStatus();
323 okButton.setEnabled(status.isTerminal());
324 updateSendButtonText(status);
325 }
326 }
327
328 /** {@inheritDoc} */
329 @Override
330 public boolean isReady() {
331 //We're never ready
/*
P/P * Method: bool isReady()
*
* Postconditions:
* return_value == 0
*/
332 return false;
333 }
334 }
SofCheck Inspector Build Version : 2.17854
| FatalErrorDialog.java |
2009-Jun-25 01:54:24 |
| FatalErrorDialog.class |
2009-Sep-02 17:04:17 |
| FatalErrorDialog$1.class |
2009-Sep-02 17:04:17 |
| FatalErrorDialog$2.class |
2009-Sep-02 17:04:17 |
| FatalErrorDialog$3.class |
2009-Sep-02 17:04:17 |
| FatalErrorDialog$3$1.class |
2009-Sep-02 17:04:17 |
| FatalErrorDialog$4.class |
2009-Sep-02 17:04:17 |