001: /*
002: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jconsole;
027:
028: import java.text.MessageFormat;
029: import java.util.MissingResourceException;
030: import java.util.ResourceBundle;
031:
032: import sun.tools.jconsole.resources.JConsoleResources;
033:
034: /**
035: * Provides resource support for jconsole.
036: */
037: public final class Resources {
038:
039: private static final Object lock = new Object();
040: private static JConsoleResources resources = null;
041: static {
042: try {
043: resources = (JConsoleResources) ResourceBundle
044: .getBundle("sun.tools.jconsole.resources.JConsoleResources");
045: } catch (MissingResourceException e) {
046: // gracefully handle this later
047: }
048: }
049:
050: private Resources() {
051: throw new AssertionError();
052: }
053:
054: /**
055: * Returns the text of the jconsole resource for the specified key
056: * formatted with the specified arguments.
057: *
058: */
059: public static String getText(String key, Object... args) {
060: String format = getString(key);
061: if (format == null) {
062: format = "missing resource key: key = \"" + key + "\", "
063: + "arguments = \"{0}\", \"{1}\", \"{2}\"";
064: }
065: return formatMessage(format, args);
066: }
067:
068: static String formatMessage(String format, Object... args) {
069: String ss = null;
070: synchronized (lock) {
071: /*
072: * External synchronization required for safe use of
073: * java.text.MessageFormat:
074: */
075: ss = MessageFormat.format(format, args);
076: }
077: return ss;
078: }
079:
080: /**
081: * Returns the mnemonic keycode int of the jconsole resource for the specified key.
082: *
083: */
084: public static int getMnemonicInt(String key) {
085: int mnemonic = 0;
086: if (resources != null) {
087: Object obj = resources.getObject(key + ".mnemonic");
088: if (obj instanceof Character) {
089: mnemonic = (int) (Character) obj;
090: if (mnemonic >= 'a' && mnemonic <= 'z') {
091: mnemonic -= ('a' - 'A');
092: }
093: } else if (obj instanceof Integer) {
094: mnemonic = (Integer) obj;
095: }
096: }
097: return mnemonic;
098: }
099:
100: /**
101: * Returns the jconsole resource string for the specified key.
102: *
103: */
104: private static String getString(String key) {
105: if (resources != null) {
106: try {
107: return resources.getString(key);
108: } catch (MissingResourceException e) {
109: return null;
110: }
111: }
112: return "missing resource bundle: key = \"" + key + "\", "
113: + "arguments = \"{0}\", \"{1}\", \"{2}\"";
114: }
115: }
|