2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
* $RCSfile: ShowThrowableDialog.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.4 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2006/02/23 20:59:41 $ |
2 |
26 Feb 07 |
jari |
* $Author: caliente $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
9 |
*/ |
2 |
26 Feb 07 |
jari |
10 |
package org.tigr.microarray.mev; |
2 |
26 Feb 07 |
jari |
11 |
|
2 |
26 Feb 07 |
jari |
12 |
import java.awt.BorderLayout; |
2 |
26 Feb 07 |
jari |
13 |
import java.awt.Dimension; |
2 |
26 Feb 07 |
jari |
14 |
import java.awt.Frame; |
2 |
26 Feb 07 |
jari |
15 |
import java.awt.GridBagConstraints; |
2 |
26 Feb 07 |
jari |
16 |
import java.awt.GridBagLayout; |
2 |
26 Feb 07 |
jari |
17 |
import java.awt.GridLayout; |
2 |
26 Feb 07 |
jari |
18 |
import java.awt.Insets; |
2 |
26 Feb 07 |
jari |
19 |
import java.awt.Toolkit; |
2 |
26 Feb 07 |
jari |
20 |
import java.awt.event.ActionEvent; |
2 |
26 Feb 07 |
jari |
21 |
import java.awt.event.ActionListener; |
2 |
26 Feb 07 |
jari |
22 |
import java.awt.event.WindowAdapter; |
2 |
26 Feb 07 |
jari |
23 |
import java.awt.event.WindowEvent; |
2 |
26 Feb 07 |
jari |
24 |
import java.io.ByteArrayOutputStream; |
2 |
26 Feb 07 |
jari |
25 |
import java.io.PrintStream; |
2 |
26 Feb 07 |
jari |
26 |
|
2 |
26 Feb 07 |
jari |
27 |
import javax.swing.AbstractButton; |
2 |
26 Feb 07 |
jari |
28 |
import javax.swing.Icon; |
2 |
26 Feb 07 |
jari |
29 |
import javax.swing.JButton; |
2 |
26 Feb 07 |
jari |
30 |
import javax.swing.JComponent; |
2 |
26 Feb 07 |
jari |
31 |
import javax.swing.JDialog; |
2 |
26 Feb 07 |
jari |
32 |
import javax.swing.JLabel; |
2 |
26 Feb 07 |
jari |
33 |
import javax.swing.JOptionPane; |
2 |
26 Feb 07 |
jari |
34 |
import javax.swing.JPanel; |
2 |
26 Feb 07 |
jari |
35 |
import javax.swing.JScrollPane; |
2 |
26 Feb 07 |
jari |
36 |
import javax.swing.JTextArea; |
2 |
26 Feb 07 |
jari |
37 |
import javax.swing.JToggleButton; |
2 |
26 Feb 07 |
jari |
38 |
import javax.swing.UIManager; |
2 |
26 Feb 07 |
jari |
39 |
import javax.swing.border.BevelBorder; |
2 |
26 Feb 07 |
jari |
40 |
import javax.swing.border.EmptyBorder; |
2 |
26 Feb 07 |
jari |
41 |
|
2 |
26 Feb 07 |
jari |
42 |
public class ShowThrowableDialog extends JDialog { |
2 |
26 Feb 07 |
jari |
43 |
|
2 |
26 Feb 07 |
jari |
44 |
private JComponent message; |
2 |
26 Feb 07 |
jari |
45 |
private JComponent stack; |
2 |
26 Feb 07 |
jari |
46 |
private JPanel mainPanel; |
2 |
26 Feb 07 |
jari |
47 |
|
2 |
26 Feb 07 |
jari |
48 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs the dialog with specified title, modal flag, type to display |
2 |
26 Feb 07 |
jari |
* throwable object. |
2 |
26 Feb 07 |
jari |
51 |
*/ |
2 |
26 Feb 07 |
jari |
52 |
public ShowThrowableDialog(Frame frame, String title, boolean modal, int type, Throwable t) { |
2 |
26 Feb 07 |
jari |
53 |
super(frame, title, modal); |
2 |
26 Feb 07 |
jari |
54 |
Listener listener = new Listener(); |
2 |
26 Feb 07 |
jari |
55 |
addWindowListener(listener); |
2 |
26 Feb 07 |
jari |
56 |
|
2 |
26 Feb 07 |
jari |
57 |
this.message = createMessageContent(type, t); |
2 |
26 Feb 07 |
jari |
58 |
this.stack = createStackContent(t); |
2 |
26 Feb 07 |
jari |
59 |
|
2 |
26 Feb 07 |
jari |
60 |
this.mainPanel = createMainPanel(); |
2 |
26 Feb 07 |
jari |
61 |
this.mainPanel.add(this.message, BorderLayout.CENTER); |
2 |
26 Feb 07 |
jari |
62 |
JPanel btnsPanel = createBtnsPanel(listener); |
2 |
26 Feb 07 |
jari |
63 |
|
2 |
26 Feb 07 |
jari |
64 |
getContentPane().setLayout(new GridBagLayout()); |
2 |
26 Feb 07 |
jari |
65 |
getContentPane().add(mainPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0 |
2 |
26 Feb 07 |
jari |
66 |
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); |
2 |
26 Feb 07 |
jari |
67 |
getContentPane().add(btnsPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0 |
2 |
26 Feb 07 |
jari |
68 |
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(4, 8, 4, 8), 0, 0)); |
2 |
26 Feb 07 |
jari |
69 |
pack(); |
2 |
26 Feb 07 |
jari |
70 |
} |
2 |
26 Feb 07 |
jari |
71 |
|
2 |
26 Feb 07 |
jari |
72 |
/** |
2 |
26 Feb 07 |
jari |
* Displays the dialog with specified title. |
2 |
26 Feb 07 |
jari |
74 |
*/ |
2 |
26 Feb 07 |
jari |
75 |
public static void show(Frame frame, String title, Throwable t) { |
2 |
26 Feb 07 |
jari |
76 |
show(frame, title, true, t); |
2 |
26 Feb 07 |
jari |
77 |
} |
2 |
26 Feb 07 |
jari |
78 |
|
2 |
26 Feb 07 |
jari |
79 |
/** |
2 |
26 Feb 07 |
jari |
* Displays the dialog with specified title and modal state. |
2 |
26 Feb 07 |
jari |
81 |
*/ |
2 |
26 Feb 07 |
jari |
82 |
public static void show(Frame frame, String title, boolean modal, Throwable t) { |
2 |
26 Feb 07 |
jari |
83 |
show(frame, title, modal, JOptionPane.ERROR_MESSAGE, t); |
2 |
26 Feb 07 |
jari |
84 |
} |
2 |
26 Feb 07 |
jari |
85 |
|
2 |
26 Feb 07 |
jari |
86 |
/** |
2 |
26 Feb 07 |
jari |
* Displays the dialog with specified title, modal state and message type. |
2 |
26 Feb 07 |
jari |
88 |
*/ |
2 |
26 Feb 07 |
jari |
89 |
public static void show(Frame frame, String title, boolean modal, int type, Throwable t) { |
2 |
26 Feb 07 |
jari |
90 |
ShowThrowableDialog dlg = new ShowThrowableDialog(frame, title, modal, type, t); |
2 |
26 Feb 07 |
jari |
91 |
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
2 |
26 Feb 07 |
jari |
92 |
dlg.setLocation((screenSize.width - dlg.getSize().width)/2, (screenSize.height - dlg.getSize().height)/2); |
2 |
26 Feb 07 |
jari |
93 |
dlg.show(); |
2 |
26 Feb 07 |
jari |
94 |
} |
2 |
26 Feb 07 |
jari |
95 |
|
2 |
26 Feb 07 |
jari |
96 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the icon to use for the passed in type. |
2 |
26 Feb 07 |
jari |
98 |
*/ |
2 |
26 Feb 07 |
jari |
99 |
private Icon getIconForType(int messageType) { |
2 |
26 Feb 07 |
jari |
100 |
if (messageType < 0 || messageType > 3) |
2 |
26 Feb 07 |
jari |
101 |
return null; |
2 |
26 Feb 07 |
jari |
102 |
switch (messageType) { |
2 |
26 Feb 07 |
jari |
103 |
case 0: |
2 |
26 Feb 07 |
jari |
104 |
return UIManager.getIcon("OptionPane.errorIcon"); |
2 |
26 Feb 07 |
jari |
105 |
case 1: |
2 |
26 Feb 07 |
jari |
106 |
return UIManager.getIcon("OptionPane.informationIcon"); |
2 |
26 Feb 07 |
jari |
107 |
case 2: |
2 |
26 Feb 07 |
jari |
108 |
return UIManager.getIcon("OptionPane.warningIcon"); |
2 |
26 Feb 07 |
jari |
109 |
case 3: |
2 |
26 Feb 07 |
jari |
110 |
return UIManager.getIcon("OptionPane.questionIcon"); |
2 |
26 Feb 07 |
jari |
111 |
} |
2 |
26 Feb 07 |
jari |
112 |
return null; |
2 |
26 Feb 07 |
jari |
113 |
} |
2 |
26 Feb 07 |
jari |
114 |
|
2 |
26 Feb 07 |
jari |
115 |
/** |
2 |
26 Feb 07 |
jari |
* Creates a component to render message content. |
2 |
26 Feb 07 |
jari |
117 |
*/ |
2 |
26 Feb 07 |
jari |
118 |
private JComponent createMessageContent(int type, Throwable t) { |
2 |
26 Feb 07 |
jari |
119 |
JPanel panel = new JPanel(new GridBagLayout()); |
2 |
26 Feb 07 |
jari |
120 |
panel.setBorder(new EmptyBorder(10, 10, 10, 10)); |
2 |
26 Feb 07 |
jari |
121 |
GridBagConstraints gbc = new GridBagConstraints(); |
2 |
26 Feb 07 |
jari |
122 |
gbc.anchor = GridBagConstraints.WEST; |
2 |
26 Feb 07 |
jari |
123 |
gbc.fill = GridBagConstraints.HORIZONTAL; |
2 |
26 Feb 07 |
jari |
124 |
gbc.gridx = 0; |
2 |
26 Feb 07 |
jari |
125 |
gbc.gridy = 0; |
2 |
26 Feb 07 |
jari |
126 |
gbc.weightx = 0.0; |
2 |
26 Feb 07 |
jari |
127 |
panel.add(new JLabel(getIconForType(type)), gbc); |
2 |
26 Feb 07 |
jari |
128 |
gbc.gridx = 1; |
2 |
26 Feb 07 |
jari |
129 |
gbc.weightx = 1.0; |
2 |
26 Feb 07 |
jari |
130 |
|
2 |
26 Feb 07 |
jari |
131 |
gbc.insets.left = 20; |
2 |
26 Feb 07 |
jari |
132 |
panel.add(new JLabel(t.getMessage()), gbc); |
2 |
26 Feb 07 |
jari |
133 |
return panel; |
2 |
26 Feb 07 |
jari |
134 |
} |
2 |
26 Feb 07 |
jari |
135 |
|
2 |
26 Feb 07 |
jari |
136 |
/** |
2 |
26 Feb 07 |
jari |
* Returns a throwable object stack trace. |
2 |
26 Feb 07 |
jari |
138 |
*/ |
2 |
26 Feb 07 |
jari |
139 |
private String getTrace(Throwable t) { |
2 |
26 Feb 07 |
jari |
140 |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
2 |
26 Feb 07 |
jari |
141 |
PrintStream ps = new PrintStream(stream); |
2 |
26 Feb 07 |
jari |
142 |
t.printStackTrace(ps); |
2 |
26 Feb 07 |
jari |
143 |
return stream.toString(); |
2 |
26 Feb 07 |
jari |
144 |
} |
2 |
26 Feb 07 |
jari |
145 |
|
2 |
26 Feb 07 |
jari |
146 |
/** |
2 |
26 Feb 07 |
jari |
* Creates a component to render stack trace. |
2 |
26 Feb 07 |
jari |
148 |
*/ |
2 |
26 Feb 07 |
jari |
149 |
private JComponent createStackContent(Throwable t) { |
2 |
26 Feb 07 |
jari |
150 |
JTextArea area = new JTextArea(getTrace(t)); |
2 |
26 Feb 07 |
jari |
151 |
area.setEditable(false); |
2 |
26 Feb 07 |
jari |
152 |
JScrollPane scroll = new JScrollPane(area); |
2 |
26 Feb 07 |
jari |
153 |
scroll.setPreferredSize(new Dimension(250, 150)); |
2 |
26 Feb 07 |
jari |
154 |
return scroll; |
2 |
26 Feb 07 |
jari |
155 |
} |
2 |
26 Feb 07 |
jari |
156 |
|
2 |
26 Feb 07 |
jari |
157 |
/** |
2 |
26 Feb 07 |
jari |
* Sets main panel content. |
2 |
26 Feb 07 |
jari |
159 |
*/ |
2 |
26 Feb 07 |
jari |
160 |
private void setContent(JComponent content) { |
2 |
26 Feb 07 |
jari |
161 |
this.mainPanel.removeAll(); |
2 |
26 Feb 07 |
jari |
162 |
this.mainPanel.add(content, BorderLayout.CENTER); |
2 |
26 Feb 07 |
jari |
163 |
this.mainPanel.validate(); |
2 |
26 Feb 07 |
jari |
164 |
this.mainPanel.repaint(); |
2 |
26 Feb 07 |
jari |
165 |
} |
2 |
26 Feb 07 |
jari |
166 |
|
2 |
26 Feb 07 |
jari |
167 |
/** |
2 |
26 Feb 07 |
jari |
* Creates the main panel. |
2 |
26 Feb 07 |
jari |
169 |
*/ |
2 |
26 Feb 07 |
jari |
170 |
private JPanel createMainPanel() { |
2 |
26 Feb 07 |
jari |
171 |
JPanel panel = new JPanel(new BorderLayout()); |
2 |
26 Feb 07 |
jari |
172 |
panel.setBorder(new BevelBorder(BevelBorder.RAISED)); |
2 |
26 Feb 07 |
jari |
173 |
panel.setPreferredSize(new Dimension(400, 100)); |
2 |
26 Feb 07 |
jari |
174 |
return panel; |
2 |
26 Feb 07 |
jari |
175 |
} |
2 |
26 Feb 07 |
jari |
176 |
|
2 |
26 Feb 07 |
jari |
177 |
/** |
2 |
26 Feb 07 |
jari |
* Creates a panel with 'OK' and 'Stack' buttons. |
2 |
26 Feb 07 |
jari |
179 |
*/ |
2 |
26 Feb 07 |
jari |
180 |
private JPanel createBtnsPanel(ActionListener listener) { |
2 |
26 Feb 07 |
jari |
181 |
GridLayout gridLayout = new GridLayout(); |
2 |
26 Feb 07 |
jari |
182 |
JPanel panel = new JPanel(gridLayout); |
2 |
26 Feb 07 |
jari |
183 |
|
2 |
26 Feb 07 |
jari |
184 |
JButton okButton = new JButton("OK"); |
2 |
26 Feb 07 |
jari |
185 |
okButton.setActionCommand("ok-command"); |
2 |
26 Feb 07 |
jari |
186 |
okButton.addActionListener(listener); |
2 |
26 Feb 07 |
jari |
187 |
panel.add(okButton); |
2 |
26 Feb 07 |
jari |
188 |
|
2 |
26 Feb 07 |
jari |
189 |
JToggleButton stackButton = new JToggleButton("Stack"); |
2 |
26 Feb 07 |
jari |
190 |
stackButton.setActionCommand("stack-command"); |
2 |
26 Feb 07 |
jari |
191 |
stackButton.addActionListener(listener); |
2 |
26 Feb 07 |
jari |
192 |
gridLayout.setHgap(4); |
2 |
26 Feb 07 |
jari |
193 |
panel.add(stackButton); |
2 |
26 Feb 07 |
jari |
194 |
|
2 |
26 Feb 07 |
jari |
195 |
getRootPane().setDefaultButton(okButton); |
2 |
26 Feb 07 |
jari |
196 |
|
2 |
26 Feb 07 |
jari |
197 |
return panel; |
2 |
26 Feb 07 |
jari |
198 |
} |
2 |
26 Feb 07 |
jari |
199 |
|
2 |
26 Feb 07 |
jari |
200 |
/** |
2 |
26 Feb 07 |
jari |
* Sets message or stack content to be displayed. |
2 |
26 Feb 07 |
jari |
202 |
*/ |
2 |
26 Feb 07 |
jari |
203 |
private void onStackTrace(boolean flag) { |
2 |
26 Feb 07 |
jari |
204 |
if (flag) { |
2 |
26 Feb 07 |
jari |
205 |
setContent(this.stack); |
2 |
26 Feb 07 |
jari |
206 |
} else { |
2 |
26 Feb 07 |
jari |
207 |
setContent(this.message); |
2 |
26 Feb 07 |
jari |
208 |
} |
2 |
26 Feb 07 |
jari |
209 |
} |
2 |
26 Feb 07 |
jari |
210 |
|
2 |
26 Feb 07 |
jari |
211 |
/** |
2 |
26 Feb 07 |
jari |
* Listener to listen to window and action events. |
2 |
26 Feb 07 |
jari |
213 |
*/ |
2 |
26 Feb 07 |
jari |
214 |
private class Listener extends WindowAdapter implements ActionListener { |
2 |
26 Feb 07 |
jari |
215 |
|
2 |
26 Feb 07 |
jari |
216 |
public void actionPerformed(ActionEvent e) { |
2 |
26 Feb 07 |
jari |
217 |
String command = e.getActionCommand(); |
2 |
26 Feb 07 |
jari |
218 |
if (command.equals("ok-command")) { |
2 |
26 Feb 07 |
jari |
219 |
dispose(); |
2 |
26 Feb 07 |
jari |
220 |
} else if (command.equals("stack-command")) { |
2 |
26 Feb 07 |
jari |
221 |
AbstractButton button = (AbstractButton)e.getSource(); |
2 |
26 Feb 07 |
jari |
222 |
onStackTrace(button.isSelected()); |
2 |
26 Feb 07 |
jari |
223 |
} |
2 |
26 Feb 07 |
jari |
224 |
} |
2 |
26 Feb 07 |
jari |
225 |
|
2 |
26 Feb 07 |
jari |
226 |
public void windowClosing(WindowEvent e) { |
2 |
26 Feb 07 |
jari |
227 |
dispose(); |
2 |
26 Feb 07 |
jari |
228 |
} |
2 |
26 Feb 07 |
jari |
229 |
} |
2 |
26 Feb 07 |
jari |
230 |
} |