| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
package ca.spaz.util; |
| 13 |
|
|
| 14 |
|
import java.awt.*; |
| 15 |
|
import java.net.URL; |
| 16 |
|
import java.util.*; |
| 17 |
|
|
| 18 |
|
import ca.spaz.cron.CRONConfiguration; |
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
public class ImageFactory { |
| 25 |
|
|
| 26 |
0 |
private static ImageFactory instance = null; |
| 27 |
|
private int cacheSize; |
| 28 |
|
private Map imageCache; |
| 29 |
|
|
| 30 |
0 |
private ImageFactory() { |
| 31 |
0 |
String cacheValue = CRONConfiguration.getInstance().getProperty("cron.imagecache.size", "10"); |
| 32 |
0 |
int cacheSz = 0; |
| 33 |
|
try { |
| 34 |
0 |
cacheSz = Integer.parseInt(cacheValue); |
| 35 |
0 |
} catch (NumberFormatException e) { |
| 36 |
0 |
cacheSz = 10; |
| 37 |
0 |
e.printStackTrace(); |
| 38 |
0 |
} |
| 39 |
0 |
this.cacheSize = cacheSz; |
| 40 |
0 |
this.imageCache = new CacheMap(cacheSize); |
| 41 |
0 |
} |
| 42 |
|
|
| 43 |
|
public static final ImageFactory getInstance() { |
| 44 |
0 |
if (null == instance) { |
| 45 |
0 |
instance = new ImageFactory(); |
| 46 |
|
} |
| 47 |
0 |
return instance; |
| 48 |
|
} |
| 49 |
|
|
| 50 |
|
public Image loadImage(URL url) { |
| 51 |
0 |
Image ret = null; |
| 52 |
0 |
if (imageCache.containsKey(url)) { |
| 53 |
0 |
ret = (Image) imageCache.get(url); |
| 54 |
0 |
} else { |
| 55 |
0 |
ret = Toolkit.getDefaultToolkit().createImage(url); |
| 56 |
0 |
if (ret != null) { |
| 57 |
0 |
imageCache.put(url, ret); |
| 58 |
|
} |
| 59 |
|
} |
| 60 |
0 |
return ret; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
public Image loadImage(String resourceID) { |
| 64 |
0 |
return loadImage(resourceID, this); |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
public Image loadImage(String resourceID, Object source) { |
| 68 |
0 |
Class base = null; |
| 69 |
0 |
if (null == source) { |
| 70 |
0 |
base = this.getClass(); |
| 71 |
0 |
} else { |
| 72 |
0 |
base = source.getClass(); |
| 73 |
|
} |
| 74 |
0 |
URL url = base.getResource(resourceID); |
| 75 |
0 |
Image ret = null; |
| 76 |
0 |
if (url == null) { |
| 77 |
0 |
url = this.getClass().getResource(resourceID); |
| 78 |
0 |
if (url == null) { |
| 79 |
0 |
throw new IllegalArgumentException(resourceID + " is not a valid resource identifier"); |
| 80 |
|
} |
| 81 |
|
} |
| 82 |
0 |
ret = loadImage(url); |
| 83 |
0 |
return ret; |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
} |