001: /*
002: * @(#)ResourceBundleUtil.java 1.3.3 2005-11-07
003: *
004: * Copyright (c) 2000-2005 Werner Randelshofer
005: * Staldenmattweg 2, CH-6405 Immensee, Switzerland
006: * All rights reserved.
007: *
008: * This software is the confidential and proprietary information of
009: * Werner Randelshofer. ("Confidential Information"). You shall not
010: * disclose such Confidential Information and shall use it only in
011: * accordance with the terms of the license agreement you entered into
012: * with Werner Randelshofer.
013: */
014: package contrib.ch.randelshofer.quaqua.util;
015:
016: import java.util.*;
017: import javax.swing.KeyStroke;
018: import javax.swing.UIManager;
019: import javax.swing.ImageIcon;
020: import java.text.MessageFormat;
021: import java.net.URL;
022:
023: /**
024: * This is a convenience wrapper for accessing resources stored in a ResourceBundle.
025: *
026: * @author Werner Randelshofer, Staldenmattweg 2, CH-6405 Immensee, Switzerland
027: * @version 1,3.3 2005-11-07 Method getLocale added.
028: * <br>1.3.2 2004-05-02 Method getLAFBundle without Locale as parameter
029: * added.
030: * <br>1.3.1 2002-07-30 Method getLAFBundle now takes a Locale as
031: * an additional parameter.
032: * <br>1.3 2001-10-10 The default resource name changed from 'name_Metal'
033: * to 'name'.
034: * <br> 1.2 2001-07-23 Adaptation to JDK 1.3 in progress.
035: * <br> 1.0 2000-06-10 Created.
036: */
037: public class ResourceBundleUtil {
038: /** The wrapped resource bundle. */
039: private ResourceBundle resource;
040:
041: /**
042: * Creates a new ResouceBundleUtil which wraps
043: * the provided resource bundle.
044: */
045: public ResourceBundleUtil(ResourceBundle r) {
046: resource = r;
047: }
048:
049: /**
050: * Get a String from the ResourceBundle.
051: * <br>Convenience method to save casting.
052: *
053: * @param key The key of the property.
054: * @return The value of the property. Returns the key
055: * if the property is missing.
056: */
057: public String getString(String key) {
058: try {
059: return resource.getString(key);
060: } catch (MissingResourceException e) {
061: return '-' + key + '-';
062: }
063: }
064:
065: /**
066: * Get an image icon from the ResourceBundle.
067: * <br>Convenience method .
068: *
069: * @param key The key of the property.
070: * @return The value of the property. Returns null
071: * if the property is missing.
072: */
073: public ImageIcon getImageIcon(String key, Class baseClass) {
074: try {
075: String rsrcName = resource.getString(key);
076: if (rsrcName.equals("")) {
077: return null;
078: }
079: URL url = baseClass.getResource(rsrcName);
080: return (url == null) ? null : new ImageIcon(url);
081: } catch (MissingResourceException e) {
082: return null;
083: }
084: }
085:
086: /**
087: * Get a Mnemonic from the ResourceBundle.
088: * <br>Convenience method.
089: *
090: * @param key The key of the property.
091: * @return The first char of the value of the property.
092: * Returns '\0' if the property is missing.
093: */
094: public char getMnemonic(String key) {
095: String s = resource.getString(key);
096: return (s == null || s.length() == 0) ? '\0' : s.charAt(0);
097: }
098:
099: /**
100: * Get a Mnemonic from the ResourceBundle.
101: * <br>Convenience method.
102: *
103: * @param key The key of the property. This method appends "Mnem" to the key.
104: * @return The first char of the value of the property.
105: * Returns '\0' if the property is missing.
106: */
107: public char getMnem(String key) {
108: String s = resource.getString(key + "Mnem");
109: return (s == null || s.length() == 0) ? '\0' : s.charAt(0);
110: }
111:
112: /**
113: * Get a KeyStroke from the ResourceBundle.
114: * <BR>Convenience method.
115: *
116: * @param key The key of the property.
117: * @return <code>javax.swing.KeyStroke.getKeyStroke(value)</code>.
118: * Returns null if the property is missing.
119: */
120: public KeyStroke getKeyStroke(String key) {
121: KeyStroke ks = null;
122: try {
123: String s = resource.getString(key);
124: ks = (s == null) ? (KeyStroke) null : KeyStroke
125: .getKeyStroke(s);
126: } catch (NoSuchElementException e) {
127: }
128: return ks;
129: }
130:
131: /**
132: * Get a KeyStroke from the ResourceBundle.
133: * <BR>Convenience method.
134: *
135: * @param key The key of the property. This method adds "Acc" to the key.
136: * @return <code>javax.swing.KeyStroke.getKeyStroke(value)</code>.
137: * Returns null if the property is missing.
138: */
139: public KeyStroke getAcc(String key) {
140: KeyStroke ks = null;
141: try {
142: String s = resource.getString(key + "Acc");
143: ks = (s == null) ? (KeyStroke) null : KeyStroke
144: .getKeyStroke(s);
145: } catch (NoSuchElementException e) {
146: }
147: return ks;
148: }
149:
150: public String getFormatted(String key, Object argument) {
151: return MessageFormat.format(resource.getString(key),
152: new Object[] { argument });
153: }
154:
155: public String getFormatted(String key, Object[] arguments) {
156: return MessageFormat.format(resource.getString(key), arguments);
157: }
158:
159: public Locale getLocale() {
160: return resource.getLocale();
161: }
162:
163: /**
164: * Get the appropriate ResourceBundle subclass.
165: *
166: * @see java.util.ResourceBundle
167: */
168: public static ResourceBundleUtil getBundle(String baseName)
169: throws MissingResourceException {
170: return new ResourceBundleUtil(ResourceBundle.getBundle(
171: baseName, Locale.getDefault()));
172: }
173:
174: /**
175: * Get the appropriate ResourceBundle subclass.
176: * The baseName is extended by the Swing Look and Feel ID
177: * and by the Locale code returned by Locale.getDefault().
178: *
179: * The default Look and Feel ID is Metal.
180: *
181: * @see java.util.ResourceBundle
182: */
183: public static ResourceBundleUtil getLAFBundle(String baseName)
184: throws MissingResourceException {
185: return getLAFBundle(baseName, Locale.getDefault());
186: }
187:
188: /**
189: * Get the appropriate ResourceBundle subclass.
190: * The baseName is extended by the Swing Look and Feel ID
191: * and by the Locale code.
192: *
193: * The default Look and Feel ID is Metal.
194: *
195: * @see java.util.ResourceBundle
196: */
197: public static ResourceBundleUtil getLAFBundle(String baseName,
198: Locale locale) throws MissingResourceException {
199: ResourceBundleUtil r;
200: try {
201: r = new ResourceBundleUtil(ResourceBundle
202: .getBundle(baseName + "_"
203: + UIManager.getLookAndFeel().getID(),
204: locale));
205: } catch (MissingResourceException e) {
206: r = new ResourceBundleUtil(ResourceBundle.getBundle(
207: baseName, locale));
208: }
209: return r;
210: }
211: }
|