KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > ErrorDebugDialog


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.modeler.dialog;
58
59 import java.awt.BorderLayout JavaDoc;
60 import java.awt.Container JavaDoc;
61 import java.awt.HeadlessException JavaDoc;
62 import java.awt.event.ActionEvent JavaDoc;
63 import java.awt.event.ActionListener JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.io.PrintWriter JavaDoc;
66 import java.io.StringWriter JavaDoc;
67
68 import javax.swing.BorderFactory JavaDoc;
69 import javax.swing.JButton JavaDoc;
70 import javax.swing.JEditorPane JavaDoc;
71 import javax.swing.JPanel JavaDoc;
72 import javax.swing.JScrollPane JavaDoc;
73 import javax.swing.JTextArea JavaDoc;
74
75 import org.objectstyle.cayenne.modeler.Application;
76 import org.objectstyle.cayenne.modeler.CayenneModelerFrame;
77 import org.objectstyle.cayenne.modeler.util.CayenneDialog;
78 import org.objectstyle.cayenne.modeler.util.PanelFactory;
79 import org.objectstyle.cayenne.util.Util;
80 import org.scopemvc.util.UIStrings;
81
82 /**
83  * Displays CayenneModeler exceptions and warning messages.
84  *
85  * @author Andrei Adamchik
86  */

