KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > UIExceptions


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.concurrent.Callable JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.LogRecord JavaDoc;
32 import org.openide.util.Exceptions;
33
34 /** Don't use this class outside fo core, copy its impl, if you really think
35  * you need it. Prefered way is to use DialogDisplayer.notifyLater(...);
36  *
37  * @author Jaroslav Tulach
38  */

39 public final class UIExceptions {
40     
41     /**
42      * Creates a new instance of UIExceptions
43      */

44     private UIExceptions() {
45     }
46     
47     public static void annotateUser(
48         Throwable JavaDoc t,
49         String JavaDoc msg,
50         String JavaDoc locMsg,
51         Throwable JavaDoc stackTrace,
52         Date JavaDoc date
53     ) {
54         AnnException ex = AnnException.findOrCreate(t, true);
55         LogRecord JavaDoc rec = new LogRecord JavaDoc(OwnLevel.USER, msg);
56         if (stackTrace != null) {
57             rec.setThrown(stackTrace);
58         }
59         ex.addRecord(rec);
60         
61         if (locMsg != null) {
62             Exceptions.attachLocalizedMessage(t, locMsg);
63         }
64     }
65     private static final class OwnLevel extends Level JavaDoc {
66         public static final Level JavaDoc USER = new OwnLevel("USER", 1973); // NOI18N
67

68         private OwnLevel(String JavaDoc s, int i) {
69             super(s, i);
70         }
71     } // end of UserLevel
72
private static final class AnnException extends Exception JavaDoc implements Callable JavaDoc<LogRecord JavaDoc[]> {
73         private List JavaDoc<LogRecord JavaDoc> records;
74
75         public String JavaDoc getMessage() {
76             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
77             String JavaDoc sep = "";
78             for (LogRecord JavaDoc r : records) {
79                 if (r.getMessage() != null) {
80                     sb.append(sep);
81                     sb.append(r.getMessage());
82                     sep = "\n";
83                 }
84             }
85             return sb.toString();
86         }
87
88         static AnnException findOrCreate(Throwable JavaDoc t, boolean create) {
89             if (t instanceof AnnException) {
90                 return (AnnException)t;
91             }
92             if (t.getCause() == null) {
93                 if (create) {
94                     t.initCause(new AnnException());
95                 }
96                 return (AnnException)t.getCause();
97             }
98             return findOrCreate(t.getCause(), create);
99         }
100
101         private AnnException() {
102         }
103
104         public synchronized void addRecord(LogRecord JavaDoc rec) {
105             if (records == null) {
106                 records = new ArrayList JavaDoc<LogRecord JavaDoc>();
107             }
108             records.add(rec);
109         }
110
111         public LogRecord JavaDoc[] call() {
112             List JavaDoc<LogRecord JavaDoc> r = records;
113             LogRecord JavaDoc[] empty = new LogRecord JavaDoc[0];
114             return r == null ? empty : r.toArray(empty);
115         }
116
117         public void printStackTrace(PrintStream JavaDoc s) {
118             super.printStackTrace(s);
119             logRecords(s);
120         }
121
122         public void printStackTrace(PrintWriter JavaDoc s) {
123             super.printStackTrace(s);
124             logRecords(s);
125         }
126
127         public void printStackTrace() {
128             printStackTrace(System.err);
129         }
130
131         private void logRecords(Appendable JavaDoc a) {
132             List JavaDoc<LogRecord JavaDoc> r = records;
133             if (r == null) {
134                 return;
135             }
136             try {
137
138                 for (LogRecord JavaDoc log : r) {
139                     if (log.getMessage() != null) {
140                         a.append(log.getMessage()).append("\n");;
141                     }
142                     if (log.getThrown() != null) {
143                         StringWriter JavaDoc w = new StringWriter JavaDoc();
144                         log.getThrown().printStackTrace(new PrintWriter JavaDoc(w));
145                         a.append(w.toString()).append("\n");
146                     }
147                 }
148             } catch (IOException JavaDoc ex) {
149                 ex.printStackTrace();
150             }
151         }
152     } // end AnnException
153
}
154
155
Popular Tags