001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.xml.lib;
042:
043: import org.openide.ErrorManager;
044: import org.openide.util.Lookup;
045: import org.openide.util.NbBundle;
046:
047: import java.awt.*;
048: import java.util.StringTokenizer;
049: import java.util.MissingResourceException;
050:
051: /**
052: * This provides package scope utilities for debugging and code
053: * internationalization. It is designed to have a subclass,
054: * <code>ErrorManager</code> instance and bundle in each client's package.
055: * <p>
056: * It ensures localized strings will be loaded from <code>Bundle.properties</code>
057: * (branded) in same package as subclass belongs.
058: * <p>
059: * Debugging methods use {@link org.openide.ErrorManager} to log messages.
060: * <code>ErrorManager</code> instance also belongs to sub-class's package.
061: *
062: * @author Libor Kramolis
063: */
064: public abstract class AbstractUtil {
065: /** Instance package ErrorManager. */
066: private ErrorManager packageErrorManager;
067: /** Default debug severity used with ErrorManager. */
068: private static final int DEBUG_SEVERITY = ErrorManager.INFORMATIONAL;
069:
070: //
071: // String localizing purposes
072: //
073:
074: /**
075: * Get localized string from package bundle.
076: * @param key Key identifing localized value.
077: * @return localized value.
078: */
079: public final String getString(String key) {
080: if (key == null)
081: throw new NullPointerException();
082: return NbBundle.getMessage(this .getClass(), key);
083: }
084:
085: /**
086: * Get localized string from package bundle.
087: * @param key Key identifing localized value (<code>MessageFormat</code>).
088: * @param param An argument <code>{0}</code> used for message parametrization.
089: * @return localized value.
090: */
091: public final String getString(String key, Object param) {
092: if (key == null)
093: throw new NullPointerException();
094: return NbBundle.getMessage(this .getClass(), key, param);
095: }
096:
097: /**
098: * Get localized string from package bundle.
099: * @param key Key identifing localized value (<code>MessageFormat</code>).
100: * @param param1 An argument <code>{0}</code> used for message parametrization.
101: * @param param2 An argument <code>{1}</code> used for message parametrization.
102: * @return Localized value.
103: */
104: public final String getString(String key, Object param1,
105: Object param2) {
106: if (key == null)
107: throw new NullPointerException();
108: return NbBundle
109: .getMessage(this .getClass(), key, param1, param2);
110: }
111:
112: /**
113: * Get localized character from package bundle. Usually used on mnemonic.
114: * @param key Key identifing localized value.
115: * @return localized value.
116: */
117: public final char getChar(String key) {
118: if (key == null)
119: throw new NullPointerException();
120: return NbBundle.getMessage(this .getClass(), key).charAt(0);
121: }
122:
123: /**
124: * Loads branded color. Parses syntax given by to Color(int,int,int) constructor:
125: * e.g.: <tt>0,255,128</tt>.
126: * @return color or <code>null</null> if bundle contains <tt>null</tt> literal
127: * (masking) or of entry miss at all.
128: * @throws MissingResourceException on invalid color syntax
129: */
130: public final Color getColor(String key) {
131: String raw = null;
132: try {
133: raw = getString(key);
134: } catch (MissingResourceException e) {
135: return null;
136: }
137: StringTokenizer tokenizer = new StringTokenizer(raw, ", \t"); // NOI18N
138: if (tokenizer.countTokens() < 3) {
139: if (tokenizer.countTokens() == 1) {
140: if ("null".equals(tokenizer.nextToken()))
141: return null; // NOI18N
142: }
143: throw new MissingResourceException("Invalid color format: "
144: + raw, getClass().getName(), key); // NOI18N
145: }
146:
147: String red = tokenizer.nextToken();
148: String green = tokenizer.nextToken();
149: String blue = tokenizer.nextToken();
150: int r = Integer.parseInt(red);
151: if (r < 0 || r > 255)
152: throw new MissingResourceException("Invalid color format: "
153: + raw, getClass().getName(), key); // NOI18N
154: int g = Integer.parseInt(green);
155: if (g < 0 || g > 255)
156: throw new MissingResourceException("Invalid color format: "
157: + raw, getClass().getName(), key); // NOI18N
158: int b = Integer.parseInt(blue);
159: if (b < 0 || b > 255)
160: throw new MissingResourceException("Invalid color format: "
161: + raw, getClass().getName(), key); // NOI18N
162: // ignore remainig tokens, possibly alpha in future
163:
164: return new Color(r, g, b);
165: }
166:
167: //
168: // Debugging purposes
169: //
170:
171: /**
172: * Check whether running at loggable level.
173: * @return true if <code>debug (...)</code> will log something.
174: */
175: public final boolean isLoggable() {
176: return getErrorManager().isLoggable(DEBUG_SEVERITY);
177: }
178:
179: /**
180: * Log a message if package log level passes.
181: * @param message Message to log down. <code>null</code> is allowed
182: * but is not logged.
183: */
184: public final void debug(String message) {
185: if (message == null)
186: return;
187: getErrorManager().log(DEBUG_SEVERITY, message);
188: }
189:
190: /**
191: * Always log a exception.
192: * @param ex Exception to log down. <code>null</code> is allowed
193: * but is not logged.
194: */
195: public final void debug(Throwable ex) {
196: if (ex == null)
197: return;
198: getErrorManager().notify(DEBUG_SEVERITY, ex);
199: }
200:
201: /**
202: * Always log an annotated exception.
203: * @param message Message used for exception annotation or <code>null</code>.
204: * @param ex Exception to log down. <code>null</code> is allowed
205: * but is not logged.
206: */
207: public final void debug(String message, Throwable ex) {
208: if (ex == null)
209: return;
210: if (message != null) {
211: ex = getErrorManager().annotate(ex, DEBUG_SEVERITY,
212: message, null, null, null);
213: }
214: debug(ex);
215: }
216:
217: /**
218: * Provide an <code>ErrorManager</code> instance named per subclass package.
219: * @return ErrorManager which is default for package where is class
220: * declared .
221: */
222: public final synchronized ErrorManager getErrorManager() {
223: if (packageErrorManager == null) {
224: String pack = "org.netbeans.modules.xml.core.lib"; // NOI18N
225: packageErrorManager = ErrorManager.getDefault()
226: .getInstance(pack);
227: }
228: return packageErrorManager;
229: }
230:
231: }
|