|
Java example source code file (Resources.java)
The Resources.java Java example source code/* * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.tools.jconsole; import java.awt.event.KeyEvent; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.text.MessageFormat; import java.util.Collections; import java.util.IdentityHashMap; import java.util.Map; import java.util.MissingResourceException; import java.util.ResourceBundle; /** * Toolkit that provides resource support for JConsole. */ public final class Resources { private static Map<String, Integer> MNEMONIC_LOOKUP = Collections .synchronizedMap(new IdentityHashMap<String, Integer>()); private Resources() { throw new AssertionError(); } /** * Convenience method for {@link MessageFormat#format(String, Object...)}. * * @param pattern the pattern * @param objects the arguments for the pattern * * @return a formatted string */ public static String format(String pattern, Object... arguments) { return MessageFormat.format(pattern, arguments); } /** * Returns the mnemonic for a message. * * @param message the message * * @return the mnemonic <code>int */ public static int getMnemonicInt(String message) { Integer integer = MNEMONIC_LOOKUP.get(message); if (integer != null) { return integer.intValue(); } return 0; } /** * Initializes all non-final public static fields in the given class with * messages from a {@link ResourceBundle}. * * @param clazz the class containing the fields */ public static void initializeMessages(Class<?> clazz, String rbName) { ResourceBundle rb = null; try { rb = ResourceBundle.getBundle(rbName); } catch (MissingResourceException mre) { // fall through, handled later } for (Field field : clazz.getFields()) { if (isWritableField(field)) { String key = field.getName(); String message = getMessage(rb, key); int mnemonicInt = findMnemonicInt(message); message = removeMnemonicAmpersand(message); message = replaceWithPlatformLineFeed(message); setFieldValue(field, message); MNEMONIC_LOOKUP.put(message, mnemonicInt); } } } private static boolean isWritableField(Field field) { int modifiers = field.getModifiers(); return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers); } /** * Returns the message corresponding to the key in the bundle or a text * describing it's missing. * * @param rb the resource bundle * @param key the key * * @return the message */ private static String getMessage(ResourceBundle rb, String key) { if (rb == null) { return "missing resource bundle"; } try { return rb.getString(key); } catch (MissingResourceException mre) { return "missing message for key = \"" + key + "\" in resource bundle "; } } private static void setFieldValue(Field field, String value) { try { field.set(null, value); } catch (IllegalArgumentException | IllegalAccessException e) { throw new Error("Unable to access or set message for field " + field.getName()); } } /** * Returns a {@link String} where all <code>\n in the Other Java examples (source code examples)Here is a short list of links related to this Java Resources.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.