package jalview.util; import java.awt.Image; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.swing.ImageIcon; import jalview.bin.Cache; public class ChannelProperties { private static final String CHANNEL_PROPERTIES_FILENAME = "/channel_properties"; private Properties channelProps = new Properties(); private static final Properties defaultProps; private Map imageMap = new HashMap(); private static ChannelProperties instance; private boolean initDone = false; static { defaultProps = new Properties(); // these should be kept up to date, but in real life they should never // actually be used anyway. defaultProps.put("app_name", "Jalview"); defaultProps.put("banner", "/default_images/jalview_banner.png"); defaultProps.put("logo.16", "/default_images/jalview_logo-16.png"); defaultProps.put("logo.32", "/default_images/jalview_logo-32.png"); defaultProps.put("logo.38", "/default_images/jalview_logo-38.png"); defaultProps.put("logo.48", "/default_images/jalview_logo-48.png"); defaultProps.put("logo.64", "/default_images/jalview_logo-64.png"); defaultProps.put("logo.128", "/default_images/jalview_logo-128.png"); defaultProps.put("logo.256", "/default_images/jalview_logo-256.png"); defaultProps.put("logo.512", "/default_images/jalview_logo-512.png"); defaultProps.put("rotatable_logo.48", "/default_images/rotatable_jalview_logo-38.png"); defaultProps.put("uod_banner", "/default_images/UoD_banner.png"); defaultProps.put("default_appbase", "https://www.jalview.org/getdown/release/1.8"); } private boolean init() { if (initDone) { return initDone; } // load channel_properties URL channelPropsURL = getClass() .getResource(CHANNEL_PROPERTIES_FILENAME); if (channelPropsURL == null) { // fallback System.err.println("Failed to find '" + CHANNEL_PROPERTIES_FILENAME + "' filem using defaults"); channelProps = defaultProps; return false; } else { try { InputStream channelPropsIS = channelPropsURL.openStream(); channelProps.load(channelPropsIS); channelPropsIS.close(); } catch (IOException e) { Cache.log.warn(e.getMessage()); return false; } } initDone = true; return initDone; } private ChannelProperties() { init(); } public static ChannelProperties getChannelProperties() { if (instance == null) { instance = new ChannelProperties(); } return instance; } private static Properties channelProps() { return getChannelProperties().channelProps; } private static Map imageMap() { return getChannelProperties().imageMap; } /* * getProperty(key) will get property value from channel_properties for key. * If no property for key is found, it will fall back to using the defaultProps defined for this class. */ public static String getProperty(String key) { return getProperty(key, null, true); } /* * getProperty(key, defaultVal) will get property value from channel_properties for key. * If no property for key is found, it will return defaultVal and NOT fall back to the class defaultProps. */ public static String getProperty(String key, String defaultVal) { return getProperty(key, defaultVal, false); } private static String getProperty(String key, String defaultVal, boolean useClassDefaultProps) { if (channelProps() != null) { if (channelProps().containsKey(key)) { return channelProps().getProperty(key, useClassDefaultProps ? defaultProps.getProperty(key) : defaultVal); } else { System.err.println("Failed to get channel property '" + key + "'"); } } return null; } public static Image getImage(String key) { Image img = null; if (imageMap().containsKey(key)) { img = imageMap().get(key); } // Catch a previously untried or failed load if (img == null) { String path = getProperty(key); if (path == null) { return null; } URL imageURL = getChannelProperties().getClass().getResource(path); img = new ImageIcon(imageURL).getImage(); if (img == null) { System.err.println( "Failed to load channel image " + key + "=" + path); } else { imageMap().put(key, img); } } return img; } }