0b165c76145d5f0fc235ca2114efa891477215e0
[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 static final Properties defaultProps;
23
24   private Map<String, Image> imageMap = new HashMap<String, Image>();
25
26   private static ChannelProperties instance;
27
28   private boolean initDone = false;
29
30   static
31   {
32     defaultProps = new Properties();
33     // these should be kept up to date, but in real life they should never
34     // actually be used anyway.
35     defaultProps.put("app_name", "Jalview");
36     defaultProps.put("banner", "/default_images/jalview_banner.png");
37     defaultProps.put("logo.16", "/default_images/jalview_logo-16.png");
38     defaultProps.put("logo.32", "/default_images/jalview_logo-32.png");
39     defaultProps.put("logo.38", "/default_images/jalview_logo-38.png");
40     defaultProps.put("logo.48", "/default_images/jalview_logo-48.png");
41     defaultProps.put("logo.64", "/default_images/jalview_logo-64.png");
42     defaultProps.put("logo.128", "/default_images/jalview_logo-128.png");
43     defaultProps.put("logo.256", "/default_images/jalview_logo-256.png");
44     defaultProps.put("logo.512", "/default_images/jalview_logo-512.png");
45     defaultProps.put("rotatable_logo.48",
46             "/default_images/rotatable_jalview_logo-38.png");
47     defaultProps.put("bg_logo.62", "/default_images/barton_group-62.png");
48     defaultProps.put("uod_banner", "/default_images/UoD_banner.png");
49     defaultProps.put("default_appbase",
50             "https://www.jalview.org/getdown/release/1.8");
51   }
52
53   private boolean init()
54   {
55     if (initDone)
56     {
57       return initDone;
58     }
59     // load channel_properties
60     URL channelPropsURL = getClass()
61             .getResource(CHANNEL_PROPERTIES_FILENAME);
62     if (channelPropsURL == null)
63     {
64       // fallback
65       System.err.println("Failed to find '" + CHANNEL_PROPERTIES_FILENAME
66               + "' filem using defaults");
67       channelProps = defaultProps;
68       return false;
69     }
70     else
71     {
72       try
73       {
74         InputStream channelPropsIS = channelPropsURL.openStream();
75         channelProps.load(channelPropsIS);
76         channelPropsIS.close();
77       } catch (IOException e)
78       {
79         Cache.log.warn(e.getMessage());
80         return false;
81       }
82     }
83     initDone = true;
84     return initDone;
85   }
86
87   private ChannelProperties()
88   {
89     init();
90   }
91
92   public static ChannelProperties getChannelProperties()
93   {
94     if (instance == null)
95     {
96       instance = new ChannelProperties();
97     }
98     return instance;
99   }
100
101   private static Properties channelProps()
102   {
103     return getChannelProperties().channelProps;
104   }
105
106   private static Map<String, Image> imageMap()
107   {
108     return getChannelProperties().imageMap;
109   }
110
111   /*
112    * getProperty(key) will get property value from channel_properties for key.
113    * If no property for key is found, it will fall back to using the defaultProps defined for this class.
114    */
115   public static String getProperty(String key)
116   {
117     return getProperty(key, null, true);
118   }
119
120   /*
121    * getProperty(key, defaultVal) will get property value from channel_properties for key.
122    * If no property for key is found, it will return defaultVal and NOT fall back to the class defaultProps.
123    */
124   public static String getProperty(String key, String defaultVal)
125   {
126     return getProperty(key, defaultVal, false);
127   }
128
129   private static String getProperty(String key, String defaultVal,
130           boolean useClassDefaultProps)
131   {
132     if (channelProps() != null)
133     {
134       if (channelProps().containsKey(key))
135       {
136         return channelProps().getProperty(key,
137                 useClassDefaultProps ? defaultProps.getProperty(key)
138                         : defaultVal);
139       }
140       else
141       {
142         System.err.println("Failed to get channel property '" + key + "'");
143       }
144     }
145     return null;
146   }
147
148   public static Image getImage(String key)
149   {
150     Image img = null;
151     if (imageMap().containsKey(key))
152     {
153       img = imageMap().get(key);
154     }
155     // Catch a previously untried or failed load
156     if (img == null)
157     {
158       String path = getProperty(key);
159       if (path == null)
160       {
161         return null;
162       }
163       URL imageURL = getChannelProperties().getClass().getResource(path);
164       img = new ImageIcon(imageURL).getImage();
165       if (img == null)
166       {
167         System.err.println(
168                 "Failed to load channel image " + key + "=" + path);
169       }
170       else
171       {
172         imageMap().put(key, img);
173       }
174     }
175     return img;
176   }
177 }