1 package net.logAnalyzer.resources;
2
3 import java.util.Locale;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6 import java.util.Vector;
7
8 import javax.swing.ImageIcon;
9
10 /***
11 * Used to retreive a Localized string in a resource propertie file.
12 *
13 * @author GKDZ7572
14 */
15 public class LAResourceBundle {
16 /***
17 * Localized text strings.
18 */
19 private static ResourceBundle textResource;
20
21 /***
22 * Hidden constructor.
23 *
24 */
25 private LAResourceBundle() {
26 }
27
28 public static ImageIcon getIcon(String name) {
29 return new ImageIcon(LAResourceBundle.class
30 .getResource(LAResourceBundle.getLocalizedString(name)));
31 }
32
33 public static ImageIcon getIcon(String name, String param) {
34 return new ImageIcon(LAResourceBundle.class
35 .getResource(LAResourceBundle.getLocalizedString(name, param)));
36 }
37
38 public static ImageIcon getIcon(String name, Vector params) {
39 return new ImageIcon(LAResourceBundle.class
40 .getResource(LAResourceBundle.getLocalizedString(name, params)));
41 }
42
43 /***
44 * Gets a Localized string in the resource. If another ressource name is
45 * presents between two "$$", then it is replaced by its own value.
46 *
47 * @param name
48 * Name of the property
49 * @return Localized value
50 */
51 public static String getLocalizedString(String name) {
52 try {
53 if (textResource != null) {
54 StringBuffer string = new StringBuffer(textResource
55 .getString(name));
56 int start = -1;
57 int end = -1;
58 while ((start = string.indexOf("$$")) >= 0
59 && (end = string.indexOf("$$", start + 2)) >= 0) {
60 string.replace(start, end + 2, getLocalizedString(string
61 .substring(start + 2, end)));
62 }
63 return string.toString();
64 } else {
65 return "";
66 }
67 } catch (MissingResourceException mre) {
68 return name;
69 }
70 }
71
72 /***
73 * Gets a Localized string in the resource.
74 *
75 * @param name
76 * Name of the property
77 * @param param
78 * String value to replace in the Localized string
79 * @return Localized value
80 */
81 public static String getLocalizedString(String name, String param) {
82 if (textResource != null) {
83 String label = getLocalizedString(name);
84 label = replaceParam(label, 0, param);
85 return label;
86 } else {
87 return "";
88 }
89
90 }
91
92 /***
93 * Gets a Localized string in the resource.
94 *
95 * @param name
96 * Name of the property
97 * @param params
98 * Vector of values to replace in the Localized string
99 * @return Localized value
100 */
101 public static String getLocalizedString(String name, Vector params) {
102 if (textResource != null) {
103 String label = getLocalizedString(name);
104 for (int i = 0; i < params.size(); i++) {
105 label = replaceParam(label, i, (String) params.elementAt(i));
106 }
107 return label;
108 } else {
109 return "";
110 }
111
112 }
113
114 /***
115 * Replac the pattern "$n" (with "n" a positive unsigned integer) by the
116 * specified value.
117 *
118 * @param label
119 * String with (or without) parameters.
120 * @param numParam
121 * Parameter index (the "n" value in the pattern "$n").
122 * @param valParam
123 * Parameter value
124 * @return String in which the pattern was replaced by the parameter value.
125 */
126 private static String replaceParam(String label, int numParam,
127 String valParam) {
128 String param = "$" + numParam;
129 int i;
130 StringBuffer sb = new StringBuffer();
131 while ((i = label.indexOf(param)) >= 0) {
132 sb.append(label.substring(0, i));
133 sb.append(valParam);
134 label = label.substring(i + param.length());
135 }
136 sb.append(label);
137 return sb.toString();
138 }
139
140 static {
141
142 try {
143 textResource = ResourceBundle.getBundle(
144 "net/logAnalyzer/resources/logAnalyzer", Locale
145 .getDefault());
146 } catch (java.util.MissingResourceException mre) {
147
148 }
149 }
150 }