/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

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