1 package net.suberic.util.swing; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.io.*; 7 8 12 public class ExceptionDisplayPanel extends JPanel { 13 14 private static int S_INDENT = 10; 15 16 Exception mException; 18 19 JButton mButton; 21 22 26 public ExceptionDisplayPanel(String pButtonText, Exception pException) { 27 super(); 28 29 this.setLayout(new CardLayout()); 30 31 mException = pException; 32 33 mButton = new JButton(pButtonText); 34 35 Box buttonBox = Box.createHorizontalBox(); 36 buttonBox.add(Box.createHorizontalGlue()); 37 buttonBox.add("BUTTON", mButton); 38 buttonBox.add(Box.createHorizontalGlue()); 39 40 this.add("BUTTON", buttonBox); 41 42 mButton.addActionListener(new AbstractAction() { 43 44 public void actionPerformed(ActionEvent ae) { 45 showStackTrace(); 46 } 47 }); 48 } 49 50 53 public void showStackTrace() { 54 StringWriter exceptionWriter = new StringWriter(); 56 mException.printStackTrace(new PrintWriter(exceptionWriter)); 57 String exceptionString = exceptionWriter.toString(); 58 59 64 68 72 Window windowAncestor = SwingUtilities.getWindowAncestor(this); 73 if (windowAncestor instanceof JDialog) { 74 JDialog dialog = (JDialog) windowAncestor; 75 Window owner = dialog.getOwner(); 76 if (owner != null) 77 windowAncestor = owner; 78 } 79 Component jsp = createMessageComponent(exceptionString,windowAncestor ); 80 this.add("EXCEPTION", jsp); 81 82 83 ((CardLayout) getLayout()).show(this, "EXCEPTION"); 84 Dimension currentMinimum = getMinimumSize(); 85 this.setMinimumSize(new Dimension(Math.max(currentMinimum.width, 150), Math.max(currentMinimum.height, 100))); 86 87 JInternalFrame parentIntFrame = null; 88 try { 89 parentIntFrame = (JInternalFrame) SwingUtilities.getAncestorOfClass(Class.forName("javax.swing.JInternalFrame"), this); 90 } catch (Exception e) { 91 } 92 if (parentIntFrame != null) { 93 JDesktopPane jdp = parentIntFrame.getDesktopPane(); 95 System.err.println("got jdp."); 96 if (jdp != null) { 97 System.err.println("jdp is not null."); 98 Point iFrameLocation = parentIntFrame.getLocation(); 99 Dimension jdpSize = jdp.getSize(); 100 System.err.println("iFrameLocation = " + iFrameLocation + ", jdpSize = " + jdpSize + ", parentIntFrame.getMinimumSize() = " + parentIntFrame.getMinimumSize()); 101 parentIntFrame.setMaximumSize(new Dimension(Math.max(parentIntFrame.getMinimumSize().width, jdpSize.width - iFrameLocation.x), Math.max(parentIntFrame.getMinimumSize().height, jdpSize.height - iFrameLocation.y))); 102 parentIntFrame.setPreferredSize(new Dimension(Math.max(parentIntFrame.getMinimumSize().width, jdpSize.width - iFrameLocation.x), Math.max(parentIntFrame.getMinimumSize().height, jdpSize.height - iFrameLocation.y))); 103 System.err.println("parentIntFrame.maximumSize = " + parentIntFrame.getMaximumSize()); 104 } 105 parentIntFrame.pack(); 106 } else { 108 Window parentWindow = SwingUtilities.getWindowAncestor(this); 109 if (parentWindow != null) { 110 if (parentWindow instanceof JDialog) { 111 JDialog parentDialog = (JDialog) parentWindow; 112 parentDialog.setSize(parentDialog.getPreferredSize()); 113 Window owner = parentDialog.getOwner(); 114 if (owner != null) { 115 Point ownerPoint = owner.getLocationOnScreen(); 116 Dimension ownerSize = owner.getSize(); 117 } 118 } 119 parentWindow.pack(); 120 } 122 } 123 } 124 125 128 public Dimension calculateDisplaySize(Component parentComponent, Component displayComponent) { 129 Dimension parentSize = parentComponent.getSize(); 131 int maxWidth = Math.max(30, (int) (parentSize.width * 0.8)); 133 int maxHeight = Math.max(30, (int) (parentSize.height * 0.8)); 134 135 Dimension displayPrefSize = displayComponent.getPreferredSize(); 136 137 int newWidth = Math.min(maxWidth, displayPrefSize.width); 138 int newHeight = Math.min(maxHeight, displayPrefSize.height); 139 return new Dimension(newWidth +5, newHeight+5); 140 } 141 142 146 public Component createMessageComponent(String message, Component parentComponent) { 147 Component labelComponent = createLabel(message); 148 Dimension displaySize = calculateDisplaySize(parentComponent, labelComponent); 149 JScrollPane jsp = new JScrollPane(labelComponent); 150 JScrollBar jsb = jsp.getVerticalScrollBar(); 152 if (jsb != null) { 153 displaySize = new Dimension(displaySize.width + jsb.getPreferredSize().width, displaySize.height); 154 } else { 155 jsb = new JScrollBar(JScrollBar.VERTICAL); 156 displaySize = new Dimension(displaySize.width + jsb.getPreferredSize().width, displaySize.height); 157 158 } 159 jsp.setPreferredSize(displaySize); 160 return jsp; 161 } 162 163 166 public Component createLabel(String s) { 167 Container c = Box.createVerticalBox(); 168 169 addMessageComponents(c, s, 160); 170 171 return c; 172 } 173 174 private void addMessageComponents(Container c, String s, int maxll) { 175 int nl = -1; 177 int nll = 0; 178 179 if ((nl = s.indexOf("\r\n")) >= 0) { 180 nll = 2; 181 } else if ((nl = s.indexOf('\n')) >= 0) { 182 nll = 1; 183 } 184 185 if (nl >= 0) { 186 if (nl == 0) { 188 JPanel breakPanel = new JPanel() { 189 public Dimension getPreferredSize() { 190 Font f = getFont(); 191 192 if (f != null) { 193 return new Dimension(1, f.getSize() + 2); 194 } 195 return new Dimension(0, 0); 196 } 197 }; 198 breakPanel.setName("OptionPane.break"); 199 c.add(breakPanel); 200 } else { 201 addMessageComponents(c, s.substring(0, nl), maxll); 202 } 203 addMessageComponents(c, s.substring(nl + nll), maxll); 204 205 212 } else { 213 JLabel label; 214 label = new JLabel(s, JLabel.LEADING ); 215 label.setName("OptionPane.label"); 216 c.add(label); 217 } 218 } 219 220 224 private void burstStringInto(Container c, String d, int maxll) { 225 int len = d.length(); 227 if (len <= 0) 228 return; 229 if (len > maxll) { 230 int p = d.lastIndexOf(' ', maxll); 231 if (p <= 0) 232 p = d.indexOf(' ', maxll); 233 if (p > 0 && p < len) { 234 burstStringInto(c, d.substring(0, p), maxll); 235 burstStringInto(c, d.substring(p + 1), maxll); 236 return; 237 } 238 } 239 JLabel label = new JLabel(d, JLabel.LEFT); 240 label.setName("OptionPane.label"); 241 c.add(label); 242 } 243 244 } 245 246 | Popular Tags |