JAL-3594 New jalview.util.ChannelProperties to get/cache values and images used in...
[jalview.git] / src / jalview / util / ChannelProperties.java
diff --git a/src/jalview/util/ChannelProperties.java b/src/jalview/util/ChannelProperties.java
new file mode 100644 (file)
index 0000000..5d7f795
--- /dev/null
@@ -0,0 +1,136 @@
+package jalview.util;
+
+import java.awt.Image;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.swing.ImageIcon;
+
+import jalview.bin.Cache;
+
+public class ChannelProperties
+{
+
+  private static final String CHANNEL_PROPERTIES_FILENAME = "/channel_properties";
+
+  private Properties channelProps = new Properties();
+
+  private Map<String, Image> imageMap = new HashMap<String, Image>();
+
+  private static ChannelProperties instance;
+
+  private boolean initDone = false;
+
+  private boolean init()
+  {
+    if (initDone)
+    {
+      return initDone;
+    }
+    // load channel_properties
+    URL channelPropsURL = getClass()
+            .getResource(CHANNEL_PROPERTIES_FILENAME);
+    if (channelPropsURL == null)
+    {
+      // fallback
+      System.err.println(
+              "Failed to find '" + CHANNEL_PROPERTIES_FILENAME + "' file");
+      return false;
+    }
+    else
+    {
+      try
+      {
+        InputStream channelPropsIS = channelPropsURL.openStream();
+        channelProps.load(channelPropsIS);
+        channelPropsIS.close();
+      } catch (IOException e)
+      {
+        Cache.log.warn(e.getMessage());
+        return false;
+      }
+    }
+    initDone = true;
+    return initDone;
+  }
+
+  private ChannelProperties()
+  {
+    init();
+  }
+
+  public static ChannelProperties getChannelProperties()
+  {
+    if (instance == null)
+    {
+      instance = new ChannelProperties();
+    }
+    return instance;
+  }
+
+  public static String getProperty(String key)
+  {
+    return getProperty(key, null);
+  }
+
+  private static Properties channelProps()
+  {
+    return getChannelProperties().channelProps;
+  }
+
+  private static Map<String, Image> imageMap()
+  {
+    return getChannelProperties().imageMap;
+  }
+
+  public static String getProperty(String key, String defaultVal)
+  {
+    if (channelProps() != null)
+    {
+      if (channelProps().containsKey(key))
+      {
+        String val = channelProps().getProperty(key, "NOTFOUND");
+        return channelProps().getProperty(key, defaultVal);
+      }
+      else
+      {
+        System.err.println("Failed to get channel property '" + key + "'");
+      }
+    }
+    return null;
+  }
+
+  public static Image getImage(String key)
+  {
+    Image img = null;
+    if (imageMap().containsKey(key))
+    {
+      img = imageMap().get(key);
+    }
+    // Catch a previously untried or failed load
+    if (img == null)
+    {
+      String path = getProperty(key);
+      if (path == null)
+      {
+        return null;
+      }
+      URL imageURL = getChannelProperties().getClass().getResource(path);
+      img = new ImageIcon(imageURL).getImage();
+      if (img == null)
+      {
+        System.err.println(
+                "Failed to load channel image " + key + "=" + path);
+      }
+      else
+      {
+        imageMap().put(key, img);
+      }
+    }
+    return img;
+  }
+}
\ No newline at end of file