KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openide > explorer > UIException


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.modules.openide.explorer;
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 UIException {
40     
41     /** Creates a new instance of UIException */
42     private UIException() {
43     }
44     
45     public static void annotateUser(
46         Throwable JavaDoc t,
47         String JavaDoc msg,
48         String JavaDoc locMsg,
49         Throwable JavaDoc stackTrace,
50         Date JavaDoc date
51     ) {
52         AnnException ex = AnnException.findOrCreate(t, true);
53         LogRecord JavaDoc rec = new LogRecord JavaDoc(OwnLevel.USER, msg);
54         if (stackTrace != null) {
55             rec.setThrown(stackTrace);
56         }
57         ex.addRecord(rec);
58         
59         if (locMsg != null) {
60             Exceptions.attachLocalizedMessage(t, locMsg);
61         }
62     }
63     private static final class OwnLevel extends Level JavaDoc {
64         public static final Level JavaDoc USER = new OwnLevel("USER", 1973); // NOI18N
65

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