KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > chat > resourceloader > ResourceLoader


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.chat.resourceloader;
19
20 import java.net.URL JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.MissingResourceException JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24
25 import javax.swing.ImageIcon JavaDoc;
26
27 import org.columba.core.io.DiskIO;
28 import org.columba.core.resourceloader.GlobalResourceLoader;
29
30 public class ResourceLoader {
31
32     private static final String JavaDoc ICON_PATH = "/org/columba/chat/icons";
33
34     private static String JavaDoc i18nPath = "org.columba.chat.i18n";
35
36     public static ImageIcon JavaDoc getMiscIcon(String JavaDoc resourceName) {
37         if (resourceName == null)
38             throw new IllegalArgumentException JavaDoc("resourceName == null");
39
40         URL JavaDoc url = ResourceLoader.class.getResource(ResourceLoader.ICON_PATH + "/MISC/"
41                 + resourceName);
42
43         if (url == null)
44             url = getFallback(true);
45
46         ImageIcon JavaDoc icon = new ImageIcon JavaDoc(url);
47
48         return icon;
49     }
50
51     public static ImageIcon JavaDoc getIcon(String JavaDoc name) {
52         return getIcon(ResourceLoader.ICON_PATH, name, false);
53     }
54
55     public static ImageIcon JavaDoc getSmallIcon(String JavaDoc name) {
56         return getIcon(ResourceLoader.ICON_PATH, name, true);
57     }
58
59     public static ImageIcon JavaDoc getIcon(String JavaDoc path, String JavaDoc name, boolean small) {
60         URL JavaDoc url;
61
62         if (small)
63             url = ResourceLoader.class.getResource(path + "/16x16/" + name);
64         else
65             url = ResourceLoader.class.getResource(path + "/22x22/" + name);
66
67         if (url == null)
68             url = getFallback(small);
69
70         ImageIcon JavaDoc icon = new ImageIcon JavaDoc(url);
71
72         return icon;
73     }
74
75     /**
76      * @param small
77      * @return
78      */

79     private static URL JavaDoc getFallback(boolean small) {
80         String JavaDoc path;
81         String JavaDoc name;
82         URL JavaDoc url;
83         path = "/org/columba/core/icons";
84         name = "image-missing.png";
85         if (small)
86             url = ResourceLoader.class.getResource(path + "/16x16/" + name);
87         else
88             url = ResourceLoader.class.getResource(path + "/22x22/" + name);
89         return url;
90     }
91
92     public static final String JavaDoc getString(String JavaDoc resourceBundleName,
93             String JavaDoc resourceName) {
94         ResourceBundle JavaDoc bundle = null;
95
96         String JavaDoc bundlePath = i18nPath + "." + resourceBundleName;
97
98         try {
99             bundle = ResourceBundle.getBundle(bundlePath, Locale.getDefault());
100
101             return bundle.getString(resourceName);
102         } catch (MissingResourceException JavaDoc e) {
103             
104             // fall-back to global resource loader
105
return GlobalResourceLoader.getString(null, resourceBundleName,
106                     resourceName);
107         }
108     }
109 }
110
Popular Tags