Jogamp
exception reporter for CLInfo.
[jocl-demos.git] / src / com / jogamp / opencl / util / ExceptionReporter.java
1 /*
2  * Created on Sep 10, 2010, 6:12:26 PM
3  */
4
5 package com.jogamp.opencl.util;
6
7 import com.jogamp.common.JogampRuntimeException;
8 import java.awt.Component;
9 import java.awt.Desktop;
10 import java.awt.Frame;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
13 import java.io.IOException;
14 import java.io.PrintWriter;
15 import java.io.StringWriter;
16 import java.io.Writer;
17 import java.net.URI;
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
20 import javax.swing.GroupLayout;
21 import javax.swing.GroupLayout.Alignment;
22 import javax.swing.ImageIcon;
23 import javax.swing.JButton;
24 import javax.swing.JDialog;
25 import javax.swing.JEditorPane;
26 import javax.swing.JLabel;
27 import javax.swing.JScrollPane;
28 import javax.swing.JTextPane;
29 import javax.swing.LayoutStyle.ComponentPlacement;
30 import javax.swing.SwingConstants;
31 import javax.swing.WindowConstants;
32
33 /*
34  * Icon used in the dialog is from http://lazycrazy.deviantart.com/ released under the CC attibution license.
35  */
36
37 /**
38  *
39  * @author Michael Bien
40  */
41 public class ExceptionReporter extends JDialog {
42
43     public ExceptionReporter(Frame parent, boolean modal) {
44         super(parent, "Something went wrong", modal);
45         initComponents();
46     }
47
48     public static void appear(String string, JogampRuntimeException ex) {
49         ExceptionReporter reporter = new ExceptionReporter(null, true);
50         reporter.setLocationRelativeTo(null);
51         reporter.description.setText(string);
52
53         StringBuilder sb = new StringBuilder();
54         final Writer result = new StringWriter();
55         ex.printStackTrace(new PrintWriter(result));
56
57         sb.append("<pre>").append(result.toString()).append("</pre>");
58         reporter.messagePane.setText(sb.toString());
59         reporter.setVisible(true);
60
61     }
62
63     /** This method is called from within the constructor to
64      * initialize the form.
65      * WARNING: Do NOT modify this code. The content of this method is
66      * always regenerated by the Form Editor.
67      */
68     @SuppressWarnings("unchecked")
69     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
70     private void initComponents() {
71         JLabel icon = new JLabel();
72         JButton reportButton = new JButton();
73         JButton okButton = new JButton();
74         JScrollPane jScrollPane1 = new JScrollPane();
75         messagePane = new JEditorPane();
76         JScrollPane jScrollPane2 = new JScrollPane();
77         description = new JTextPane();
78
79         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
80
81         icon.setIcon(new ImageIcon(getClass().getResource("/com/jogamp/opencl/util/64_35.png"))); // NOI18N
82
83         reportButton.setText("not at all. I file a bug!");
84         reportButton.addActionListener(new ActionListener() {
85             public void actionPerformed(ActionEvent evt) {
86                 reportButtonActionPerformed(evt);
87             }
88         });
89
90         okButton.setText("understood");
91         okButton.addActionListener(new ActionListener() {
92             public void actionPerformed(ActionEvent evt) {
93                 okButtonActionPerformed(evt);
94             }
95         });
96
97         messagePane.setContentType("text/html");
98         messagePane.setEditable(false);
99         jScrollPane1.setViewportView(messagePane);
100
101         description.setEditable(false);
102         description.setFocusable(false);
103         jScrollPane2.setViewportView(description);
104
105         GroupLayout layout = new GroupLayout(getContentPane());
106         getContentPane().setLayout(layout);
107         layout.setHorizontalGroup(
108             layout.createParallelGroup(Alignment.LEADING)
109             .addGroup(layout.createSequentialGroup()
110                 .addContainerGap()
111                 .addGroup(layout.createParallelGroup(Alignment.LEADING)
112                     .addGroup(layout.createSequentialGroup()
113                         .addComponent(icon)
114                         .addGap(18, 18, 18)
115                         .addGroup(layout.createParallelGroup(Alignment.LEADING)
116                             .addComponent(jScrollPane2, GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
117                             .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)))
118                     .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
119                         .addComponent(okButton)
120                         .addPreferredGap(ComponentPlacement.UNRELATED)
121                         .addComponent(reportButton)))
122                 .addContainerGap())
123         );
124
125         layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {okButton, reportButton});
126
127         layout.setVerticalGroup(
128             layout.createParallelGroup(Alignment.LEADING)
129             .addGroup(layout.createSequentialGroup()
130                 .addContainerGap()
131                 .addGroup(layout.createParallelGroup(Alignment.LEADING)
132                     .addComponent(icon)
133                     .addGroup(layout.createSequentialGroup()
134                         .addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
135                         .addGap(4, 4, 4)
136                         .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)))
137                 .addPreferredGap(ComponentPlacement.RELATED)
138                 .addGroup(layout.createParallelGroup(Alignment.BASELINE)
139                     .addComponent(reportButton)
140                     .addComponent(okButton))
141                 .addContainerGap())
142         );
143
144         pack();
145     }// </editor-fold>//GEN-END:initComponents
146
147     private void reportButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_reportButtonActionPerformed
148         try {
149             Desktop.getDesktop().browse(URI.create("http://www.jogamp.org/bugzilla/"));
150         } catch (IOException ex) {
151             Logger.getLogger(ExceptionReporter.class.getName()).log(Level.SEVERE, null, ex);
152         }
153     }//GEN-LAST:event_reportButtonActionPerformed
154
155     private void okButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
156         dispose();
157     }//GEN-LAST:event_okButtonActionPerformed
158
159
160     // Variables declaration - do not modify//GEN-BEGIN:variables
161     private JTextPane description;
162     private JEditorPane messagePane;
163     // End of variables declaration//GEN-END:variables
164
165 }
http://JogAmp.org git info: FAQ, tutorial and man pages.