0N/A/*
2362N/A * Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.tools.jconsole;
0N/A
5335N/Aimport java.awt.event.KeyEvent;
5335N/Aimport java.lang.reflect.Field;
5335N/Aimport java.lang.reflect.Modifier;
0N/Aimport java.text.MessageFormat;
5335N/Aimport java.util.Collections;
5335N/Aimport java.util.HashMap;
5335N/Aimport java.util.Map;
0N/Aimport java.util.MissingResourceException;
0N/Aimport java.util.ResourceBundle;
0N/A
0N/A/**
5335N/A * Toolkit that provides resource support for JConsole.
0N/A */
0N/Apublic final class Resources {
5335N/A private static Map<String, Integer> MNEMONIC_LOOKUP = Collections
5335N/A .synchronizedMap(new HashMap<String, Integer>());
0N/A
5335N/A private Resources() {
5335N/A throw new AssertionError();
5335N/A }
5335N/A
5335N/A /**
5335N/A * Convenience method for {@link MessageFormat#format(String, Object...)}.
5335N/A *
5335N/A * @param pattern the pattern
5335N/A * @param objects the arguments for the pattern
5335N/A *
5335N/A * @return a formatted string
5335N/A */
5335N/A public static String format(String pattern, Object... arguments) {
5335N/A return MessageFormat.format(pattern, arguments);
5335N/A }
5335N/A
5335N/A /**
5335N/A * Returns the mnemonic for a message.
5335N/A *
5335N/A * @param message the message
5335N/A *
5335N/A * @return the mnemonic <code>int</code>
5335N/A */
5335N/A public static int getMnemonicInt(String message) {
5335N/A Integer integer = MNEMONIC_LOOKUP.get(message);
5335N/A if (integer != null) {
5335N/A return integer.intValue();
5335N/A }
5335N/A return 0;
5335N/A }
5335N/A
5335N/A /**
5335N/A * Initializes all non-final public static fields in the given class with
5335N/A * messages from a {@link ResourceBundle}.
5335N/A *
5335N/A * @param clazz the class containing the fields
5335N/A */
5335N/A public static void initializeMessages(Class<?> clazz, String rbName) {
5335N/A ResourceBundle rb = null;
0N/A try {
5335N/A rb = ResourceBundle.getBundle(rbName);
5335N/A } catch (MissingResourceException mre) {
5335N/A // fall through, handled later
5335N/A }
5335N/A for (Field field : clazz.getFields()) {
5335N/A if (isWritableField(field)) {
5335N/A String key = field.getName();
5335N/A String message = getMessage(rb, key);
5335N/A int mnemonicInt = findMnemonicInt(message);
5335N/A message = removeMnemonicAmpersand(message);
5335N/A message = replaceWithPlatformLineFeed(message);
5335N/A setFieldValue(field, message);
5335N/A MNEMONIC_LOOKUP.put(message, mnemonicInt);
5335N/A }
0N/A }
0N/A }
0N/A
5335N/A private static boolean isWritableField(Field field) {
5335N/A int modifiers = field.getModifiers();
5335N/A return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
5335N/A && !Modifier.isFinal(modifiers);
5335N/A }
0N/A
0N/A /**
5335N/A * Returns the message corresponding to the key in the bundle or a text
5335N/A * describing it's missing.
0N/A *
5335N/A * @param rb the resource bundle
5335N/A * @param key the key
5335N/A *
5335N/A * @return the message
0N/A */
5335N/A private static String getMessage(ResourceBundle rb, String key) {
5335N/A if (rb == null) {
5335N/A return "missing resource bundle";
0N/A }
5335N/A try {
5335N/A return rb.getString(key);
5335N/A } catch (MissingResourceException mre) {
5335N/A return "missing message for key = \"" + key
5335N/A + "\" in resource bundle ";
5335N/A }
0N/A }
0N/A
5335N/A private static void setFieldValue(Field field, String value) {
5335N/A try {
5335N/A field.set(null, value);
5335N/A } catch (IllegalArgumentException | IllegalAccessException e) {
5335N/A throw new Error("Unable to access or set message for field " + field.getName());
0N/A }
5335N/A }
5335N/A
5335N/A /**
5335N/A * Returns a {@link String} where all <code>\n</code> in the <text> have
5335N/A * been replaced with the line separator for the platform.
5335N/A *
5335N/A * @param text the to be replaced
5335N/A *
5335N/A * @return the replaced text
5335N/A */
5335N/A private static String replaceWithPlatformLineFeed(String text) {
5335N/A return text.replace("\n", System.getProperty("line.separator"));
0N/A }
0N/A
0N/A /**
5335N/A * Removes the mnemonic identifier (<code>&</code>) from a string unless
5335N/A * it's escaped by <code>&&</code> or placed at the end.
0N/A *
5335N/A * @param message the message
5335N/A *
5335N/A * @return a message with the mnemonic identifier removed
0N/A */
5335N/A private static String removeMnemonicAmpersand(String message) {
5335N/A StringBuilder s = new StringBuilder();
5335N/A for (int i = 0; i < message.length(); i++) {
5335N/A char current = message.charAt(i);
5335N/A if (current != '&' || i == message.length() - 1
5335N/A || message.charAt(i + 1) == '&') {
5335N/A s.append(current);
0N/A }
0N/A }
5335N/A return s.toString();
0N/A }
0N/A
0N/A /**
5335N/A * Finds the mnemonic character in a message.
0N/A *
5335N/A * The mnemonic character is the first character followed by the first
5335N/A * <code>&</code> that is not followed by another <code>&</code>.
5335N/A *
5335N/A * @return the mnemonic as an <code>int</code>, or <code>0</code> if it
5335N/A * can't be found.
0N/A */
5335N/A private static int findMnemonicInt(String s) {
5335N/A for (int i = 0; i < s.length() - 1; i++) {
5335N/A if (s.charAt(i) == '&') {
5335N/A if (s.charAt(i + 1) != '&') {
5335N/A return lookupMnemonicInt(s.substring(i + 1, i + 2));
5335N/A } else {
5335N/A i++;
5335N/A }
0N/A }
0N/A }
5335N/A return 0;
5335N/A }
5335N/A
5335N/A /**
5335N/A * Lookups the mnemonic for a key in the {@link KeyEvent} class.
5335N/A *
5335N/A * @param c the character to lookup
5335N/A *
5335N/A * @return the mnemonic as an <code>int</code>, or <code>0</code> if it
5335N/A * can't be found.
5335N/A */
5335N/A private static int lookupMnemonicInt(String c) {
5335N/A try {
5335N/A return KeyEvent.class.getDeclaredField("VK_" + c.toUpperCase())
5335N/A .getInt(null);
5335N/A } catch (IllegalArgumentException | IllegalAccessException
5335N/A | NoSuchFieldException | SecurityException e) {
5335N/A // Missing VK is okay
5335N/A return 0;
5335N/A }
0N/A }
0N/A}