View Javadoc
1 package org.argosfields.resource; 2 3 import java.text.MessageFormat; 4 import java.util.ArrayList; 5 import java.util.Collection; 6 import java.util.Enumeration; 7 import java.util.MissingResourceException; 8 import java.util.ResourceBundle; 9 10 import org.eclipse.jface.resource.ImageDescriptor; 11 import org.eclipse.jface.resource.ImageRegistry; 12 import org.eclipse.swt.SWT; 13 import org.eclipse.swt.graphics.Image; 14 15 /*** 16 * ResourceManager.java 17 * @author Xavier Cho 18 * @version $Revision: 1.3 $ $Date: 2003/10/21 14:59:48 $ 19 */ 20 public final class ResourceManager { 21 public static final String RESOURCE_BUNDLE = 22 "org.argosfields.resource.ApplicationResources"; 23 public static final String ACTION_PREFIX = "org.argosfields.action"; 24 25 private static ResourceManager instance; 26 private ImageRegistry imageRegistry; 27 private ResourceBundle bundle; 28 29 private ResourceManager() { 30 this.bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE); 31 } 32 33 public static ResourceManager getInstance() { 34 if (instance == null) { 35 synchronized (ResourceManager.class) { 36 instance = new ResourceManager(); 37 } 38 } 39 return instance; 40 } 41 42 public String getString(final String key) { 43 if (key == null) { 44 SWT.error(SWT.ERROR_NULL_ARGUMENT); 45 } 46 47 String value; 48 49 try { 50 value = bundle.getString(key); 51 } catch (MissingResourceException e) { 52 value = null; 53 } 54 55 return value; 56 } 57 58 public String getString(final String key, final Object[] args) { 59 if (key == null) { 60 SWT.error(SWT.ERROR_NULL_ARGUMENT); 61 } 62 63 String value; 64 65 try { 66 value = bundle.getString(key); 67 value = MessageFormat.format(value, args); 68 } catch (MissingResourceException e) { 69 value = null; 70 } 71 72 return value; 73 } 74 75 public Image getImage(final String key) { 76 if (imageRegistry == null) { 77 synchronized (this) { 78 this.imageRegistry = new ImageRegistry(); 79 } 80 } 81 82 Image image = imageRegistry.get(key); 83 84 if (image == null) { 85 String path = getString(key); 86 87 if (path != null) { 88 ImageDescriptor descriptor = 89 ImageDescriptor.createFromFile(ResourceManager.class, path); 90 imageRegistry.put(key, descriptor); 91 image = imageRegistry.get(key); 92 } 93 } 94 95 return image; 96 } 97 98 public ImageDescriptor getImageDescriptor(final String key) { 99 if (imageRegistry == null) { 100 synchronized (this) { 101 this.imageRegistry = new ImageRegistry(); 102 } 103 } 104 105 ImageDescriptor descriptor = imageRegistry.getDescriptor(key); 106 107 if (descriptor == null) { 108 String path = getString(key); 109 110 if (path != null) { 111 descriptor = 112 ImageDescriptor.createFromFile(ResourceManager.class, path); 113 imageRegistry.put(key, descriptor); 114 } 115 } 116 117 return descriptor; 118 } 119 120 public Collection getActionNames() { 121 Collection actions = new ArrayList(20); 122 123 Enumeration enum = bundle.getKeys(); 124 while (enum.hasMoreElements()) { 125 String name = (String) enum.nextElement(); 126 127 if (name.startsWith(ACTION_PREFIX)) { 128 int index = name.lastIndexOf("Action") + 6; 129 name = name.substring(0, index); 130 131 if (!actions.contains(name)) { 132 actions.add(name); 133 } 134 } 135 } 136 137 return actions; 138 } 139 }

This page was automatically generated by Maven