KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > AbstractUtil


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.modules.css;
20
21 import org.openide.ErrorManager;
22 import org.openide.util.Lookup;
23 import org.openide.util.NbBundle;
24
25 /**
26  * This provides package scope utilities for debugging and code
27  * internationalization. It is designed to have a subclass,
28  * <code>ErrorManager</code> instance and bundle in each client's package.
29  * <p>
30  * It ensures localized strings will be loaded from <code>Bundle.properties</code>
31  * (branded) in same package as subclass belongs.
32  * <p>
33  * Debugging methods use {@link org.openide.ErrorManager} to log messages.
34  * <code>ErrorManager</code> instance also belongs to sub-class's package.
35  *
36  * @author Libor Kramolis
37  */

38 public abstract class AbstractUtil {
39     
40     /** Cached package name. */
41     private String JavaDoc packageName;
42     /** Instance package ErrorManager. */
43     private ErrorManager packageErrorManager;
44     /** Default debug severity used with ErrorManager. */
45     private static final int DEBUG_SEVERITY = ErrorManager.INFORMATIONAL;
46     
47     /**
48      * @return package name of this instance
49      */

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

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

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

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

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

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

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

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

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

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

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

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