KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > common > ShowExceptionMessageDialog


1 package com.calipso.reportgenerator.common;
2
3 import com.calipso.reportgenerator.common.CalipsoException;
4
5 import javax.swing.*;
6 import java.awt.event.ActionListener JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.WindowAdapter JavaDoc;
9 import java.awt.event.WindowEvent JavaDoc;
10 import java.awt.*;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 /**
15  * Despliga un mensaje de error
16  */

17
18 public class ShowExceptionMessageDialog extends JDialog {
19   JPanel mainPanel;
20   JPanel topPanel;
21   JPanel centerPanel;
22   JButton hideDetailsButton;
23   JTextArea textField;
24   JScrollPane scrollPane;
25   private JLabel detalleLabel;
26   private JButton cancelBtn;
27   private JButton detalleBtn;
28   private JButton stackBtn;
29
30   /**
31    * Inicializa un objeto ShowExceptionMessageDialog
32    * @param s
33    * @param e
34    */

35   public ShowExceptionMessageDialog(String JavaDoc s, Exception JavaDoc e) {
36     super(new JFrame(), true);
37     showExceptionMessage(s, e);
38   }
39
40   /**
41    * Crea los componentes que componen el Dialog
42    */

43   private void initComponents(Exception JavaDoc e) {
44     setTitle(com.calipso.reportgenerator.common.LanguageTraslator.traslate("231"));
45     getContentPane().add(getMainPanel(e));
46     setResizable(false);
47     addWindowListener(new WindowAdapter JavaDoc() {
48       public void windowOpened(WindowEvent JavaDoc e) {
49         setSize(getWidth(), getHeight() - centerPanel.getHeight());
50         scrollPane.setVisible(false);
51       }
52     });
53     pack();
54     java.awt.Dimension JavaDoc screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
55     setSize(new java.awt.Dimension JavaDoc(400, 300));
56     setLocation((screenSize.width - 400) / 2, (screenSize.height - 300) / 4);
57   }
58
59   /**
60    * Retorna el panel principal del Dialog
61    * @return
62    */

63   private JPanel getMainPanel(Exception JavaDoc e) {
64     JPanel xTopPanel = getTopPanel(e);
65     JPanel xCenterPanel = getCenterPanel();
66     mainPanel = new JPanel(new BorderLayout());
67     mainPanel.add(xTopPanel, BorderLayout.NORTH);
68     mainPanel.add(xCenterPanel, BorderLayout.CENTER);
69     return mainPanel;
70   }
71
72   /**
73    * Retorna el Panel que contiene el JTextArea ubicado en la seccion central del panel principal
74    * @return
75    */

76   private JPanel getCenterPanel() {
77     centerPanel = new JPanel(new BorderLayout());
78     textField = new JTextArea();
79     scrollPane = new JScrollPane();
80     textField.setEditable(false);
81     scrollPane.getViewport().add(textField);
82     centerPanel.add(scrollPane, BorderLayout.CENTER);
83     return centerPanel;
84   }
85
86   /**
87    * Retorna el panel ubicado al norte del panel principal
88    * @return
89    */

90   private JPanel getTopPanel(final Exception JavaDoc e) {
91     detalleBtn = new JButton();
92     cancelBtn = new JButton();
93     stackBtn = new JButton();
94     topPanel = new JPanel(null);
95     topPanel.setPreferredSize(new Dimension(400, 200));
96     detalleBtn.addActionListener(new ActionListener JavaDoc() {
97       public void actionPerformed(ActionEvent JavaDoc e) {
98         boolean value = scrollPane.isVisible();
99         if (value) {
100           setSize(getWidth(), getHeight() - centerPanel.getHeight());
101           stackBtn.setVisible(false);
102         }
103         else {
104           setSize(getWidth(), getHeight() + centerPanel.getHeight());
105           stackBtn.setVisible(true);
106         }
107         scrollPane.setVisible(!value);
108
109       }
110     });
111     cancelBtn.addActionListener(new ActionListener JavaDoc() {
112       public void actionPerformed(ActionEvent JavaDoc e) {
113         setVisible(false);
114         dispose();
115       }
116     });
117
118     stackBtn.addActionListener(new ActionListener JavaDoc() {
119       public void actionPerformed(ActionEvent JavaDoc evt) {
120         ShowExceptionStackTrace.showStackTrace(e);
121       }
122     });
123
124     detalleLabel = new JLabel();
125     getContentPane().add(detalleLabel);
126     detalleLabel.setText("222");
127     detalleLabel.setBounds(8, 65, 382, 16);
128
129     cancelBtn.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("112"));
130     getContentPane().add(cancelBtn);
131     cancelBtn.setBounds(306, 168, 85, 26);
132
133     detalleBtn.setText(com.calipso.reportgenerator.common.LanguageTraslator.traslate("230"));
134     getContentPane().add(detalleBtn);
135     detalleBtn.setBounds(216, 168, 81, 26);
136
137     stackBtn.setText("Stack");
138     getContentPane().add(stackBtn);
139     stackBtn.setBounds(126, 168, 81, 26);
140     stackBtn.setVisible(false);
141     return topPanel;
142   }
143
144   /**
145    * Crea y carga y visualiza el Componente
146    * @param code
147    * @param e
148    */

149   public static void initExceptionDialogMessage(String JavaDoc code, Exception JavaDoc e) {
150     new ShowExceptionMessageDialog(code, e);
151
152
153   }
154
155   /**
156    * Agrega al JTextField todos los mensajes de la Exceptions que ocurrieron
157    * @param code
158    * @param e
159    */

160   private void showExceptionMessage(String JavaDoc code, Exception JavaDoc e) {
161     ArrayList JavaDoc list = new ArrayList JavaDoc();
162     fillCauses(e, list);
163     initComponents(e);
164     detalleLabel.setText(code);
165     Iterator JavaDoc iterator = list.iterator();
166     while (iterator.hasNext()) {
167       String JavaDoc s = (String JavaDoc) iterator.next();
168       if(s !=null){
169         textField.append(s+"\n");
170       }
171     }
172
173     setVisible(true);
174   }
175
176   /**
177    * Carga las causas por las cuales ocurrieron las distintas Exceptions
178    * @param e
179    * @param list
180    */

181   private void fillCauses(Throwable JavaDoc e, ArrayList JavaDoc list) {
182     list.add(e.getMessage());
183     if (e instanceof CalipsoException) {
184       Throwable JavaDoc subE = e.getCause();
185       if (subE != null && e != subE) {
186         fillCauses(subE, list);
187       }
188     }
189   }
190 }
191
Popular Tags