KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openide > loaders > 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.loaders;
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         static AnnException findOrCreate(Throwable JavaDoc t, boolean create) {
74             if (t instanceof AnnException) {
75                 return (AnnException)t;
76             }
77             if (t.getCause() == null) {
78                 if (create) {
79                     t.initCause(new AnnException());
80                 }
81                 return (AnnException)t.getCause();
82             }
83             return findOrCreate(t.getCause(), create);
84         }
85
86         private AnnException() {
87         }
88
89         public synchronized void addRecord(LogRecord JavaDoc rec) {
90             if (records == null) {
91                 records = new ArrayList JavaDoc<LogRecord JavaDoc>();
92             }
93             records.add(rec);
94         }
95
96         public LogRecord JavaDoc[] call() {
97             List JavaDoc<LogRecord JavaDoc> r = records;
98             LogRecord JavaDoc[] empty = new LogRecord JavaDoc[0];
99             return r == null ? empty : r.toArray(empty);
100         }
101     } // end AnnException
102
}
103
104
Popular Tags