87 public class ErrorDebugDialog extends CayenneDialog implements ActionListener JavaDoc {
88     protected JButton JavaDoc close;
89     protected JButton JavaDoc showHide;
90     protected JTextArea JavaDoc exText = new JTextArea JavaDoc();
91     protected JPanel JavaDoc exPanel;
92     protected Throwable JavaDoc throwable;
93     protected boolean detailed;
94
95     public static void guiException(Throwable JavaDoc th) {
96         if (th != null) {
97             th.printStackTrace();
98         }
99
100         ErrorDebugDialog dialog =
101             new ErrorDebugDialog(Application.getFrame(), "CayenneModeler Error", th, true);
102         dialog.setVisible(true);
103     }
104
105     public static void guiWarning(Throwable JavaDoc th, String JavaDoc message) {
106         if (th != null) {
107             th.printStackTrace();
108         }
109
110         WarningDialog dialog = new WarningDialog(Application.getFrame(), message, th, false);
111         dialog.setVisible(true);
112     }
113
114     /**
115      * Constructor for ErrorDebugDialog.
116      */

117     protected ErrorDebugDialog(
118     CayenneModelerFrame owner,
119         String JavaDoc title,
120         Throwable JavaDoc throwable,
121         boolean detailed)
122         throws HeadlessException JavaDoc {
123
124         super(owner, title, true);
125
126         setThrowable(Util.unwindException(throwable));
127         setDetailed(detailed);
128         init();
129     }
130
131     protected void init() {
132         setResizable(false);
133
134         Container JavaDoc pane = this.getContentPane();
135         pane.setLayout(new BorderLayout JavaDoc());
136
137         // info area
138
JEditorPane JavaDoc infoText = new JEditorPane JavaDoc("text/html", infoHTML());
139         infoText.setBackground(pane.getBackground());
140         infoText.setEditable(false);
141         // popup hyperlinks
142
infoText.addHyperlinkListener(this);
143
144         JPanel JavaDoc infoPanel = new JPanel JavaDoc();
145         infoPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
146         infoPanel.add(infoText);
147         pane.add(infoPanel, BorderLayout.NORTH);
148
149         // exception area
150
if (throwable != null) {
151             exText.setEditable(false);
152             exText.setLineWrap(true);
153             exText.setWrapStyleWord(true);
154             exText.setRows(16);
155             exText.setColumns(40);
156             JScrollPane JavaDoc exScroll =
157                 new JScrollPane JavaDoc(
158                     exText,
159                     JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
160                     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
161             exPanel = new JPanel JavaDoc();
162             exPanel.setLayout(new BorderLayout JavaDoc());
163             exPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
164             exPanel.add(exScroll, BorderLayout.CENTER);
165
166             // buttons
167
showHide = new JButton JavaDoc("");
168             showHide.addActionListener(this);
169             if (isDetailed()) {
170                 showDetails();
171             } else {
172                 hideDetails();
173             }
174         }
175
176         close = new JButton JavaDoc("Close");
177         close.addActionListener(this);
178
179         JButton JavaDoc[] buttons = (showHide != null) ? new JButton JavaDoc[] { showHide, close }
180         : new JButton JavaDoc[] { close };
181         pane.add(PanelFactory.createButtonPanel(buttons), BorderLayout.SOUTH);
182
183         // prepare to display
184
this.pack();
185         this.centerWindow();
186     }
187
188     protected String JavaDoc infoHTML() {
189         String JavaDoc bugreportURL = UIStrings.get("cayenne.bugreport.url");
190         return "<b><font face='Arial,Helvetica' size='+1' color='red'>"
191             + getTitle()
192             + "</font></b><br>"
193             + "<font face='Arial,Helvetica' size='-1'>Please copy the message below and "
194             + "report this error by going to <br>"
195             + "<a HREF='"
196             + bugreportURL
197             + "'>"
198             + bugreportURL
199             + "</a></font>";
200     }
201
202     protected void setThrowable(Throwable JavaDoc throwable) {
203         this.throwable = throwable;
204
205         String JavaDoc text = null;
206         if (throwable != null) {
207             StringWriter JavaDoc str = new StringWriter JavaDoc();
208             PrintWriter JavaDoc out = new PrintWriter JavaDoc(str);
209
210             // first add extra diagnostics
211
String JavaDoc version = UIStrings.get("cayenne.version");
212             version = (version != null) ? version : "(unknown)";
213
214             String JavaDoc buildDate = UIStrings.get("cayenne.build.date");
215             buildDate = (buildDate != null) ? buildDate : "(unknown)";
216
217             out.println("CayenneModeler Info");
218             out.println("Version: " + version);
219             out.println("Build Date: " + buildDate);
220             out.println("Exception: ");
221             out.println("=================================");
222             buildStackTrace(out, throwable);
223
224             try {
225                 out.close();
226                 str.close();
227             } catch (IOException JavaDoc ioex) {
228                 // this should never happen
229
}
230             text = str.getBuffer().toString();
231         }
232
233         exText.setText(text);
234     }
235
236     protected void buildStackTrace(PrintWriter JavaDoc out, Throwable JavaDoc th) {
237         if (th == null) {
238             return;
239         }
240
241         th.printStackTrace(out);
242
243         Throwable JavaDoc cause = th.getCause();
244         if (cause != null) {
245             out.println("Caused by:");
246             buildStackTrace(out, cause);
247         }
248     }
249
250     public void actionPerformed(ActionEvent JavaDoc e) {
251         if (e.getSource() == close) {
252             this.dispose();
253         } else if (e.getSource() == showHide) {
254             if (isDetailed()) {
255                 hideDetails();
256             } else {
257                 showDetails();
258             }
259             this.pack();
260             this.centerWindow();
261         }
262     }
263
264     protected void hideDetails() {
265         getContentPane().remove(exPanel);
266         showHide.setText("Show Details");
267         setDetailed(false);
268     }
269
270     protected void showDetails() {
271         getContentPane().add(exPanel, BorderLayout.CENTER);
272         showHide.setText("Hide Details");
273         setDetailed(true);
274     }
275
276     /**
277      * Returns the detailed.
278      * @return boolean
279      */

280     public boolean isDetailed() {
281         return detailed;
282     }
283
284     /**
285      * Sets the detailed.
286      * @param detailed The detailed to set
287      */

288     public void setDetailed(boolean detailed) {
289         this.detailed = detailed;
290     }
291 }
292
Popular Tags