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