5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.io.InputStream;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Properties;
17 import javax.swing.ImageIcon;
19 public class ChannelProperties
22 public static final String CHANNEL_PROPERTIES_FILENAME = "channel.props";
24 private static Properties channelProps;
26 private static final Properties defaultProps;
28 private static Map<String, Image> imageMap = new HashMap<String, Image>();
30 private static Map<String, URL> urlMap = new HashMap<String, URL>();
32 private static final ArrayList<Image> iconList;
36 defaultProps = new Properties();
37 // these should be kept up to date, but in real life they should never
38 // actually be used anyway.
39 defaultProps.put("app_name", "Jalview");
40 defaultProps.put("banner", "/default_images/jalview_banner.png");
41 defaultProps.put("logo.16", "/default_images/jalview_logo-16.png");
42 defaultProps.put("logo.32", "/default_images/jalview_logo-32.png");
43 defaultProps.put("logo.38", "/default_images/jalview_logo-38.png");
44 defaultProps.put("logo.48", "/default_images/jalview_logo-48.png");
45 defaultProps.put("logo.64", "/default_images/jalview_logo-64.png");
46 defaultProps.put("logo.128", "/default_images/jalview_logo-128.png");
47 defaultProps.put("logo.256", "/default_images/jalview_logo-256.png");
48 defaultProps.put("logo.512", "/default_images/jalview_logo-512.png");
49 defaultProps.put("rotatable_logo.48",
50 "/default_images/rotatable_jalview_logo-38.png");
51 defaultProps.put("bg_logo.28", "/default_images/barton_group-28.png");
52 defaultProps.put("bg_logo.30", "/default_images/barton_group-30.png");
53 defaultProps.put("bg_logo.32", "/default_images/barton_group-32.png");
54 defaultProps.put("uod_banner.28", "/default_images/UoD_banner-28.png");
55 defaultProps.put("uod_banner.30", "/default_images/UoD_banner-30.png");
56 defaultProps.put("uod_banner.32", "/default_images/UoD_banner-32.png");
57 defaultProps.put("default_appbase",
58 "https://www.jalview.org/getdown/release/1.8");
59 defaultProps.put("preferences.filename", ".jalview_properties");
61 // load channel_properties
62 Properties tryChannelProps = new Properties();
63 URL channelPropsURL = ChannelProperties.class
64 .getResource("/" + CHANNEL_PROPERTIES_FILENAME);
65 if (channelPropsURL == null)
67 // complete failure of channel_properties, set all properties to defaults
68 System.err.println("Failed to find '/" + CHANNEL_PROPERTIES_FILENAME
70 + (channelPropsURL == null ? "null"
71 : channelPropsURL.toString())
72 + "'. Using class defaultProps.");
73 tryChannelProps = defaultProps;
79 InputStream channelPropsIS = channelPropsURL.openStream();
80 tryChannelProps.load(channelPropsIS);
81 channelPropsIS.close();
82 } catch (IOException e)
84 System.err.println(e.getMessage());
88 channelProps = tryChannelProps;
91 * The following slight palava for caching an icon list is so that all sizes of icons
92 * are the same. i.e. if there are /any/ channel_properties icons to use, then _only_
93 * use those channel_properties icons, don't mix in class default icons for missing
94 * sizes. If there are _no_ (usable) channel icons then we can use the class default icons.
96 iconList = new ArrayList<Image>();
97 List<String> sizes = Arrays.asList("16", "32", "48", "64", "128", "256",
99 for (String size : sizes)
102 // not using defaults or class props first time through
103 logo = ChannelProperties.getImage("logo." + size, null, false);
109 // now add the class defaults if there were no channel icons defined
110 if (iconList.size() == 0)
112 for (String size : sizes)
115 String path = defaultProps.getProperty("logo." + size);
116 URL imageURL = ChannelProperties.class.getResource(path);
117 ImageIcon imgIcon = imageURL == null ? null
118 : new ImageIcon(imageURL);
119 logo = imgIcon == null ? null : imgIcon.getImage();
128 protected static void loadProps(File dir)
130 File channelPropsFile = new File(dir, CHANNEL_PROPERTIES_FILENAME);
131 if (channelPropsFile.exists())
135 InputStream is = new FileInputStream(channelPropsFile);
136 channelProps.load(is);
137 } catch (FileNotFoundException e)
139 System.err.println(e.getMessage());
140 } catch (IOException e)
142 System.err.println(e.getMessage());
147 private static Properties channelProps()
152 private static Map<String, Image> imageMap()
157 private static Map<String, URL> urlMap()
163 * getProperty(key) will get property value from channel_properties for key.
164 * If no property for key is found, it will fall back to using the defaultProps defined for this class.
166 public static String getProperty(String key)
168 return getProperty(key, null, true);
172 * getProperty(key, defaultVal) will get property value from channel_properties for key.
173 * If no property for key is found, it will return defaultVal and NOT fall back to the class defaultProps.
175 public static String getProperty(String key, String defaultVal)
177 return getProperty(key, defaultVal, false);
181 * internal method. note that setting useClassDefaultProps=true will ignore the provided defaultVal
183 private static String getProperty(String key, String defaultVal,
184 boolean useClassDefaultProps)
186 if (channelProps() != null)
188 if (channelProps().containsKey(key))
190 return channelProps().getProperty(key,
191 useClassDefaultProps ? defaultProps.getProperty(key)
196 System.err.println("Failed to get channel property '" + key + "'");
203 * getImage(key) returns the channel defined image for property key. If that is null (e.g. due to
204 * no defined channel image or the image file being corrupt/unusable/missing) it uses the image
205 * defined in defaultChannelProps
207 public static Image getImage(String key)
209 return getImage(key, null, true);
213 * getImage(key, defaultImg) will get image associated with value from channel_properties for key.
214 * If no property or associated image for key is found (or is usable), it will return defaultImg
215 * and NOT fall back to the class defaultProps.
217 public static Image getImage(String key, Image defaultImg)
219 return getImage(key, defaultImg, false);
223 * internal method. note that setting useClassDefaultImage=true will ignore the provided defaultImg
225 private static Image getImage(String key, Image defaultImg,
226 boolean useClassDefaultImage)
229 if (imageMap().containsKey(key))
231 img = imageMap().get(key);
233 // Catch a previously untried or failed load
236 String path = getProperty(key, null, useClassDefaultImage);
237 if (path == null) // no channel property or class default property (if
240 return useClassDefaultImage ? null : defaultImg;
243 URL imageURL = ChannelProperties.class.getResource(path);
244 ImageIcon imgIcon = imageURL == null ? null : new ImageIcon(imageURL);
245 img = imgIcon == null ? null : imgIcon.getImage();
249 "Failed to load channel image " + key + "=" + path);
250 if (!useClassDefaultImage)
257 imageMap().put(key, img);
258 urlMap.put(key, imageURL);
265 * Public method to get the URL object pointing to a cached image.
267 public static URL getImageURL(String key)
269 if (getImage(key) != null)
271 if (urlMap().containsKey(key))
273 return urlMap().getOrDefault(key, null);
276 "Do not use getImageURL(key) before using getImage(key...)");
282 * Get a List of Icon images of different sizes.
284 public static ArrayList<Image> getIconList()