001: /*
002: * Project: AMODA - Abstract Modeled Application
003: * Class: de.gulden.framework.amoda.environment.gui.component.ErrorDialog
004: * Version: snapshot-beautyj-1.1
005: *
006: * Date: 2004-09-29
007: *
008: * This is a snapshot version of the AMODA 0.2 development branch,
009: * it is not released as a seperate version.
010: * For AMODA, see http://amoda.berlios.de/.
011: *
012: * This is licensed under the GNU Lesser General Public License (LGPL)
013: * and comes with NO WARRANTY.
014: *
015: * Author: Jens Gulden
016: * Email: amoda@jensgulden.de
017: */
018:
019: package de.gulden.framework.amoda.environment.gui.component;
020:
021: import de.gulden.framework.amoda.model.interaction.*;
022: import de.gulden.util.Toolbox;
023: import javax.swing.*;
024: import java.awt.*;
025: import java.io.*;
026: import java.util.*;
027: import java.util.List;
028:
029: /**
030: * Class ErrorDialog.
031: *
032: * @author Jens Gulden
033: * @version snapshot-beautyj-1.1
034: */
035: class ErrorDialog extends JDialog implements ErrorMessagePerformer {
036:
037: // ------------------------------------------------------------------------
038: // --- fields ---
039: // ------------------------------------------------------------------------
040:
041: private List messages;
042:
043: private int pos;
044:
045: private boolean extended;
046:
047: private boolean firstTime;
048:
049: private JButton backButton;
050:
051: private JPanel buttonPanel;
052:
053: private JLabel countLabel;
054:
055: private JPanel extendPanel;
056:
057: private JPanel extendedPanel;
058:
059: private JButton forwardButton;
060:
061: private JLabel messageLabel;
062:
063: private JPanel messagePanel;
064:
065: private JButton moreButton;
066:
067: private JPanel moreButtonPanel;
068:
069: private JButton okButton;
070:
071: private JScrollPane stackTraceScrollPane;
072:
073: private JTextPane stackTraceTextPane;
074:
075: // ------------------------------------------------------------------------
076: // --- constructor ---
077: // ------------------------------------------------------------------------
078:
079: ErrorDialog(Frame parent) {
080: super (parent, "Error", false); // not modal
081: messages = new ArrayList();
082: pos = -1;
083: extended = false;
084: firstTime = true;
085: initComponents();
086: okButton.requestFocus();
087: pack();
088: }
089:
090: // ------------------------------------------------------------------------
091: // --- methods ---
092: // ------------------------------------------------------------------------
093:
094: /**
095: *
096: * @param errorMessage ...
097: * </p>
098: */
099: public void doErrorMessage(ErrorMessage errorMessage) {
100: messages.add(errorMessage);
101: //(comment-out to keep first message visible (may be wanted if multiple errors occur before user can react)):
102: pos = messages.size() - 1;
103: if (firstTime) {
104: Toolbox.centerComponent(this , getParent());
105: pos = 0;
106: firstTime = false;
107: }
108: update();
109: setVisible(true);
110: }
111:
112: private void initComponents() {
113: //GEN-BEGIN:initComponents
114: java.awt.GridBagConstraints gridBagConstraints;
115:
116: messagePanel = new javax.swing.JPanel();
117: messageLabel = new javax.swing.JLabel();
118: buttonPanel = new javax.swing.JPanel();
119: okButton = new javax.swing.JButton();
120: extendPanel = new javax.swing.JPanel();
121: moreButtonPanel = new javax.swing.JPanel();
122: backButton = new javax.swing.JButton();
123: forwardButton = new javax.swing.JButton();
124: countLabel = new javax.swing.JLabel();
125: moreButton = new javax.swing.JButton();
126: extendedPanel = new javax.swing.JPanel();
127: stackTraceScrollPane = new javax.swing.JScrollPane();
128: stackTraceTextPane = new javax.swing.JTextPane();
129:
130: addWindowListener(new java.awt.event.WindowAdapter() {
131: public void windowClosing(java.awt.event.WindowEvent evt) {
132: closeDialog(evt);
133: }
134: });
135:
136: messagePanel.setLayout(new java.awt.BorderLayout());
137:
138: messagePanel.setBorder(new javax.swing.border.EtchedBorder());
139: messageLabel
140: .setIcon(new javax.swing.ImageIcon(
141: getClass()
142: .getResource(
143: "/de/gulden/framework/amoda/environment/gui/resource/Stop24.gif")));
144: messageLabel.setText("...");
145: messageLabel.setPreferredSize(new java.awt.Dimension(800, 72));
146: messagePanel.add(messageLabel, java.awt.BorderLayout.CENTER);
147:
148: getContentPane().add(messagePanel, java.awt.BorderLayout.NORTH);
149:
150: buttonPanel.setLayout(new java.awt.FlowLayout(
151: java.awt.FlowLayout.CENTER, 5, 4));
152:
153: okButton.setFont(new java.awt.Font("Dialog", 0, 12));
154: okButton.setText(" OK ");
155: okButton.addActionListener(new java.awt.event.ActionListener() {
156: public void actionPerformed(java.awt.event.ActionEvent evt) {
157: okButtonActionPerformed(evt);
158: }
159: });
160:
161: buttonPanel.add(okButton);
162:
163: getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);
164:
165: extendPanel.setLayout(new java.awt.BorderLayout());
166:
167: moreButtonPanel.setLayout(new java.awt.GridBagLayout());
168:
169: backButton.setFont(new java.awt.Font("Dialog", 0, 10));
170: backButton.setText("<<");
171: backButton
172: .addActionListener(new java.awt.event.ActionListener() {
173: public void actionPerformed(
174: java.awt.event.ActionEvent evt) {
175: backButtonActionPerformed(evt);
176: }
177: });
178:
179: gridBagConstraints = new java.awt.GridBagConstraints();
180: gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 0);
181: moreButtonPanel.add(backButton, gridBagConstraints);
182:
183: forwardButton.setFont(new java.awt.Font("Dialog", 0, 10));
184: forwardButton.setText(">>");
185: forwardButton
186: .addActionListener(new java.awt.event.ActionListener() {
187: public void actionPerformed(
188: java.awt.event.ActionEvent evt) {
189: forwardButtonActionPerformed(evt);
190: }
191: });
192:
193: moreButtonPanel.add(forwardButton,
194: new java.awt.GridBagConstraints());
195:
196: countLabel.setFont(new java.awt.Font("Dialog", 0, 10));
197: countLabel.setText("Message 1 of 10");
198: countLabel.setEnabled(false);
199: gridBagConstraints = new java.awt.GridBagConstraints();
200: gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
201: moreButtonPanel.add(countLabel, gridBagConstraints);
202:
203: moreButton.setFont(new java.awt.Font("Dialog", 0, 10));
204: moreButton.setText("More >>");
205: moreButton
206: .addActionListener(new java.awt.event.ActionListener() {
207: public void actionPerformed(
208: java.awt.event.ActionEvent evt) {
209: moreButtonActionPerformed(evt);
210: }
211: });
212:
213: gridBagConstraints = new java.awt.GridBagConstraints();
214: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
215: gridBagConstraints.weightx = 1.0;
216: gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 1);
217: moreButtonPanel.add(moreButton, gridBagConstraints);
218:
219: extendPanel.add(moreButtonPanel, java.awt.BorderLayout.SOUTH);
220:
221: extendedPanel.setLayout(new java.awt.BorderLayout());
222:
223: stackTraceTextPane
224: .setBackground(javax.swing.UIManager.getDefaults()
225: .getColor("TextField.inactiveBackground"));
226: stackTraceTextPane.setEditable(false);
227: stackTraceTextPane.setFont(new java.awt.Font("DialogInput", 0,
228: 10));
229: stackTraceTextPane.setMinimumSize(new java.awt.Dimension(600,
230: 250));
231: stackTraceTextPane.setPreferredSize(new java.awt.Dimension(800,
232: 250));
233: stackTraceScrollPane.setViewportView(stackTraceTextPane);
234:
235: extendedPanel.add(stackTraceScrollPane,
236: java.awt.BorderLayout.CENTER);
237:
238: extendPanel.add(extendedPanel, java.awt.BorderLayout.CENTER);
239:
240: getContentPane().add(extendPanel, java.awt.BorderLayout.CENTER);
241:
242: pack();
243: }
244:
245: private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
246: //GEN-FIRST:event_okButtonActionPerformed
247: setVisible(false);
248: }
249:
250: private void forwardButtonActionPerformed(
251: java.awt.event.ActionEvent evt) {
252: //GEN-FIRST:event_forwardButtonActionPerformed
253: if (pos < messages.size() - 1) {
254: pos++;
255: update();
256: }
257: }
258:
259: private void backButtonActionPerformed(
260: java.awt.event.ActionEvent evt) {
261: //GEN-FIRST:event_backButtonActionPerformed
262: if (pos > 0) {
263: pos--;
264: update();
265: }
266: }
267:
268: private void moreButtonActionPerformed(
269: java.awt.event.ActionEvent evt) {
270: //GEN-FIRST:event_moreButtonActionPerformed
271: extended = !extended;
272: if (!extended) {
273: pos = messages.size() - 1; // make sure it's the current one if collapsed
274: }
275: update();
276: }
277:
278: private void closeDialog(java.awt.event.WindowEvent evt) {
279: //GEN-FIRST:event_closeDialog
280: setVisible(false);
281:
282: }
283:
284: private void update() {
285: ErrorMessage msg = (ErrorMessage) messages.get(pos);
286: String text = msg.getText();
287: Throwable throwable = msg.getCause();
288: if (throwable != null) {
289: String throwableName = throwable.getClass().getName();
290: int index = throwableName.lastIndexOf('.');
291: if (index != -1) {
292: throwableName = throwableName.substring(index + 1);
293: }
294: String tmsg = throwable.getMessage();
295: if (tmsg != null) {
296: tmsg = Toolbox.capitalize(tmsg.trim());
297: if (!tmsg.endsWith(".")) {
298: tmsg += ".";
299: }
300: text = text + " " + tmsg;
301: }
302: if ((text == null) || (text.length() == 0)) {
303: text = throwableName;
304: } else {
305: text = throwableName + ": " + text;
306: }
307: }
308: messageLabel.setText(text);
309: if (extended) {
310: extendPanel.add(extendedPanel, BorderLayout.CENTER);
311: moreButtonPanel.remove(moreButton);
312: GridBagConstraints gbc = new GridBagConstraints();
313: moreButtonPanel.add(backButton, gbc);
314: moreButtonPanel.add(forwardButton, gbc);
315: moreButtonPanel.add(countLabel, gbc);
316: gbc.weightx = 1.0;
317: gbc.anchor = GridBagConstraints.EAST;
318: moreButtonPanel.add(moreButton, gbc);
319: moreButton.setText("Less <<");
320: StringWriter sw = new StringWriter();
321: throwable.printStackTrace(new PrintWriter(sw));
322: String stackTrace = sw.toString();
323: stackTraceTextPane.setText(stackTrace);
324: } else {
325: extendPanel.remove(extendedPanel);
326: moreButtonPanel.remove(backButton);
327: moreButtonPanel.remove(forwardButton);
328: moreButtonPanel.remove(countLabel);
329: moreButton.setText("More >>");
330: }
331: backButton.setEnabled((pos > 0));
332: forwardButton.setEnabled((pos < messages.size() - 1));
333: countLabel.setText("Message " + (pos + 1) + " of "
334: + messages.size());
335: pack();
336: stackTraceTextPane.setCaretPosition(0);
337: }
338:
339: } // end ErrorDialog
|