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 ; 7 import java.awt.event.ActionEvent ; 8 import java.awt.event.WindowAdapter ; 9 import java.awt.event.WindowEvent ; 10 import java.awt.*; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 14 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 35 public ShowExceptionMessageDialog(String s, Exception e) { 36 super(new JFrame(), true); 37 showExceptionMessage(s, e); 38 } 39 40 43 private void initComponents(Exception e) { 44 setTitle(com.calipso.reportgenerator.common.LanguageTraslator.traslate("231")); 45 getContentPane().add(getMainPanel(e)); 46 setResizable(false); 47 addWindowListener(new WindowAdapter () { 48 public void windowOpened(WindowEvent e) { 49 setSize(getWidth(), getHeight() - centerPanel.getHeight()); 50 scrollPane.setVisible(false); 51 } 52 }); 53 pack(); 54 java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 55 setSize(new java.awt.Dimension (400, 300)); 56 setLocation((screenSize.width - 400) / 2, (screenSize.height - 300) / 4); 57 } 58 59 63 private JPanel getMainPanel(Exception 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 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 90 private JPanel getTopPanel(final Exception 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 () { 97 public void actionPerformed(ActionEvent 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 () { 112 public void actionPerformed(ActionEvent e) { 113 setVisible(false); 114 dispose(); 115 } 116 }); 117 118 stackBtn.addActionListener(new ActionListener () { 119 public void actionPerformed(ActionEvent 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 149 public static void initExceptionDialogMessage(String code, Exception e) { 150 new ShowExceptionMessageDialog(code, e); 151 152 153 } 154 155 160 private void showExceptionMessage(String code, Exception e) { 161 ArrayList list = new ArrayList (); 162 fillCauses(e, list); 163 initComponents(e); 164 detalleLabel.setText(code); 165 Iterator iterator = list.iterator(); 166 while (iterator.hasNext()) { 167 String s = (String ) iterator.next(); 168 if(s !=null){ 169 textField.append(s+"\n"); 170 } 171 } 172 173 setVisible(true); 174 } 175 176 181 private void fillCauses(Throwable e, ArrayList list) { 182 list.add(e.getMessage()); 183 if (e instanceof CalipsoException) { 184 Throwable subE = e.getCause(); 185 if (subE != null && e != subE) { 186 fillCauses(subE, list); 187 } 188 } 189 } 190 } 191 | Popular Tags |