1 package com.ca.commons.cbutil; 2 3 import javax.swing.*; 4 import javax.swing.border.TitledBorder ; 5 import java.awt.*; 6 import java.awt.event.ActionEvent ; 7 import java.awt.event.ActionListener ; 8 import java.io.PrintWriter ; 9 import java.io.StringWriter ; 10 11 15 16 public class CBErrorWin extends JDialog 17 { 18 19 String msg; 20 String error; 21 String title; 22 boolean haveErrorMsg; 23 CBButton OK, Details; 24 CBPanel display; 25 Exception exception; 26 27 39 40 public CBErrorWin(Dialog owner, String Msg, Exception e) 41 { 42 super(owner); 43 title = CBIntText.get("Error Encountered"); 44 commonConstructor(Msg, e); 45 } 46 47 59 60 public CBErrorWin(Frame owner, String Msg, Exception e) 61 { 62 super(owner); 63 title = CBIntText.get("Error Encountered"); 64 commonConstructor(Msg, e); 65 } 66 67 68 75 76 public CBErrorWin(Frame owner, String Msg, String msgTitle) 77 { 78 super(owner); 79 title = msgTitle; 80 commonConstructor(Msg, null); 81 } 82 83 84 92 93 public void commonConstructor(String Msg, Exception e) 94 { 95 setModal(true); 96 97 exception = e; 98 99 setTitle(CBIntText.get(title)); 100 101 msg = (Msg == null) ? CBIntText.get("No Message Given") : Msg; 102 103 haveErrorMsg = (e != null); 104 error = (haveErrorMsg) ? e.toString() : CBIntText.get("No specific information"); 105 106 JScrollPane scrollPane = new JScrollPane(makeTextArea(msg)); 107 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 109 display = new CBPanel(); 110 display.makeHeavy(); 111 112 display.addLine(scrollPane); 113 114 display.makeLight(); 115 JPanel buttons = new JPanel(); 116 117 buttons.add(OK = new CBButton(CBIntText.get("OK"), CBIntText.get("Click to close error window."))); 118 119 if (haveErrorMsg != false) buttons.add(Details = new CBButton(CBIntText.get("Details"), CBIntText.get("Click to display the full error message."))); 121 122 display.add(buttons); 123 124 OK.addActionListener(new ActionListener () 125 { 126 public void actionPerformed(ActionEvent e) 127 { 128 setVisible(false); 129 dispose(); 130 } 131 }); 132 133 if (haveErrorMsg != false) { 135 Details.addActionListener(new ActionListener () 136 { 137 public void actionPerformed(ActionEvent e) 138 { 139 JScrollPane scrollPane2 = new JScrollPane(makeTextArea(msg)); 140 final JScrollPane newPane = new JScrollPane(makeTextArea(error)); 141 142 TitledBorder border = new TitledBorder (CBIntText.get("error details")); 143 newPane.setBorder(border); 144 145 CBErrorWin.this.getContentPane().remove(display); 146 147 display = new CBPanel(); 148 149 display.makeHeavy(); 150 151 display.addLine(scrollPane2); 152 display.addLine(newPane); 153 154 display.makeLight(); 155 156 159 160 JPanel buttons2 = new JPanel(); 161 162 if (exception != null) 163 { 164 JButton StackTrace = new JButton(CBIntText.get("Print Stack")); 165 StackTrace.setToolTipText(CBIntText.get("Prints a stack trace to the console (if active)")); 166 StackTrace.addActionListener(new ActionListener () 167 { 168 public void actionPerformed(ActionEvent e) 169 { 170 StringWriter sw = new StringWriter (); 171 exception.printStackTrace(new PrintWriter (sw)); 172 String trace = sw.toString(); 173 newPane.setViewportView(makeTextArea(trace)); 174 exception.printStackTrace(); } 176 }); 177 buttons2.add(StackTrace); 178 } 179 180 buttons2.add(OK); 181 display.add(buttons2); 182 183 CBErrorWin.this.setContentPane(display); 184 CBErrorWin.this.setSize(new Dimension(getWidth(), getHeight() + 100)); 185 CBErrorWin.this.setVisible(true); 186 } 187 }); 188 } 189 190 192 display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), "enter"); 193 display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escape"); 194 display.getActionMap().put("enter", new MyAction(CBAction.ENTER)); 195 display.getActionMap().put("escape", new MyAction(CBAction.ESCAPE)); 196 197 setContentPane(display); 198 setSize(new Dimension(400, 150)); 199 CBUtility.center(this, getOwner()); 200 setVisible(true); 201 } 202 203 204 216 private class MyAction extends CBAction 217 { 218 223 public MyAction(int key) 224 { 225 super(key); 226 } 227 228 233 public void actionPerformed(ActionEvent e) 234 { 235 setVisible(false); 236 dispose(); 237 } 238 } 239 240 241 248 249 protected JTextArea makeTextArea(String text) 250 { 251 JTextArea a = new JTextArea(text); 252 253 a.setBackground(CBErrorWin.this.getBackground()); 254 a.setEnabled(true); a.setDisabledTextColor(Color.black); a.setLineWrap(true); a.setWrapStyleWord(true); 259 260 265 a.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"), "enter"); a.getActionMap().put("enter", new MyAction(CBAction.ENTER)); 267 268 return a; 269 } 270 } | Popular Tags |