KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > 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.calendar.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.resourceloader.GlobalResourceLoader;
28
29 /**
30  * ResourceLoader class
31  * @author unknown
32  *
33  */

34 public class ResourceLoader {
35     private static String JavaDoc ICON_PATH = "/org/columba/calendar/icons";
36     private static String JavaDoc i18nPath = "org.columba.calendar.i18n";
37     
38     /**
39      * getMiscIcon method
40      * @param resourceName
41      * @return icon
42      */

43     public static ImageIcon JavaDoc getMiscIcon(String JavaDoc resourceName) {
44         if (resourceName == null)
45             throw new IllegalArgumentException JavaDoc("resourceName == null");
46
47         URL JavaDoc url = ResourceLoader.class.getResource(ResourceLoader.ICON_PATH
48                 + "/MISC/" + resourceName);
49
50         if (url == null)
51             url = getFallback(true);
52
53         ImageIcon JavaDoc icon = new ImageIcon JavaDoc(url);
54         return icon;
55     }
56
57     /**
58      * getIcon method
59      * @param name
60      * @return standard icon
61      */

62     public static ImageIcon JavaDoc getIcon(String JavaDoc name) {
63         return getIcon(ResourceLoader.ICON_PATH, name, false);
64     }
65
66     /**
67      * getSmallIcon method
68      * @param name
69      * @return small icon
70      */

71     public static ImageIcon JavaDoc getSmallIcon(String JavaDoc name) {
72         return getIcon(ResourceLoader.ICON_PATH, name, true);
73     }
74
75     /**
76      * getIcon method
77      * @param path
78      * @param name
79      * @param small
80      * @return icon
81      */

82     public static ImageIcon JavaDoc getIcon(String JavaDoc path, String JavaDoc name, boolean small) {
83         URL JavaDoc url;
84
85         if (small)
86             url = ResourceLoader.class.getResource(path + "/16x16/" + name);
87         else
88             url = ResourceLoader.class.getResource(path + "/22x22/" + name);
89
90         if (url == null)
91             url = getFallback(small);
92
93         ImageIcon JavaDoc icon = new ImageIcon JavaDoc(url);
94
95         return icon;
96     }
97
98     /**
99      * GetFallback method - returns correct size image-missing icon if other icon does not exist
100      * @param small
101      * @return icon
102      */

103     private static URL JavaDoc getFallback(boolean small) {
104         String JavaDoc path;
105         String JavaDoc name;
106         URL JavaDoc url;
107         path = "org/columba/core/icons";
108         name = "image-missing.png";
109         if (small)
110
111             url = ResourceLoader.class.getResource(path + "/16x16/" + name);
112         else
113             url = ResourceLoader.class.getResource(path + "/22x22/" + name);
114         return url;
115     }
116
117     /**
118      * getString method - gets i18n bundle name
119      * @param resourceBundleName
120      * @param resourceName
121      * @return resource bundle
122      */

123     public static final String JavaDoc getString(String JavaDoc resourceBundleName,
124             String JavaDoc resourceName) {
125         ResourceBundle JavaDoc bundle = null;
126         String JavaDoc bundlePath = i18nPath + "." + resourceBundleName;
127
128         try {
129             bundle = ResourceBundle.getBundle(bundlePath, Locale.getDefault());
130
131             return bundle.getString(resourceName);
132         } catch (MissingResourceException JavaDoc e) {
133
134             // fall-back to global resource loader
135
return GlobalResourceLoader.getString(null, resourceBundleName,
136                     resourceName);
137         }
138     }
139 }
140
Popular Tags