5d7f7958063388df4795caf42301f520cccd5851
[jalview.git] / src / jalview / util / ChannelProperties.java
1 package jalview.util;
2
3 import java.awt.Image;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
7 import java.util.HashMap;
8 import java.util.Map;
9 import java.util.Properties;
10
11 import javax.swing.ImageIcon;
12
13 import jalview.bin.Cache;
14
15 public class ChannelProperties
16 {
17
18   private static final String CHANNEL_PROPERTIES_FILENAME = "/channel_properties";
19
20   private Properties channelProps = new Properties();
21
22   private Map<String, Image> imageMap = new HashMap<String, Image>();
23
24   private static ChannelProperties instance;
25
26   private boolean initDone = false;
27
28   private boolean init()
29   {
30     if (initDone)
31     {
32       return initDone;
33     }
34     // load channel_properties
35     URL channelPropsURL = getClass()
36             .getResource(CHANNEL_PROPERTIES_FILENAME);
37     if (channelPropsURL == null)
38     {
39       // fallback
40       System.err.println(
41               "Failed to find '" + CHANNEL_PROPERTIES_FILENAME + "' file");
42       return false;
43     }
44     else
45     {
46       try
47       {
48         InputStream channelPropsIS = channelPropsURL.openStream();
49         channelProps.load(channelPropsIS);
50         channelPropsIS.close();
51       } catch (IOException e)
52       {
53         Cache.log.warn(e.getMessage());
54         return false;
55       }
56     }
57     initDone = true;
58     return initDone;
59   }
60
61   private ChannelProperties()
62   {
63     init();
64   }
65
66   public static ChannelProperties getChannelProperties()
67   {
68     if (instance == null)
69     {
70       instance = new ChannelProperties();
71     }
72     return instance;
73   }
74
75   public static String getProperty(String key)
76   {
77     return getProperty(key, null);
78   }
79
80   private static Properties channelProps()
81   {
82     return getChannelProperties().channelProps;
83   }
84
85   private static Map<String, Image> imageMap()
86   {
87     return getChannelProperties().imageMap;
88   }
89
90   public static String getProperty(String key, String defaultVal)
91   {
92     if (channelProps() != null)
93     {
94       if (channelProps().containsKey(key))
95       {
96         String val = channelProps().getProperty(key, "NOTFOUND");
97         return channelProps().getProperty(key, defaultVal);
98       }
99       else
100       {
101         System.err.println("Failed to get channel property '" + key + "'");
102       }
103     }
104     return null;
105   }
106
107   public static Image getImage(String key)
108   {
109     Image img = null;
110     if (imageMap().containsKey(key))
111     {
112       img = imageMap().get(key);
113     }
114     // Catch a previously untried or failed load
115     if (img == null)
116     {
117       String path = getProperty(key);
118       if (path == null)
119       {
120         return null;
121       }
122       URL imageURL = getChannelProperties().getClass().getResource(path);
123       img = new ImageIcon(imageURL).getImage();
124       if (img == null)
125       {
126         System.err.println(
127                 "Failed to load channel image " + key + "=" + path);
128       }
129       else
130       {
131         imageMap().put(key, img);
132       }
133     }
134     return img;
135   }
136 }