1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package inc.che.common.resource;
20
21
22
23 import inc.che.common.string.StringUtil;
24
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Locale;
31 import java.util.MissingResourceException;
32 import java.util.ResourceBundle;
33
34 import javax.swing.ImageIcon;
35
36 import org.apache.log4j.Logger;
37
38 /***
39 * <b>ResourceManager für Textmeldungen</b>
40 * @version $Id: ResourceManager.java,v 1.1 2005/03/06 12:57:02 stevemcmee Exp $
41 * @author <address> Steve McMee <stevemcmee@users.sourceforge.net> </address>
42 */
43
44 public final class ResourceManager {
45
46 /*** CVS ID of this file */
47 public static final String CVS_ID =
48 "$Id: ResourceManager.java,v 1.1 2005/03/06 12:57:02 stevemcmee Exp $";
49
50 /***
51 * logger instance for this class
52 */
53
54 private static Logger log = Logger.getLogger(ResourceManager.class);
55
56 private ResourceBundle resourceBundle = null;
57 private String resourceFile = null;
58 private Locale locale;
59
60 /*** The Managers */
61
62 private static HashMap managers = null;
63
64 private static ArrayList keysNotFound = new ArrayList();
65
66 /***
67 * forbidden Construcor
68 */
69
70 private ResourceManager() {
71 }
72
73 public static ResourceManager getCommonsResourceManager() {
74 return ResourceManager.getResourceManager(
75 StringResources.TEXT_RESOURCES);
76 }
77 public static ResourceManager getResourceManager(String resourceFile) {
78
79 if (managers == null) {
80 managers = new HashMap();
81 }
82 if (managers.get(resourceFile) == null) {
83 ResourceManager manager = new ResourceManager();
84 manager.resourceBundle = ResourceBundle.getBundle(resourceFile);
85 manager.locale = manager.resourceBundle.getLocale();
86 manager.resourceFile = resourceFile;
87 managers.put(resourceFile, manager);
88 }
89 return (ResourceManager) managers.get(resourceFile);
90
91 }
92
93 public void setLocale(Locale locale) {
94 if (log.isDebugEnabled()) {
95 log.debug("setLocale(" + locale + ")");
96 }
97 if (log.isDebugEnabled()) {
98 log.debug("resourceFile:" + resourceFile);
99 }
100 this.locale = locale;
101 this.resourceBundle = ResourceBundle.getBundle(resourceFile, locale);
102 }
103 public Locale getLocale() {
104 return locale;
105 }
106
107 public String getText(String id) {
108 return getText(id, null);
109 }
110
111 public String getText(String id, String[] parameter) {
112 if (log.isDebugEnabled()) {
113 log.debug("getText(" + id + ")");
114 }
115 String text = null;
116 try {
117 text = resourceBundle.getString(id);
118 } catch (MissingResourceException ex) {
119 ex.printStackTrace();
120 keysNotFound.add(id);
121 log.error("Cannot find TextresourceID " + id);
122 }
123 if (text == null) {
124 text = "[Cannot find TextresourceID '" + id + "']";
125 } else {
126 if (parameter != null) {
127 for (int i = 0; i < parameter.length; i++) {
128 if (parameter[i] != null) {
129 text =
130 StringUtil.replace(
131 text,
132 "%" + (i + 1),
133 parameter[i]);
134 }
135 }
136 }
137 }
138 if (log.isDebugEnabled()) {
139 log.debug("getText(" + id + ")->" + text);
140 }
141 return text;
142 }
143
144 public InputStream getResource(String name) {
145 String filename = getText(name);
146 return ResourceManager.class.getClassLoader().getResourceAsStream(
147 filename);
148 }
149 public ImageIcon getIconByGifName(String name) {
150 ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
151 ImageIcon icon = null;
152 try {
153
154 String filename = getText(name);
155 InputStream is =
156 ResourceManager.class.getClassLoader().getResourceAsStream(
157 filename);
158 if (is != null) {
159 int c = -1;
160 while ((c = is.read()) != -1) {
161 imageStream.write(c);
162 }
163 is.close();
164 imageStream.close();
165 icon = new ImageIcon(imageStream.toByteArray());
166 } else {
167 log.error("Cannot load image " + name + " " + filename);
168 }
169 } catch (IOException ex) {
170 log.error("Cannot load image " + name, ex);
171 }
172 return icon;
173 }
174
175 public ImageIcon getIconByGifPath(String name) {
176 ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
177 ImageIcon icon = null;
178 try {
179 String filename = name;
180 InputStream is =
181 ResourceManager.class.getClassLoader().getResourceAsStream(
182 filename);
183 if (is != null) {
184 int c = -1;
185 while ((c = is.read()) != -1) {
186 imageStream.write(c);
187 }
188 is.close();
189 imageStream.close();
190 icon = new ImageIcon(imageStream.toByteArray());
191 } else {
192 log.error("Cannot load image " + name);
193 }
194 } catch (IOException ex) {
195 log.error("Cannot load image " + name, ex);
196 }
197 return icon;
198 }
199
200 public ArrayList getMissingKeys() {
201 return this.keysNotFound;
202 }
203 }