Source code: com/port80/eclipse/util/Messages.java
1 package com.port80.eclipse.util;
2
3 import java.text.MessageFormat;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6
7 /**
8 * @author chrisl
9 *
10 * Utility class which helps managing messages
11 */
12 public class Messages {
13 private static final String RESOURCE_BUNDLE = "com.port80.eclipse.util.messages"; //$NON-NLS-1$
14 private static ResourceBundle bundle =
15 ResourceBundle.getBundle(RESOURCE_BUNDLE);
16
17 private Messages() {
18 // prevent instantiation of class
19 }
20 /**
21 * Returns the formatted message for the given key in
22 * the resource bundle.
23 *
24 * @param key the resource name
25 * @param args the message arguments
26 * @return the string
27 */
28 public static String format(String key, Object[] args) {
29 return MessageFormat.format(getString(key), args);
30 }
31 /**
32 * Returns the resource object with the given key in
33 * the resource bundle. If there isn't any value under
34 * the given key, the key is returned.
35 *
36 * @param key the resource name
37 * @return the string
38 */
39 public static String getString(String key) {
40 try {
41 return bundle.getString(key);
42 } catch (MissingResourceException e) {
43 return key;
44 }
45 }
46 /**
47 * Returns the resource object with the given key in
48 * the resource bundle. If there isn't any value under
49 * the given key, the default value is returned.
50 *
51 * @param key the resource name
52 * @param def the default value
53 * @return the string
54 */
55 public static String getString(String key, String def) {
56 try {
57 return bundle.getString(key);
58 } catch (MissingResourceException e) {
59 return def;
60 }
61 }
62 }