diff options
Diffstat (limited to 'src/main/java/com/c2kernel/gui/ImageLoader.java')
| -rw-r--r-- | src/main/java/com/c2kernel/gui/ImageLoader.java | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/main/java/com/c2kernel/gui/ImageLoader.java b/src/main/java/com/c2kernel/gui/ImageLoader.java new file mode 100644 index 0000000..f03e1dd --- /dev/null +++ b/src/main/java/com/c2kernel/gui/ImageLoader.java @@ -0,0 +1,68 @@ +package com.c2kernel.gui;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Hashtable;
+
+import javax.swing.ImageIcon;
+
+import com.c2kernel.common.ObjectNotFoundException;
+import com.c2kernel.utils.Logger;
+import com.c2kernel.utils.Resource;
+
+public class ImageLoader {
+
+ static private Hashtable<String, ImageIcon> imgCache = new Hashtable<String, ImageIcon>();
+ static public final ImageIcon nullImg = new ImageIcon(new byte[] { 0 });
+
+ /**
+ * Gets an image from the resource directories
+ *
+ * @param resName - filename after resources/images
+ * @return
+ */
+ static public ImageIcon findImage(String resName) {
+ try {
+ for (String ns : Resource.getModuleBaseURLs().keySet()) {
+ try {
+ return getImage(ns, resName);
+ } catch (ObjectNotFoundException ex) { }
+ }
+ return getImage(null, resName);
+ } catch (ObjectNotFoundException ex) {
+ Logger.warning("Image '"+resName+"' not found. Using null icon");
+ return nullImg;
+ }
+ }
+
+ static public ImageIcon getImage(String ns, String resName) throws ObjectNotFoundException {
+ if (resName == null)
+ return nullImg;
+
+ if (imgCache.containsKey(ns+'/'+resName)) {
+ return imgCache.get(ns+'/'+resName);
+ }
+
+ URL imgLocation = null;
+ if (ns == null)
+ try {
+ imgLocation = Resource.getKernelResourceURL("images/"+resName);
+ } catch (MalformedURLException ex) { }
+ else
+ try {
+ imgLocation = Resource.getModuleResourceURL(ns, "images/"+resName);
+ } catch (MalformedURLException ex) { }
+
+ if (imgLocation!= null) {
+ ImageIcon newImg = new ImageIcon(imgLocation);
+
+ if (newImg.getIconHeight() > -1) {
+ imgCache.put(ns+'/'+resName, newImg);
+ Logger.msg(0, "Loaded "+resName+" "+newImg.getIconWidth()+"x"+newImg.getIconHeight());
+ return newImg;
+ }
+ }
+ throw new ObjectNotFoundException();
+ }
+
+}
|
