4 import java.io.IOException;
5 import java.io.InputStream;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.HashMap;
10 import java.util.List;
12 import java.util.Properties;
14 import javax.swing.ImageIcon;
16 import jalview.bin.Cache;
18 public class ChannelProperties
21 private static final String CHANNEL_PROPERTIES_FILENAME = "/channel.props";
23 private static final Properties channelProps;
25 private static final Properties defaultProps;
27 private static Map<String, Image> imageMap = new HashMap<String, Image>();
29 private static Map<String, URL> urlMap = new HashMap<String, URL>();
31 private static final ArrayList<Image> iconList;
35 defaultProps = new Properties();
36 // these should be kept up to date, but in real life they should never
37 // actually be used anyway.
38 defaultProps.put("app_name", "Jalview");
39 defaultProps.put("banner", "/default_images/jalview_banner.png");
40 defaultProps.put("logo.16", "/default_images/jalview_logo-16.png");
41 defaultProps.put("logo.32", "/default_images/jalview_logo-32.png");
42 defaultProps.put("logo.38", "/default_images/jalview_logo-38.png");
43 defaultProps.put("logo.48", "/default_images/jalview_logo-48.png");
44 defaultProps.put("logo.64", "/default_images/jalview_logo-64.png");
45 defaultProps.put("logo.128", "/default_images/jalview_logo-128.png");
46 defaultProps.put("logo.256", "/default_images/jalview_logo-256.png");
47 defaultProps.put("logo.512", "/default_images/jalview_logo-512.png");
48 defaultProps.put("rotatable_logo.48",
49 "/default_images/rotatable_jalview_logo-38.png");
50 defaultProps.put("bg_logo.28", "/default_images/barton_group-28.png");
51 defaultProps.put("bg_logo.30", "/default_images/barton_group-30.png");
52 defaultProps.put("bg_logo.32", "/default_images/barton_group-32.png");
53 defaultProps.put("uod_banner.28", "/default_images/UoD_banner-28.png");
54 defaultProps.put("uod_banner.30", "/default_images/UoD_banner-30.png");
55 defaultProps.put("uod_banner.32", "/default_images/UoD_banner-32.png");
56 defaultProps.put("default_appbase",
57 "https://www.jalview.org/getdown/release/1.8");
58 defaultProps.put("preferences.filename", ".jalview_properties");
60 // load channel_properties
61 Properties tryChannelProps = new Properties();
62 URL channelPropsURL = ChannelProperties.class
63 .getResource(CHANNEL_PROPERTIES_FILENAME);
64 if (channelPropsURL == null)
66 // complete failure of channel_properties, set all properties to defaults
67 System.err.println("Failed to find '" + CHANNEL_PROPERTIES_FILENAME
69 + (channelPropsURL == null ? "null"
70 : channelPropsURL.toString())
71 + "'. Using class defaultProps.");
72 tryChannelProps = defaultProps;
78 InputStream channelPropsIS = channelPropsURL.openStream();
79 tryChannelProps.load(channelPropsIS);
80 channelPropsIS.close();
81 } catch (IOException e)
83 Cache.log.warn(e.getMessage());
87 channelProps = tryChannelProps;
90 * The following slight palava for caching an icon list is so that all sizes of icons
91 * are the same. i.e. if there are /any/ channel_properties icons to use, then _only_
92 * use those channel_properties icons, don't mix in class default icons for missing
93 * sizes. If there are _no_ (usable) channel icons then we can use the class default icons.
95 iconList = new ArrayList<Image>();
96 List<String> sizes = Arrays.asList("16", "32", "48", "64", "128", "256",
98 for (String size : sizes)
101 // not using defaults or class props first time through
102 logo = ChannelProperties.getImage("logo." + size, null, false);
108 // now add the class defaults if there were no channel icons defined
109 if (iconList.size() == 0)
111 for (String size : sizes)
114 String path = defaultProps.getProperty("logo." + size);
115 URL imageURL = ChannelProperties.class.getResource(path);
116 logo = new ImageIcon(imageURL).getImage();
125 private static Properties channelProps()
130 private static Map<String, Image> imageMap()
135 private static Map<String, URL> urlMap()
141 * getProperty(key) will get property value from channel_properties for key.
142 * If no property for key is found, it will fall back to using the defaultProps defined for this class.
144 public static String getProperty(String key)
146 return getProperty(key, null, true);
150 * getProperty(key, defaultVal) will get property value from channel_properties for key.
151 * If no property for key is found, it will return defaultVal and NOT fall back to the class defaultProps.
153 public static String getProperty(String key, String defaultVal)
155 return getProperty(key, defaultVal, false);
159 * internal method. note that setting useClassDefaultProps=true will ignore the provided defaultVal
161 private static String getProperty(String key, String defaultVal,
162 boolean useClassDefaultProps)
164 if (channelProps() != null)
166 if (channelProps().containsKey(key))
168 return channelProps().getProperty(key,
169 useClassDefaultProps ? defaultProps.getProperty(key)
174 System.err.println("Failed to get channel property '" + key + "'");
181 * getImage(key) returns the channel defined image for property key. If that is null (e.g. due to
182 * no defined channel image or the image file being corrupt/unusable/missing) it uses the image
183 * defined in defaultChannelProps
185 public static Image getImage(String key)
187 return getImage(key, null, true);
191 * getImage(key, defaultImg) will get image associated with value from channel_properties for key.
192 * If no property or associated image for key is found (or is usable), it will return defaultImg
193 * and NOT fall back to the class defaultProps.
195 public static Image getImage(String key, Image defaultImg)
197 return getImage(key, defaultImg, false);
201 * internal method. note that setting useClassDefaultImage=true will ignore the provided defaultImg
203 private static Image getImage(String key, Image defaultImg,
204 boolean useClassDefaultImage)
207 if (imageMap().containsKey(key))
209 img = imageMap().get(key);
211 // Catch a previously untried or failed load
214 String path = getProperty(key, null, useClassDefaultImage);
215 if (path == null) // no channel property or class default property (if
218 return useClassDefaultImage ? null : defaultImg;
221 URL imageURL = ChannelProperties.class.getResource(path);
222 img = new ImageIcon(imageURL).getImage();
226 "Failed to load channel image " + key + "=" + path);
227 if (!useClassDefaultImage)
234 imageMap().put(key, img);
235 urlMap.put(key, imageURL);
242 * Public method to get the URL object pointing to a cached image.
244 public static URL getImageURL(String key)
246 if (getImage(key) != null)
248 if (urlMap().containsKey(key))
250 return urlMap().getOrDefault(key, null);
253 "Do not use getImageURL(key) before using getImage(key...)");
259 * Get a List of Icon images of different sizes.
261 public static ArrayList<Image> getIconList()