| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.BorderLayout; |
| 4 |
import java.awt.Component; |
| 5 |
import java.awt.Dimension; |
| 6 |
import java.awt.Frame; |
| 7 |
import java.awt.event.ActionEvent; |
| 8 |
import java.io.StringWriter; |
| 9 |
import java.io.PrintWriter; |
| 10 |
import javax.swing.AbstractAction; |
| 11 |
import javax.swing.BorderFactory; |
| 12 |
import javax.swing.Box; |
| 13 |
import javax.swing.JButton; |
| 14 |
import javax.swing.JComponent; |
| 15 |
import javax.swing.JDialog; |
| 16 |
import javax.swing.JLabel; |
| 17 |
import javax.swing.JPanel; |
| 18 |
import javax.swing.JScrollPane; |
| 19 |
import javax.swing.JTextArea; |
| 20 |
import javax.swing.SwingUtilities; |
| 21 |
import javax.swing.UIManager; |
| 22 |
import javax.swing.border.Border; |
| 23 |
|
| 24 |
/** |
| 25 |
* Dialog window which displays an error message, possibly with some |
| 26 |
* verbose details optionally visible. |
| 27 |
* |
| 28 |
* @author Mark Taylor |
| 29 |
* @since 5 Sep 2008 |
| 30 |
*/ |
| 31 |
public abstract class ErrorDialog extends JDialog { |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor. |
| 35 |
* |
| 36 |
* @param owner parent frame |
| 37 |
* @param title dialog title |
| 38 |
* @param summary short text string describing what's up |
| 39 |
*/ |
| 40 |
protected ErrorDialog( Frame owner, String title, String summary ) { |
| 41 |
super( owner, title == null ? "Error" : title, true ); |
| 42 |
setDefaultCloseOperation( DISPOSE_ON_CLOSE ); |
| 43 |
|
| 44 |
// Define buttons. |
| 45 |
final JPanel main = new JPanel( new BorderLayout() ); |
| 46 |
JButton disposeButton = new JButton(); |
| 47 |
JButton detailButton = new JButton(); |
| 48 |
final JComponent dataBox = new JPanel( new BorderLayout() ); |
| 49 |
dataBox.add( new JLabel( summary ) ); |
| 50 |
JComponent buttonBox = Box.createHorizontalBox(); |
| 51 |
|
| 52 |
// Populate main panel. |
| 53 |
Border gapBorder = BorderFactory.createEmptyBorder( 5, 5, 5, 5 ); |
| 54 |
JComponent iconLabel = |
| 55 |
new JLabel( UIManager.getIcon( "OptionPane.errorIcon" ) ); |
| 56 |
iconLabel.setBorder( gapBorder ); |
| 57 |
main.add( iconLabel, BorderLayout.WEST ); |
| 58 |
buttonBox.add( Box.createHorizontalGlue() ); |
| 59 |
buttonBox.add( disposeButton ); |
| 60 |
buttonBox.add( Box.createHorizontalStrut( 10 ) ); |
| 61 |
buttonBox.add( detailButton ); |
| 62 |
buttonBox.add( Box.createHorizontalGlue() ); |
| 63 |
dataBox.setBorder( gapBorder ); |
| 64 |
main.add( buttonBox, BorderLayout.SOUTH ); |
| 65 |
main.add( dataBox, BorderLayout.CENTER ); |
| 66 |
main.setBorder( gapBorder ); |
| 67 |
|
| 68 |
// Set button action for dismiss button. |
| 69 |
disposeButton.setAction( new AbstractAction( "OK" ) { |
| 70 |
public void actionPerformed( ActionEvent evt ) { |
| 71 |
dispose(); |
| 72 |
} |
| 73 |
} ); |
| 74 |
|
| 75 |
// Set button action for display detail button. |
| 76 |
detailButton.setAction( new AbstractAction( "Show Details" ) { |
| 77 |
public void actionPerformed( ActionEvent evt ) { |
| 78 |
JTextArea ta = new JTextArea(); |
| 79 |
ta.setLineWrap( false ); |
| 80 |
ta.setEditable( false ); |
| 81 |
ta.append( getDetailText() ); |
| 82 |
ta.setCaretPosition( 0 ); |
| 83 |
JScrollPane scroller = new JScrollPane( ta ); |
| 84 |
dataBox.removeAll(); |
| 85 |
dataBox.add( scroller ); |
| 86 |
Dimension size = dataBox.getPreferredSize(); |
| 87 |
size.height = Math.min( size.height, 300 ); |
| 88 |
size.width = Math.min( size.width, 500 ); |
| 89 |
dataBox.revalidate(); |
| 90 |
dataBox.setPreferredSize( size ); |
| 91 |
pack(); |
| 92 |
setEnabled( false ); |
| 93 |
} |
| 94 |
} ); |
| 95 |
getContentPane().add( main ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Supplies the text to be displayed in the detail panel. |
| 100 |
* |
| 101 |
* @return detail text |
| 102 |
*/ |
| 103 |
protected abstract String getDetailText(); |
| 104 |
|
| 105 |
/** |
| 106 |
* Pops up a window which shows the content of a exception. |
| 107 |
* |
| 108 |
* @param parent parent component |
| 109 |
* @param title window title |
| 110 |
* @param summary short text string |
| 111 |
* @param error throwable |
| 112 |
*/ |
| 113 |
public static void showError( Component parent, String title, |
| 114 |
String summary, final Throwable error ) { |
| 115 |
Frame fparent = parent == null |
| 116 |
? null |
| 117 |
: (Frame) SwingUtilities |
| 118 |
.getAncestorOfClass( Frame.class, parent ); |
| 119 |
|
| 120 |
JDialog dialog = new ErrorDialog( fparent, title, summary ) { |
| 121 |
protected String getDetailText() { |
| 122 |
StringWriter traceWriter = new StringWriter(); |
| 123 |
error.printStackTrace( new PrintWriter( traceWriter ) ); |
| 124 |
return traceWriter.toString(); |
| 125 |
} |
| 126 |
}; |
| 127 |
dialog.setLocationRelativeTo( parent ); |
| 128 |
dialog.pack(); |
| 129 |
dialog.setVisible( true ); |
| 130 |
} |
| 131 |
} |