package com.onaro.util.jfc; import java.text.MessageFormat; import java.util.MissingResourceException; import java.util.ResourceBundle; import javax.swing.Action; import javax.swing.ImageIcon; import javax.swing.KeyStroke; import com.onaro.commons.ui.CommonsUIPlugin; import com.onaro.commons.util.IResourceCommonProperties; import com.onaro.commons.util.ResourceCommonProperties; import com.onaro.sanscreen.client.SANscreenClientPlugin; public class ResourceCommonPropertiesFactory { /** * Constructs the properties for the given base name. * * @param resources * the bundle to get the resources from * @param resName * the base name */ public static IResourceCommonProperties create(ResourceBundle resources, String resName) { ResourceCommonProperties rcp = new ResourceCommonProperties(); try { String name = resources.getString(resName + ".name"); //$NON-NLS-1$ //we allow 0 length strings if (name != null) { rcp.putValue(Action.NAME, name); } } catch (MissingResourceException e) { } try { String iconName = resources.getString(resName + ".icon"); //$NON-NLS-1$ if (iconName != null && iconName.length() > 0) { ImageIcon icon = ResourceCommonPropertiesFactory.getIcon(iconName); rcp.putValue(Action.SMALL_ICON, icon); } } catch (MissingResourceException e) { } try { String shortDescription = resources.getString(resName + ".tooltip"); //$NON-NLS-1$ rcp.putValue(Action.SHORT_DESCRIPTION, shortDescription); } catch (MissingResourceException e) { } try { String mnemonicStr = resources.getString(resName + ".mnemonic"); //$NON-NLS-1$ if (mnemonicStr != null && mnemonicStr.length() > 0) { Integer mnemonicCode = Integer.valueOf(mnemonicStr.charAt(0)); rcp.putValue(Action.MNEMONIC_KEY, mnemonicCode); } } catch (MissingResourceException e) { } try { String acceleratorStr = resources.getString(resName + ".accelerator"); //$NON-NLS-1$ if (acceleratorStr != null && acceleratorStr.length() > 0) { KeyStroke acceleratorKey = KeyStroke.getKeyStroke(acceleratorStr); rcp.putValue(Action.ACCELERATOR_KEY, acceleratorKey); } } catch (MissingResourceException e) { } return rcp; } public static ImageIcon getIcon(String name, boolean required) throws IllegalArgumentException { if (CommonsUIPlugin.isOSGIRunning()) return getIcon(name, required, SANscreenClientPlugin.getDefault().getAllBundlesClassLoader()); else return getIcon(name, required, null); } /** * * @param name Relative resource name to ResourceCommonProperties. * @param required is this icon required, null if not found (no exception thrown). * @throws IllegalArgumentException if the icon cannot be found. */ public static ImageIcon getIcon(String name, boolean required, ClassLoader classLoader) throws IllegalArgumentException { if (name == null || name.length() == 0) return null; java.net.URL iconUrl = null; if (classLoader!=null) iconUrl = classLoader.getResource(name); else iconUrl = ResourceCommonPropertiesFactory.class.getResource(name); ImageIcon icon = null; if (iconUrl == null) { if (required) { String msgPattern = "Icon with name: {0} not found"; //$NON-NLS-1$ String msg = MessageFormat.format(msgPattern, name); throw new IllegalArgumentException(msg); } } else { icon = new ImageIcon(iconUrl); } return icon; } /** * Gets and icon with the given name as a resource (reading it from the class path). * * @param name * the icon's file name, may be null * @return the requested icon or null if name is null */ public static ImageIcon getIcon(String name) { return getIcon(name, true); } public static ImageIcon getIcon(String name, ClassLoader classLoader) { return getIcon(name, true, classLoader); } }