View Javadoc

1   /*
2    * $HeadURL: https://mindtreeinsight.svn.sourceforge.net/svnroot/mindtreeinsight/insight/insight-ui/trunk/src/main/java/com/mindtree/techworks/insight/ResourceManager.java $
3    * $Date: 2008-01-15 19:53:53 -0700 (Tue, 15 Jan 2008) $
4    * $Revision: 135 $
5    * $Author: bindul $
6    * 
7    * Copyright (c) 2007 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight;
25  
26  import java.awt.Image;
27  import java.awt.Toolkit;
28  import java.awt.image.ImageProducer;
29  import java.io.File;
30  import java.io.IOException;
31  import java.net.URL;
32  import java.security.AccessController;
33  import java.security.PrivilegedAction;
34  import java.util.HashMap;
35  import java.util.Locale;
36  import java.util.Map;
37  import java.util.ResourceBundle;
38  
39  import javax.swing.ImageIcon;
40  
41  /**
42   * A simple resource manager implementation for Insight. This implementation
43   * starts with loading images, but can be extended for i18n implementation
44   * later.
45   * 
46   * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a>
47   * @version $Revision: 135 $ $Date: 2008-01-15 19:53:53 -0700 (Tue, 15 Jan 2008) $
48   */
49  public final class ResourceManager {
50  
51  	/**
52  	 * The singleton instance of the class
53  	 */
54  	private static ResourceManager instance = new ResourceManager();
55  	
56  	/**
57  	 * The resource bundle to load images from
58  	 */
59  	private static final String imageResourceBundle = "com.mindtree.techworks.insight.images";
60  	
61  	/**
62  	 * The resource bundle with the location of images.
63  	 */
64  	private ResourceBundle imageLocResourceBundle;
65  	
66  	/**
67  	 * The default locale of the run
68  	 */
69  	private Locale defaultLocale;	// Initialized in the constructor
70  	
71  	/**
72  	 * The cache used to store loaded images
73  	 */
74  	private Map imageIconCache;
75  	
76  	/**
77  	 * Mutex used to synchronize write access to the image icon cache
78  	 */
79  	private Object imageCacheMutex = new Object();
80  	
81  	/**
82  	 * The private constructor to maintain the singleton.
83  	 */
84  	private ResourceManager () {
85  		this.defaultLocale = Locale.getDefault();
86  		imageIconCache = new HashMap();
87  		
88  		// Instantiate the resource bundles
89  		imageLocResourceBundle = ResourceBundle.getBundle(imageResourceBundle);
90  	}
91  	
92  	/**
93  	 * Gets the singleton instance of the resource manager.
94  	 * @return The Resource Manager instance.
95  	 */
96  	public static ResourceManager getInstance() {
97  		return instance;
98  	}
99  	
100 	/**
101 	 * Loads an {@link Image} using the image key and the classloader provided.
102 	 * @param resourcePath The classpath relative path to the image
103 	 * @param classloader The classloader to use to load the image
104 	 * @return The loaded image or <code>null</code> if one cannot be loaded
105 	 */
106 	private Image loadImage (final String resourcePath, final ClassLoader classLoader) {
107 		
108 		Image loadedImage = null;
109 		try {
110 			
111 			// First try the old way to get the image
112 			String insightHomeResourcePath = System
113 					.getProperty(InsightConstants.INSIGHT_HOME)
114 					+ File.separator + resourcePath;
115 			File insightHomeResourceFile = new File(insightHomeResourcePath);
116 			if (insightHomeResourceFile.exists()) {
117 				loadedImage = new ImageIcon(insightHomeResourceFile.getAbsolutePath()).getImage();
118 			}
119 			
120 			// If not found use the 'new' way
121 			if (null == loadedImage) {
122 				ImageProducer imageProducer = (ImageProducer) AccessController.doPrivileged(new PrivilegedAction() {
123 	
124 					public Object run() {
125 						Object returnContent = null;
126 						URL imageUrl = classLoader.getResource(resourcePath);
127 						if (null != imageUrl) {
128 							try {
129 								returnContent = imageUrl.getContent();
130 							} catch (IOException e) {
131 								// Ignore
132 								// TODO Log later
133 							}
134 						}
135 						return returnContent;
136 					}});
137 	
138 	            if (null != imageProducer) {
139 	            	loadedImage = Toolkit.getDefaultToolkit().createImage(imageProducer);
140 	            }
141 			}
142         }
143         catch (Exception ex) {
144             // Ignore
145         	// TODO Log later
146         }
147 
148         return loadedImage;
149 	}
150 	
151 	/**
152 	 * Resolves the image key and returns the locale specific image.
153 	 * @param imageKey The image key to resolve
154 	 * @return The resolved image key
155 	 */
156 	private String getResolvedImagekey (String imageKey) {
157 		String resolvedKey = imageLocResourceBundle.getString(imageKey);
158 		if (null == resolvedKey || resolvedKey.trim().length() == 0) {
159 			// Fallback to english with no language
160 			ResourceBundle defaultResourceBundle = ResourceBundle.getBundle(imageResourceBundle, Locale.ENGLISH);
161 			resolvedKey = defaultResourceBundle.getString(imageKey);
162 			if (null == resolvedKey || resolvedKey.trim().length() == 0) {
163 				resolvedKey = imageKey;
164 			}
165 		}
166 		return resolvedKey;
167 	}
168 	
169 	/**
170 	 * Loads an image from the classpath with the relative key passed in. The
171 	 * default classloader of this class is used.
172 	 * @param imageKey The key to the image
173 	 * @return The loaded image
174 	 */
175 	public ImageIcon loadImageIcon (String imageKey) {
176 		return loadImageIcon(imageKey, getClass().getClassLoader());
177 	}
178 	
179 	/**
180 	 * Loads an image icon using the image key passed in and the class loader.
181 	 * The loaded images are cached, and further requests for the same key
182 	 * are resolved from the cache.
183 	 * @param imageKey The key to the image
184 	 * @param classLoader The classloader to use
185 	 * @return The loaded image
186 	 */
187 	public ImageIcon loadImageIcon (String imageKey, ClassLoader classLoader) {
188 		String resolvedImageKey = getResolvedImagekey(imageKey);
189 		
190 		// Check if we have it in the cache
191 		ImageIcon imageIcon = (ImageIcon) imageIconCache.get(resolvedImageKey);
192 		
193 		if (null == imageIcon) {
194 			// Not found in cache... load it
195 			synchronized (imageCacheMutex) {
196 				// Check if someone else loaded it
197 				imageIcon = (ImageIcon) imageIconCache.get(resolvedImageKey);
198 				if (null == imageIcon) {
199 					Image imageForIcon = loadImage(resolvedImageKey, classLoader);
200 					if (null != imageForIcon) {
201 						imageIcon = new ImageIcon(imageForIcon);
202 						imageIconCache.put(resolvedImageKey, imageIcon);
203 					} else {
204 						imageIcon = new ImageIcon();
205 					}
206 				}
207 			}
208 			
209 		}
210 		
211 		return imageIcon;
212 	}
213 
214 	/**
215 	 * @return the defaultLocale
216 	 */
217 	public Locale getDefaultLocale() {
218 		return defaultLocale;
219 	}
220 }