Source code: org/gendiapo/publisher/Resources.java
1 package org.gendiapo.publisher;
2
3 import java.net.*;
4 import java.util.*;
5 import javax.swing.*;
6
7 import java.awt.event.*;
8 import java.lang.reflect.*;
9
10 import org.merlotxml.merlot.*;
11 import org.merlotxml.util.StringUtil;
12
13 /**
14 * Resource loader for XSLGenDiapo. This accesses resource bundles in a
15 * generalized way
16 */
17
18 public class Resources
19 {
20
21 public static final String UI = "ui";
22 public static final String ERR = "err";
23 public static final String XML = "xml";
24 public static final String FILE = "file";
25
26
27 protected HashMap _bundles = new HashMap();
28
29 protected ClassLoader _classLoader;
30
31 private static Hashtable _keycodes;
32 protected String _path;
33
34 public Resources(Object object) {
35 _classLoader = object.getClass().getClassLoader();
36 _path = object.getClass().getName();
37 int lastDot = _path.lastIndexOf(".");
38 _path = _path.substring(0, lastDot);
39 System.out.println(_path);
40 }
41
42 /**
43 * Returns a string from a resource bundle.
44 * @param bname the application name
45 * @param key the resource key
46 */
47
48 public String getString(String bname, String key)
49 throws MissingResourceException
50 {
51
52 return getStringImpl(bname,key,Locale.getDefault());
53
54 }
55
56
57 /**
58 * Returns a string from a resource bundle.
59 * @param bname the application name
60 * @param key the resource key
61 */
62
63 public String getString(String bname, String key, Locale lc)
64 throws MissingResourceException
65 {
66 return getStringImpl(bname,key,lc);
67
68 }
69
70 /**
71 * Gets an image file and loads it
72 */
73 public ImageIcon getImage(String bname, String key)
74 {
75 return getImageImpl(bname,key,Locale.getDefault());
76
77 }
78
79 public ImageIcon getImage(String bname, String key, Locale lc)
80 {
81 return getImageImpl(bname,key,lc);
82 }
83
84 public KeyStroke getKeyStroke(String bname, String key)
85 {
86
87 String keycode = getString(bname,key);
88 return getKeyStrokeImpl(keycode);
89 }
90
91 public KeyStroke getKeyStroke(String bname, String key, Locale lc)
92 {
93
94 String keycode = getString(bname,key,lc);
95 return getKeyStrokeImpl(keycode);
96 }
97
98
99
100
101 private String getStringImpl(String bname, String key, Locale lc)
102 throws MissingResourceException
103 {
104 ResourceBundle bun = loadBundle(bname,lc);
105 if (bun == null) {
106 throw new MissingResourceException("Bundle not found",bname,key);
107 }
108 String s = bun.getString(key);
109 StringUtil.KeyFinder finder = new MyKeyFinder(bname);
110
111 String ret = StringUtil.lookupKeysInString(s,finder);
112 return ret;
113
114 }
115
116
117 private ResourceBundle loadBundle(String name, Locale locale)
118 {
119 String bundlekey = name +"."+locale.toString();
120
121 ResourceBundle rb = (ResourceBundle)_bundles.get(bundlekey);
122
123 if (rb == null && !_bundles.containsKey(name)) {
124 rb = ResourceBundle.getBundle(_path + "." + name, locale, _classLoader);
125
126 _bundles.put(bundlekey,rb);
127 }
128
129 return rb;
130
131 }
132
133
134 protected ImageIcon getImageImpl(String bname, String key, Locale locale)
135 {
136
137
138 ResourceBundle bun = loadBundle(bname,locale);
139 if (bun == null) {
140 throw new MissingResourceException("Bundle not found",bname,key);
141 }
142 String filename = bun.getString(key);
143 return loadImage(bname,filename);
144
145
146 }
147
148
149 protected ImageIcon loadImage(String bname, String filename)
150 {
151 return loadImage(bname, filename, false) ;
152 }
153
154 protected ImageIcon loadImage(String bname, String filename, boolean recursing)
155 throws MissingResourceException
156 {
157
158 ImageIcon i = null;
159
160 if (filename != null) {
161 try {
162 try {
163 // hack to make images load from a jar
164 // file via getResource when they're within
165 // a subdir. Basically, the class that loads
166 // them via getResource must be in the same dir.
167 URL u = _classLoader.getResource(filename);
168 if (u != null) {
169 i = new ImageIcon(u);
170 }
171 else {
172 u = new URL(filename);
173
174 i = new ImageIcon(u);
175 }
176 }
177 catch (java.net.MalformedURLException mf) {
178 }
179
180 }
181 catch (Exception e) {
182 MerlotDebug.exception(e);
183 }
184 }
185 return i;
186
187
188 }
189
190 protected KeyStroke getKeyStrokeImpl(String keycode)
191 {
192 // String keycode = ResourceCatalog.getString(rr);
193 int modifiers = 0;
194 char c = '\0';
195
196 if (keycode != null) {
197 // now try to parse the keycode
198 StringTokenizer tok = new StringTokenizer(keycode,"-");
199 while (tok.hasMoreTokens()) {
200 String t = tok.nextToken();
201 if (t.equalsIgnoreCase("cmd")) {
202 // platform default command key
203 modifiers |= getCommandKeyMask();
204 }
205 else if (t.equalsIgnoreCase("shift")) {
206 modifiers |= java.awt.Event.SHIFT_MASK;
207 }
208 else if (t.equalsIgnoreCase("ctrl")) {
209 modifiers |= java.awt.Event.CTRL_MASK;
210 }
211 else if (t.equalsIgnoreCase("meta")) {
212 modifiers |= java.awt.Event.META_MASK;
213 }
214 else if (t.equalsIgnoreCase("alt")) {
215 modifiers |= java.awt.Event.ALT_MASK;
216 }
217 else if (t.length() == 1) {
218 c = t.toUpperCase().charAt(0);
219 }
220 else if (t.startsWith("VK_")) {
221
222 c = (char)getKeyCodeNamed(t);
223
224 // XXX get a keycode field via
225 // reflection
226 }
227
228 }
229 if ((int)c > 0) {
230 KeyStroke ks = KeyStroke.getKeyStroke((int)c,modifiers);
231 return ks;
232 }
233 }
234 return null;
235
236 }
237
238 /**
239 * Returns the platform's preferred command key.
240 * This is CTRL on unix and windows, and META on Mac
241 */
242 protected int getCommandKeyMask()
243 {
244 int os = XMLEditorSettings.getOSType();
245 switch (os) {
246 case XMLEditorSettings.MACOS:
247 return java.awt.Event.META_MASK;
248 default:
249 return java.awt.Event.CTRL_MASK;
250 }
251
252 }
253
254
255 protected int getKeyCodeNamed(String n)
256 {
257 if (_keycodes == null) {
258 loadKeyCodes();
259 }
260 Object o = _keycodes.get(n);
261 if (o instanceof Integer) {
262 return ((Integer)o).intValue();
263 }
264 return -1;
265
266 }
267
268
269 protected void loadKeyCodes()
270 {
271 _keycodes = new Hashtable();
272 KeyEvent evt = new KeyEvent(new java.awt.Label(""),0,0L,0,0);
273
274 Field[] f = KeyEvent.class.getDeclaredFields();
275 for (int i=0;i<f.length;i++) {
276 String name = f[i].getName();
277 if (name.startsWith("VK_")) {
278 try {
279 int val = f[i].getInt(evt);
280 _keycodes.put(name,new Integer(val));
281 // MerlotDebug.msg("keycode: "+name+" = "+val);
282
283 }
284 catch (Exception ex) {
285 }
286
287 }
288
289
290 }
291
292
293 }
294
295 protected static class MyKeyFinder implements StringUtil.KeyFinder
296 {
297 String _bundle;
298
299 public MyKeyFinder(String bundle)
300 {
301 _bundle = bundle;
302 }
303
304 public String lookupString(String key)
305 {
306 String ret = null;
307 try {
308 ret = MerlotResource.getString(_bundle,key);
309 }
310 catch (Exception ex) {
311 }
312 if (ret == null) {
313 ret = XMLEditorSettings.getSharedInstance().getProperty(key);
314 }
315 return ret;
316 }
317
318
319 }
320
321 }