KickJava   Java API By Example, From Geeks To Geeks.

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

42 public abstract class AbstractUtil {
43     /** Instance package ErrorManager. */
44     private ErrorManager packageErrorManager;
45     /** Default debug severity used with ErrorManager. */
46     private static final int DEBUG_SEVERITY = ErrorManager.INFORMATIONAL;
47     
48     //
49
// String localizing purposes
50
//
51

52
53     /**
54      * Get localized string from package bundle.
55      * @param key Key identifing localized value.
56      * @return localized value.
57      */

58     public final String JavaDoc getString (String JavaDoc key) {
59         if (key == null) throw new NullPointerException JavaDoc();
60     return NbBundle.getMessage (this.getClass(), key);
61     }
62     
63     /**
64      * Get localized string from package bundle.
65      * @param key Key identifing localized value (<code>MessageFormat</code>).
66      * @param param An argument <code>{0}</code> used for message parametrization.
67      * @return localized value.
68      */

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

81     public final String JavaDoc getString (String JavaDoc key, Object JavaDoc param1, Object JavaDoc param2) {
82         if (key == null) throw new NullPointerException JavaDoc();
83     return NbBundle.getMessage (this.getClass(), key, param1, param2);
84     }
85     
86     /**
87      * Get localized character from package bundle. Usually used on mnemonic.
88      * @param key Key identifing localized value.
89      * @return localized value.
90      */

91     public final char getChar (String JavaDoc key) {
92         if (key == null) throw new NullPointerException JavaDoc();
93     return NbBundle.getMessage (this.getClass(), key).charAt (0);
94     }
95
96
97     /**
98      * Loads branded color. Parses syntax given by to Color(int,int,int) constructor:
99      * e.g.: <tt>0,255,128</tt>.
100      * @return color or <code>null</null> if bundle contains <tt>null</tt> literal
101      * (masking) or of entry miss at all.
102      * @throws MissingResourceException on invalid color syntax
103      */

104     public final Color getColor(String JavaDoc key) {
105         String JavaDoc raw = null;
106         try {
107             raw = getString(key);
108         } catch (MissingResourceException JavaDoc e) {
109             return null;
110         }
111         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(raw, ", \t"); // NOI18N
112
if (tokenizer.countTokens() < 3) {
113             if (tokenizer.countTokens() == 1) {
114                 if ("null".equals(tokenizer.nextToken())) return null; // NOI18N
115
}
116             throw new MissingResourceException JavaDoc("Invalid color format: " + raw, getClass().getName(), key); // NOI18N
117
}
118
119         String JavaDoc red = tokenizer.nextToken();
120         String JavaDoc green = tokenizer.nextToken();
121         String JavaDoc blue = tokenizer.nextToken();
122         int r = Integer.parseInt(red);
123         if (r<0 || r>255) throw new MissingResourceException JavaDoc("Invalid color format: " + raw, getClass().getName(), key); // NOI18N
124
int g = Integer.parseInt(green);
125         if (g<0 || g>255) throw new MissingResourceException JavaDoc("Invalid color format: " + raw, getClass().getName(), key); // NOI18N
126
int b = Integer.parseInt(blue);
127         if (b<0 || b>255) throw new MissingResourceException JavaDoc("Invalid color format: " + raw, getClass().getName(), key); // NOI18N
128
// ignore remainig tokens, possibly alpha in future
129

130         return new Color(r, g, b);
131     }
132
133     //
134
// Debugging purposes
135
//
136

137     /**
138      * Check whether running at loggable level.
139      * @return true if <code>debug (...)</code> will log something.
140      */

141     public final boolean isLoggable () {
142         return getErrorManager().isLoggable (DEBUG_SEVERITY);
143     }
144
145     /**
146      * Log a message if package log level passes.
147      * @param message Message to log down. <code>null</code> is allowed
148      * but is not logged.
149      */

150     public final void debug (String JavaDoc message) {
151         if (message == null) return;
152         getErrorManager().log (DEBUG_SEVERITY, message);
153     }
154
155     /**
156      * Always log a exception.
157      * @param ex Exception to log down. <code>null</code> is allowed
158      * but is not logged.
159      */

160     public final void debug (Throwable JavaDoc ex) {
161         if (ex == null) return;
162         getErrorManager().notify (DEBUG_SEVERITY, ex);
163     }
164
165     /**
166      * Always log an annotated exception.
167      * @param message Message used for exception annotation or <code>null</code>.
168      * @param ex Exception to log down. <code>null</code> is allowed
169      * but is not logged.
170      */

171     public final void debug (String JavaDoc message, Throwable JavaDoc ex) {
172         if (ex == null) return;
173         if (message != null) {
174             ex = getErrorManager().annotate(ex, DEBUG_SEVERITY, message, null, null, null);
175         }
176         debug (ex);
177     }
178
179     /**
180      * Provide an <code>ErrorManager</code> instance named per subclass package.
181      * @return ErrorManager which is default for package where is class
182      * declared .
183      */

184     public final synchronized ErrorManager getErrorManager () {
185         if ( packageErrorManager == null ) {
186             String JavaDoc pack = "org.netbeans.modules.xml.core.lib"; // NOI18N
187
packageErrorManager = ErrorManager.getDefault().getInstance(pack);
188         }
189         return packageErrorManager;
190     }
191
192 }
193
Popular Tags