001package icy.gui.frame.error; 002 003import icy.gui.component.IcyTextField; 004import icy.preferences.GeneralPreferences; 005import icy.system.IcyExceptionHandler; 006import icy.util.StringUtil; 007 008import java.awt.BorderLayout; 009import java.awt.Color; 010import java.awt.Dimension; 011import java.awt.GridBagConstraints; 012import java.awt.GridBagLayout; 013import java.awt.Insets; 014import java.awt.event.MouseAdapter; 015import java.awt.event.MouseEvent; 016 017import javax.swing.Icon; 018import javax.swing.JButton; 019import javax.swing.JLabel; 020import javax.swing.JPanel; 021import javax.swing.JScrollPane; 022import javax.swing.JTextPane; 023import javax.swing.SwingConstants; 024import javax.swing.border.TitledBorder; 025import javax.swing.text.BadLocationException; 026import javax.swing.text.Document; 027import javax.swing.text.SimpleAttributeSet; 028import javax.swing.text.StyleConstants; 029 030public class ErrorReportPanel extends JPanel 031{ 032 /** 033 * 034 */ 035 private static final long serialVersionUID = -6672076887465746832L; 036 037 // GUI 038 JTextPane errorMessageTextPane; 039 JTextPane commentTextPane; 040 IcyTextField emailTextField; 041 JButton reportButton; 042 JButton closeButton; 043 JLabel label; 044 045 public ErrorReportPanel(Icon icon, String title, String message) 046 { 047 super(); 048 049 initialize(); 050 051 if (!StringUtil.isEmpty(title)) 052 label.setText(title); 053 if (icon != null) 054 label.setIcon(icon); 055 056 try 057 { 058 errorMessageTextPane.getStyledDocument().insertString(errorMessageTextPane.getStyledDocument().getLength(), 059 message, new SimpleAttributeSet()); 060 } 061 catch (BadLocationException e) 062 { 063 System.err.println("PluginErrorReport(...) error :"); 064 IcyExceptionHandler.showErrorMessage(e, true); 065 } 066 errorMessageTextPane.setCaretPosition(0); 067 068 final Document doc = commentTextPane.getDocument(); 069 070 try 071 { 072 SimpleAttributeSet attributes = new SimpleAttributeSet(); 073 StyleConstants.setItalic(attributes, true); 074 StyleConstants.setForeground(attributes, Color.GRAY); 075 doc.insertString(0, "Please type here your comment", attributes); 076 } 077 catch (BadLocationException e1) 078 { 079 080 } 081 082 commentTextPane.addMouseListener(new MouseAdapter() 083 { 084 // Displays a message at the beginning that 085 // disappears when first clicked 086 boolean firstClickDone = false; 087 088 @Override 089 public void mouseClicked(MouseEvent e) 090 { 091 if (!firstClickDone) 092 { 093 commentTextPane.setText(""); 094 095 SimpleAttributeSet attributes = new SimpleAttributeSet(); 096 StyleConstants.setItalic(attributes, false); 097 StyleConstants.setForeground(attributes, Color.BLACK); 098 try 099 { 100 doc.insertString(0, " ", attributes); 101 } 102 catch (BadLocationException e1) 103 { 104 } 105 106 firstClickDone = true; 107 } 108 } 109 }); 110 111 // set default email 112 emailTextField.setText(GeneralPreferences.getUserEmail()); 113 } 114 115 /** 116 * @wbp.parser.constructor 117 */ 118 ErrorReportPanel() 119 { 120 this(null, "Test", "An error occured"); 121 } 122 123 private void initialize() 124 { 125 // top 126 label = new JLabel("An error occured !", SwingConstants.CENTER); 127 128 // center 129 errorMessageTextPane = new JTextPane(); 130 errorMessageTextPane.setEditable(false); 131 errorMessageTextPane.setContentType("text/html"); 132 133 JScrollPane messageScrollPane = new JScrollPane(errorMessageTextPane); 134 135 JPanel messagePanel = new JPanel(); 136 messagePanel.setBorder(new TitledBorder(null, "Message", TitledBorder.LEADING, TitledBorder.TOP, null, null)); 137 messagePanel.setLayout(new BorderLayout(0, 0)); 138 messagePanel.add(messageScrollPane, BorderLayout.CENTER); 139 140 JPanel userPanel = new JPanel(); 141 userPanel.setBorder(new TitledBorder(null, "Comment", TitledBorder.LEADING, TitledBorder.TOP, null, null)); 142 userPanel.setLayout(new BorderLayout(0, 0)); 143 144 // buttons panel 145 reportButton = new JButton("Report"); 146 closeButton = new JButton("Close"); 147 148 JPanel buttonsPanel = new JPanel(); 149 buttonsPanel.add(reportButton); 150 buttonsPanel.add(closeButton); 151 152 // bottom 153 JPanel bottomPanel = new JPanel(); 154 bottomPanel.setLayout(new BorderLayout(0, 0)); 155 156 bottomPanel.add(userPanel, BorderLayout.CENTER); 157 158 JPanel commentPanel = new JPanel(); 159 userPanel.add(commentPanel, BorderLayout.CENTER); 160 commentPanel.setLayout(new BorderLayout(0, 0)); 161 162 // comment pane 163 commentTextPane = new JTextPane(); 164 commentTextPane.setEditable(true); 165 166 final JScrollPane scComment = new JScrollPane(commentTextPane); 167 commentPanel.add(scComment, BorderLayout.NORTH); 168 scComment.setPreferredSize(new Dimension(23, 60)); 169 scComment.setMinimumSize(new Dimension(23, 60)); 170 171 JPanel emailPanel = new JPanel(); 172 userPanel.add(emailPanel, BorderLayout.SOUTH); 173 GridBagLayout gbl_emailPanel = new GridBagLayout(); 174 gbl_emailPanel.columnWidths = new int[] {0, 0, 0}; 175 gbl_emailPanel.rowHeights = new int[] {0, 0}; 176 gbl_emailPanel.columnWeights = new double[] {0.0, 1.0, Double.MIN_VALUE}; 177 gbl_emailPanel.rowWeights = new double[] {0.0, Double.MIN_VALUE}; 178 emailPanel.setLayout(gbl_emailPanel); 179 180 JLabel lblEmail = new JLabel("Your email"); 181 GridBagConstraints gbc_lblEmail = new GridBagConstraints(); 182 gbc_lblEmail.insets = new Insets(0, 0, 0, 5); 183 gbc_lblEmail.anchor = GridBagConstraints.WEST; 184 gbc_lblEmail.gridx = 0; 185 gbc_lblEmail.gridy = 0; 186 emailPanel.add(lblEmail, gbc_lblEmail); 187 188 emailTextField = new IcyTextField(); 189 emailTextField.setToolTipText("You can enter your email so the developer can contact you if you wish"); 190 GridBagConstraints gbc_emailTextField = new GridBagConstraints(); 191 gbc_emailTextField.fill = GridBagConstraints.HORIZONTAL; 192 gbc_emailTextField.gridx = 1; 193 gbc_emailTextField.gridy = 0; 194 emailPanel.add(emailTextField, gbc_emailTextField); 195 emailTextField.setColumns(10); 196 bottomPanel.add(buttonsPanel, BorderLayout.SOUTH); 197 198 setLayout(new BorderLayout(0, 0)); 199 200 add(label, BorderLayout.NORTH); 201 add(messagePanel, BorderLayout.CENTER); 202 add(bottomPanel, BorderLayout.SOUTH); 203 } 204 205 /** 206 * Returns formatted report message (ready to send to web site). 207 * 208 * @throws BadLocationException 209 */ 210 public String getReportMessage() throws BadLocationException 211 { 212 final String email = emailTextField.getText(); 213 final Document commentDoc = commentTextPane.getDocument(); 214 final Document errorDoc = errorMessageTextPane.getDocument(); 215 String comment = commentDoc.getText(0, commentDoc.getLength()); 216 String result = ""; 217 218 if (!StringUtil.isEmpty(email)) 219 { 220 result += "Email: " + email + "\n"; 221 GeneralPreferences.setUserEmail(email); 222 } 223 if (!StringUtil.isEmpty(comment)) 224 result += "Comment:\n" + comment + "\n\n"; 225 226 result += errorDoc.getText(0, errorDoc.getLength()); 227 228 return result; 229 } 230 231}