1 19 20 package org.netbeans.modules.exceptions; 21 22 import java.awt.Container ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.ComponentAdapter ; 28 import java.awt.event.ComponentEvent ; 29 import java.io.IOException ; 30 import java.io.StringWriter ; 31 import java.util.Date ; 32 import java.util.LinkedList ; 33 import java.util.logging.Level ; 34 import java.util.logging.LogRecord ; 35 import java.util.logging.Logger ; 36 import java.util.logging.SimpleFormatter ; 37 import javax.swing.ImageIcon ; 38 import javax.swing.JButton ; 39 import javax.swing.JFrame ; 40 import javax.swing.JLabel ; 41 import javax.swing.JScrollPane ; 42 import javax.swing.JTextPane ; 43 import javax.swing.UIManager ; 44 import org.jdesktop.layout.GroupLayout; 45 import org.jdesktop.layout.GroupLayout.ParallelGroup; 46 import org.jdesktop.layout.GroupLayout.SequentialGroup; 47 import org.jdesktop.layout.LayoutStyle; 48 import org.netbeans.modules.exceptions.data.Record; 49 import org.openide.DialogDisplayer; 50 import org.openide.NotifyDescriptor; 51 import org.openide.util.NbBundle; 52 import org.openide.windows.WindowManager; 53 57 public class NotifyDialog extends JFrame { 58 private static final String imgSource ="org/netbeans/modules/exceptions/img.png"; private static ReportDialog rDialog; 60 private Dimension NO_DETAILS_DIMENSION= new Dimension (450, 250); 61 private Dimension DETAILS_DIMENSION= new Dimension (700, 500); 62 64 java.util.ResourceBundle bundle = NbBundle.getBundle(NotifyDialog.class); 65 66 private final JButton okButton = new JButton (bundle.getString("CTL_OK"));; 67 private final JButton nextButton = new JButton (bundle.getString("CTL_NextException")); 68 private final JButton previousButton = new JButton (bundle.getString("CTL_PreviousException")); 69 private final JButton hideDetailsButton = new JButton (bundle.getString("CTL_Exception_Hide_Details")); 70 private final JButton showDetailsButton = new JButton (bundle.getString("CTL_Exception_Show_Details")); 71 private final JButton reportButton = new JButton (bundle.getString("CTL_Report_Exception")); 72 private final JTextPane text = new JTextPane () { 73 public boolean getScrollableTracksViewportWidth() { 74 return false; 75 } 76 }; 77 private final JScrollPane scrollPane = new JScrollPane (text); 78 private final JLabel icon = new JLabel (); 79 private final ActionListener notifyListener = new NotifyListener(); 80 private boolean details; private LinkedList <LinkedList <LogRecord >> throwables; private int activ = 0; 84 public NotifyDialog() { 85 super(); 86 initializeComponents(); 87 } 88 89 private void initializeComponents(){ 90 getAccessibleContext().setAccessibleName(bundle.getString("Dialog_Accessible_name")); 91 getAccessibleContext().setAccessibleDescription(bundle.getString("Dialog_Accessible_description")); 92 setTitle(bundle.getString("CTL_Title_Exception")); 93 icon.setIcon(new ImageIcon (org.openide.util.Utilities.loadImage(imgSource))); 94 okButton.addActionListener(notifyListener); 95 okButton.setMnemonic(bundle.getString("CTL_OK").charAt(0)); 96 nextButton.addActionListener(notifyListener); 97 nextButton.setMnemonic(bundle.getString("CTL_NextException").charAt(0)); 98 previousButton.addActionListener(notifyListener); 99 previousButton.setMnemonic(bundle.getString("CTL_PreviousException").charAt(2)); 100 hideDetailsButton.addActionListener(notifyListener); 101 hideDetailsButton.setMnemonic(bundle.getString("CTL_Exception_Hide_Details").charAt(0)); 102 showDetailsButton.addActionListener(notifyListener); 103 showDetailsButton.setMnemonic(bundle.getString("CTL_Exception_Show_Details").charAt(0)); 104 reportButton.addActionListener(notifyListener); 105 reportButton.setMnemonic(bundle.getString("CTL_Report_Exception").charAt(0)); 106 text.setEditable(false); 107 addComponentListener(new ComponentAdapter () { 109 public void componentResized(ComponentEvent event) { 110 if (details) DETAILS_DIMENSION = getSize(); 111 else NO_DETAILS_DIMENSION = getSize(); 112 } 113 }); 114 Font font = new Font ("Monospaced", Font.PLAIN, text.getFont().getSize()); text.setFont(font); 116 text.setForeground(UIManager.getColor("Label.foreground")); text.setBackground(UIManager.getColor("Label.background")); Runnable runable = new Runnable (){ 119 public void run(){ 120 setLocationRelativeTo(WindowManager.getDefault().getMainWindow()); 121 } 122 }; 123 WindowManager.getDefault().invokeWhenUIReady(runable); 124 update(); 125 } 126 127 private void fillInText(){ 128 if (activ!= 0){ 129 LogRecord shown = throwables.get(activ - 1).getFirst(); 130 StringWriter swr = new StringWriter (); 131 if (details) { 132 SimpleFormatter formatter = new SimpleFormatter (); 133 swr.write(formatter.format(shown)); 134 swr.write('\n'); 135 } else { 143 swr.write(new Date (shown.getMillis()).toString() + '\n'); 144 swr.write(bundle.getString("Severity") + shown.getLevel().toString() + '\n'); 145 swr.write(shown.getThrown().getClass().toString() + '\n'); 146 } 147 text.setText(swr.toString()); 148 try { 149 swr.close(); 150 } catch (IOException ex) { 151 } 153 } 154 } 155 156 private void update(){ 157 fillInText(); 158 if ((throwables!= null)&&(throwables.size()>0)) reportButton.setEnabled(true);else reportButton.setEnabled(false); 159 Container contentPane = getContentPane(); 160 contentPane.removeAll(); 162 GroupLayout layout = new GroupLayout(contentPane); 163 ParallelGroup horGroup = layout.createParallelGroup(GroupLayout.LEADING); 164 ParallelGroup verGroup =layout.createParallelGroup(GroupLayout.LEADING); 165 166 SequentialGroup baseLine = layout.createSequentialGroup(); 169 if (!details) baseLine.add(showDetailsButton); else baseLine.add(hideDetailsButton); 170 baseLine.addPreferredGap(LayoutStyle.RELATED, 106, Short.MAX_VALUE); 171 baseLine.add(reportButton); 172 baseLine.addPreferredGap(LayoutStyle.RELATED); 173 if (activ > 1){ 174 baseLine.add(previousButton) 175 .addPreferredGap(LayoutStyle.RELATED); 176 } 177 if ((throwables != null)&&(throwables.size() > activ)){ 178 baseLine.add(nextButton) 179 .addPreferredGap(LayoutStyle.RELATED); 180 } 181 baseLine.add(okButton); 182 183 SequentialGroup textGroup = layout.createSequentialGroup(); 185 if (!details) textGroup.add(icon, 34, 34, 34); 186 textGroup.addPreferredGap(LayoutStyle.RELATED) 187 .add(scrollPane, GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE); 188 189 190 horGroup.add(GroupLayout.TRAILING, layout.createSequentialGroup() 191 .addContainerGap() 192 .add(layout.createParallelGroup(GroupLayout.TRAILING) 193 .add(GroupLayout.LEADING, textGroup) 194 .add(baseLine)) 195 .addContainerGap()); 196 197 198 ParallelGroup textVer = layout.createParallelGroup(GroupLayout.LEADING) 201 .add(scrollPane, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE); 202 if (!details) textVer.add(icon, 42, 42, 42); 203 204 ParallelGroup buttons = layout.createParallelGroup(GroupLayout.BASELINE); 206 if (!details) buttons.add(showDetailsButton); else buttons.add(hideDetailsButton); 207 buttons.add(reportButton); 208 if (activ > 1) buttons.add(previousButton); 209 if ((throwables != null)&&(throwables.size() > activ)) buttons.add(nextButton); 210 buttons.add(okButton); 211 212 verGroup.add(GroupLayout.TRAILING, layout.createSequentialGroup() 213 .addContainerGap() 214 .add(textVer) 215 .addPreferredGap(LayoutStyle.RELATED) 216 .add(buttons) 217 .addContainerGap()); 218 219 if (!details)setPreferredSize(NO_DETAILS_DIMENSION); 220 else setPreferredSize(DETAILS_DIMENSION); 221 222 layout.setHorizontalGroup(horGroup); 223 layout.setVerticalGroup(verGroup); 224 contentPane.setLayout(layout); 225 pack(); 226 } 227 228 public void notify(LinkedList <LinkedList <LogRecord >> logRecords){ 229 throwables = logRecords; 230 setActiv(throwables.size()); 231 update(); 232 } 233 234 private class NotifyListener implements ActionListener { 236 NotifyListener(){ 237 } 238 239 public void actionPerformed(ActionEvent event) { 240 if (getReportDialog().isVisible()) getReportDialog().setVisible(true); 241 else if (event.getSource().equals(okButton)){ 242 dispose(); 243 }else if (event.getSource().equals(showDetailsButton)){ 244 details = true; 245 update(); 246 }else if (event.getSource().equals(hideDetailsButton)){ 247 details = false; 248 update(); 249 }else if (event.getSource().equals(nextButton)){ 250 if (activ < throwables.size()) setActiv(activ+1); 251 update(); 252 }else if(event.getSource().equals(previousButton)){ 253 if (activ > 1) setActiv(activ-1); 254 update(); 255 }else if(event.getSource().equals(reportButton)){ 256 CheckIssue check = new CheckIssue(); 257 int exists = -1; 258 try{ 259 exists = check.exists(throwables.get(activ-1).getFirst().getThrown()); 260 Logger.getLogger(NotifyDialog.class.getName()).log(Level.FINEST, "ISSUE_EXISTS: "+Integer.toString(exists)); }catch(IOException e){ 262 Logger.getLogger(NotifyDialog.class.getName()).log(Level.WARNING, "CONNECTION REFUSED"); exists = -1; 264 } 265 if (exists != -1){ 267 String message = NbBundle.getMessage(NotifyDialog.class, "ALREADY_REPORTED"); 268 message = message.concat(Integer.toString(exists)); 269 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.INFORMATION_MESSAGE)); 270 }else{ 271 java.awt.EventQueue.invokeLater(new Runnable () { 272 public void run() { 273 getReportDialog().setVisible(true); 274 } 275 }); 276 } 277 } 278 279 } 280 } 281 282 private synchronized ReportDialog getReportDialog(){ 283 if (rDialog == null){ 284 rDialog = new ReportDialog(); 285 rDialog.setLocationRelativeTo(this); 286 } 287 return rDialog; 288 } 289 290 private void setActiv(int _activ){ 291 if ((_activ <= throwables.size())&&(_activ > 0)) activ = _activ; 292 Sender.setTransport(throwables.get(activ-1)); 293 } 294 295 297 307 } 308 | Popular Tags |