KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > xml > cookies > Util


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 package org.netbeans.spi.xml.cookies;
20
21 import org.openide.ErrorManager;
22 import org.openide.util.Lookup;
23 import org.openide.util.NbBundle;
24
25 /**
26  *
27  * @author Libor Kramolis
28  */

29 class Util {
30
31     /** Default and only one instance of this class. */
32     public static final Util THIS = new Util();
33
34     /** Nobody can create instance of it, just me. */
35     private Util () {
36     }
37
38     /** Cached package name. */
39     private String JavaDoc packageName;
40     /** Instance package ErrorManager. */
41     private ErrorManager packageErrorManager;
42     /** Default debug severity used with ErrorManager. */
43     private static final int DEBUG_SEVERITY = ErrorManager.INFORMATIONAL;
44     
45     /**
46      * @return package name of this instance
47      */

48     private final synchronized String JavaDoc getPackageName () {
49         if ( packageName == null ) {
50             //??? what for classed from default package? -> A: we do not have classes in default package!
51
packageName = this.getClass().getPackage().getName().intern();
52         }
53         return packageName;
54     }
55     
56     
57     //
58
// String localizing purposes
59
//
60

61
62     /**
63      * Get localized string from package bundle.
64      * @param key Key identifing localized value.
65      * @return localized value.
66      */

67     public final String JavaDoc getString (String JavaDoc key) {
68         if (key == null) throw new NullPointerException JavaDoc();
69     return NbBundle.getMessage (this.getClass(), key);
70     }
71     
72     /**
73      * Get localized string from package bundle.
74      * @param key Key identifing localized value (<code>MessageFormat</code>).
75      * @param param An argument <code>{0}</code> used for message parametrization.
76      * @return localized value.
77      */

78     public final String JavaDoc getString (String JavaDoc key, Object JavaDoc param) {
79         if (key == null) throw new NullPointerException JavaDoc();
80     return NbBundle.getMessage (this.getClass(), key, param);
81     }
82     
83     /**
84      * Get localized string from package bundle.
85      * @param key Key identifing localized value (<code>MessageFormat</code>).
86      * @param param1 An argument <code>{0}</code> used for message parametrization.
87      * @param param2 An argument <code>{1}</code> used for message parametrization.
88      * @return Localized value.
89      */

90     public final String JavaDoc getString (String JavaDoc key, Object JavaDoc param1, Object JavaDoc param2) {
91         if (key == null) throw new NullPointerException JavaDoc();
92     return NbBundle.getMessage (this.getClass(), key, param1, param2);
93     }
94     
95     /**
96      * Get localized character from package bundle. Usually used on mnemonic.
97      * @param key Key identifing localized value.
98      * @return localized value.
99      */

100     public final char getChar (String JavaDoc key) {
101         if (key == null) throw new NullPointerException JavaDoc();
102     return NbBundle.getMessage (this.getClass(), key).charAt (0);
103     }
104     
105     
106     //
107
// Debugging purposes
108
//
109

110     /**
111      * Check whether running at loggable level.
112      * @return true if <code>debug (...)</code> will log something.
113      */

114     public final boolean isLoggable () {
115         return getErrorManager().isLoggable (DEBUG_SEVERITY);
116     }
117
118     /**
119      * Log a message if package log level passes.
120      * @param message Message to log down. <code>null</code> is allowed
121      * but is not logged.
122      */

123     public final void debug (String JavaDoc message) {
124         if (message == null) return;
125         getErrorManager().log (DEBUG_SEVERITY, message);
126     }
127
128     /**
129      * Always log a exception.
130      * @param ex Exception to log down. <code>null</code> is allowed
131      * but is not logged.
132      */

133     public final void debug (Throwable JavaDoc ex) {
134         if (ex == null) return;
135         getErrorManager().notify (DEBUG_SEVERITY, ex);
136     }
137
138     /**
139      * Always log an annotated exception.
140      * @param message Message used for exception annotation or <code>null</code>.
141      * @param ex Exception to log down. <code>null</code> is allowed
142      * but is not logged.
143      */

144     public final void debug (String JavaDoc message, Throwable JavaDoc ex) {
145         if (ex == null) return;
146         if (message != null) {
147             ex = getErrorManager().annotate(ex, DEBUG_SEVERITY, message, null, null, null);
148         }
149         debug (ex);
150     }
151
152     /**
153      * Provide an <code>ErrorManager</code> instance named per subclass package.
154      * @return ErrorManager which is default for package where is class
155      * declared .
156      */

157     public final synchronized ErrorManager getErrorManager () {
158         if ( packageErrorManager == null ) {
159             String JavaDoc pack = getPackageName();
160             packageErrorManager = ErrorManager.getDefault().getInstance(pack);
161         }
162         return packageErrorManager;
163     }
164     
165 }
166
Popular Tags