PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/razpub/src/com/razie/pub/resources/RazIconRes.java

http://razpub.googlecode.com/
Java | 63 lines | 40 code | 10 blank | 13 comment | 15 complexity | 7534afc65722dd484718f7da4b337eec MD5 | raw file
 1package com.razie.pub.resources;
 2
 3import java.io.IOException;
 4import java.net.URL;
 5import java.util.Properties;
 6
 7import com.razie.pub.base.log.Log;
 8
 9/**
10 * just to proxy/wrap icons functionality, in case i introduce "themes" later - a theme would be a
11 * different icons.properties file for now
12 * 
13 * if you want to use this, have an icons.properties at the root in the classpath
14 * 
15 * TODO implement registry of cascaded property files etc
16 * 
17 * @author razvanc99
18 * 
19 */
20public class RazIconRes {
21    public static String            curTheme = "icons.properties";
22    public static String            getPictureService = "/classpath/public/pics/";
23    public static String            PIC_CLASSPATH = "/public/pics/";
24    public static String            UNK_CLASSPATH = "/public/pics/help_index.png";
25    static Properties props    = new Properties();
26
27    public static void init() throws IOException {
28        if (RazIconRes.class.getClassLoader().getResource(curTheme) == null)
29           throw new IllegalStateException ("ERR_CONFIG missing resource (should be in classpath): "+curTheme);
30        props.load(RazIconRes.class.getClassLoader().getResource(curTheme).openStream());
31    }
32
33    public static String getIconFile(RazIcons icon) {
34        return getIconFile(icon.name());
35    }
36
37    /** the actual url to pic (on server at runtime) or empty */
38    public static String getIconFile(String icon) {
39        if (icon == null || icon.length() <= 0)
40            icon = razie.Icons.UNKNOWN.toString();
41        String f = props.getProperty(icon.toLowerCase());
42        return f == null ? icon : getPictureService + f;
43    }
44
45    /** use this version for Swing local applications - will return classic icon URL in classpath */
46    public static URL getIconRes(String icon) {
47        if (icon == null || icon.length() <= 0)
48            icon = razie.Icons.UNKNOWN.toString();
49        String f = props.getProperty(icon.toLowerCase());
50        if (f == null) {
51            Log.logThis("ERR_PROG: cant find icon resource for icon code: " + icon);
52        }
53        
54        URL ret = RazIconRes.class.getResource(f == null ? UNK_CLASSPATH : PIC_CLASSPATH + f);
55        if (ret == null) {
56            Log.logThis("ERR_PROG: cant find icon resource for icon code: " + f);
57        }
58        
59        return ret != null ? ret : RazIconRes.class.getResource(UNK_CLASSPATH);
60    }
61
62    Properties p;
63}