1 18 package org.apache.batik.util.gui; 19 20 import java.awt.BorderLayout ; 21 import java.awt.Component ; 22 import java.awt.FlowLayout ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.Insets ; 26 import java.awt.event.ActionEvent ; 27 import java.io.PrintWriter ; 28 import java.io.StringWriter ; 29 import java.util.HashMap ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 import java.util.ResourceBundle ; 33 34 import javax.swing.AbstractAction ; 35 import javax.swing.Action ; 36 import javax.swing.BorderFactory ; 37 import javax.swing.JButton ; 38 import javax.swing.JComponent ; 39 import javax.swing.JDialog ; 40 import javax.swing.JLabel ; 41 import javax.swing.JOptionPane ; 42 import javax.swing.JPanel ; 43 import javax.swing.JScrollPane ; 44 import javax.swing.JSeparator ; 45 import javax.swing.JTextArea ; 46 47 import org.apache.batik.util.gui.resource.ActionMap; 48 import org.apache.batik.util.gui.resource.ButtonFactory; 49 import org.apache.batik.util.gui.resource.MissingListenerException; 50 import org.apache.batik.util.gui.resource.ResourceManager; 51 52 58 public class JErrorPane extends JPanel implements ActionMap { 59 60 63 protected final static String RESOURCES = 64 "org.apache.batik.util.gui.resources.JErrorPane"; 65 66 69 protected static ResourceBundle bundle; 70 71 74 protected static ResourceManager resources; 75 76 static { 77 bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault()); 78 resources = new ResourceManager(bundle); 79 } 80 81 84 protected String msg; 85 86 89 protected String stacktrace; 90 91 94 protected ButtonFactory bf = new ButtonFactory(bundle, this); 95 96 99 protected JComponent detailsArea; 100 101 104 protected JButton showDetailButton; 105 106 109 protected boolean isDetailShown = false; 110 111 114 protected JPanel subpanel; 115 116 122 public JErrorPane(Throwable th, int type) { 123 super(new GridBagLayout ()); 124 125 setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 126 127 listeners.put("ShowDetailButtonAction", new ShowDetailButtonAction()); 128 listeners.put("OKButtonAction", new OKButtonAction()); 129 this.msg = bundle.getString("Heading.text") + "\n\n" + th.getMessage(); 130 131 StringWriter writer = new StringWriter (); 132 th.printStackTrace(new PrintWriter (writer)); 133 writer.flush(); 134 this.stacktrace = writer.toString(); 135 136 ExtendedGridBagConstraints constraints = 137 new ExtendedGridBagConstraints(); 138 139 JTextArea msgArea = new JTextArea (); 140 msgArea.setText(msg); 141 msgArea.setColumns(50); 142 msgArea.setFont(new JLabel ().getFont()); 143 msgArea.setForeground(new JLabel ().getForeground()); 144 msgArea.setOpaque(false); 145 msgArea.setEditable(false); 146 msgArea.setLineWrap(true); 147 148 constraints.setWeight(0, 0); 149 constraints.anchor = GridBagConstraints.WEST; 150 constraints.fill = GridBagConstraints.NONE; 151 constraints.setGridBounds(0, 0, 1, 1); 152 add(msgArea, constraints); 153 154 constraints.setWeight(1, 0); 155 constraints.anchor = GridBagConstraints.CENTER; 156 constraints.fill = GridBagConstraints.HORIZONTAL; 157 constraints.setGridBounds(0, 1, 1, 1); 158 add(createButtonsPanel(), constraints); 159 160 JTextArea details = new JTextArea (); 161 msgArea.setColumns(50); 162 details.setText(stacktrace); 163 details.setEditable(false); 164 165 detailsArea = new JPanel (new BorderLayout (0, 10)); 166 detailsArea.add(new JSeparator (), BorderLayout.NORTH); 167 detailsArea.add(new JScrollPane (details), BorderLayout.CENTER); 168 169 subpanel = new JPanel (new BorderLayout ()); 170 171 constraints.insets = new Insets (10, 4, 4, 4); 172 constraints.setWeight(1, 1); 173 constraints.anchor = GridBagConstraints.CENTER; 174 constraints.fill = GridBagConstraints.BOTH; 175 constraints.setGridBounds(0, 2, 1, 1); 176 add(subpanel, constraints); 177 } 178 179 public JDialog createDialog(Component owner, String title) { 180 JDialog dialog = 181 new JDialog (JOptionPane.getFrameForComponent(owner), title); 182 dialog.getContentPane().add(this, BorderLayout.CENTER); 183 dialog.pack(); 184 return dialog; 185 } 186 187 protected JPanel createButtonsPanel() { 188 JPanel panel = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 189 190 showDetailButton = bf.createJButton("ShowDetailButton"); 191 panel.add(showDetailButton); 192 193 JButton okButton = bf.createJButton("OKButton"); 194 panel.add(okButton); 195 196 return panel; 197 } 198 199 202 protected Map listeners = new HashMap (); 203 204 210 public Action getAction(String key) throws MissingListenerException { 211 return (Action )listeners.get(key); 212 } 213 214 217 protected class OKButtonAction extends AbstractAction { 218 219 public void actionPerformed(ActionEvent evt) { 220 ((JDialog )getTopLevelAncestor()).dispose(); 221 } 222 } 223 224 227 protected class ShowDetailButtonAction extends AbstractAction { 228 229 public void actionPerformed(ActionEvent evt) { 230 if (isDetailShown) { 231 subpanel.remove(detailsArea); 232 isDetailShown = false; 233 showDetailButton.setText 234 (resources.getString("ShowDetailButton.text")); 235 } else { 236 subpanel.add(detailsArea, BorderLayout.CENTER); 237 showDetailButton.setText 238 (resources.getString("ShowDetailButton.text2")); 239 isDetailShown = true; 240 } 241 ((JDialog )getTopLevelAncestor()).pack(); 242 } 243 } 244 } 245 | Popular Tags |