-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageBundle.java
109 lines (88 loc) · 2.88 KB
/
ImageBundle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.util.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
/**
* ImageBundle is a ResourceBundle that retrieves the content of an image from an image file with supported extension.
*/
public class ImageBundle extends ResourceBundle {
private static final String RESOURCE = "MyApp";
private String __fileSuffix;
/**
* @uml.property name="__KEYS"
*/
private static final Vector<String> __KEYS;
private static Hashtable<String, ImageIcon> __TABLE;
static {
__KEYS = new Vector<String>();
__KEYS.addElement(RESOURCE);
__TABLE = new Hashtable<String, ImageIcon>();
}
/**
* Load image file stored in a JAR classpath
* @param imageName the filename of the image
* @return the ImageIcon of the image file
*/
private ImageIcon __loadImageFromJar(String imageName) {
String path = ""; //define the file path of the image file in imageName itself
String imagePath = path + imageName + __fileSuffix;
ImageIcon icon;
URL url;
icon = (ImageIcon)__TABLE.get(imageName);
if(icon != null)
return icon;
url = ImageBundle.class.getResource(imagePath);
icon = new ImageIcon(url);
__TABLE.put(imageName, icon);
return icon;
}
/**
* Load image file stored in a given path
* @param imageName the filename of the image
* @return the ImageIcon of the image file
*/
@SuppressWarnings("unused")
private ImageIcon __loadImageFromExternalSource(String imageName) {
String path = System.getenv("MY_APP_HOME_PATH");
String imagePath = ((path != null) ? (path + imageName + __fileSuffix) : (imageName + __fileSuffix));
ImageIcon value;
value = (ImageIcon)__TABLE.get(imageName);
if(value != null){
//Outils.debugMessage("Cached " + imagePath + "> " + imageName + ": " + value);
return value;
}
else {
//Outils.debugMessage("New " + imagePath + "> " + imageName + ": " + value);
}
ImageIcon property = null;
BufferedImage image = null;
try {
image = ImageIO.read(new File(imagePath));
} catch (IOException e) {
e.printStackTrace();
}
property = new ImageIcon(image);
value = property;
__TABLE.put(imageName, value);
return value;
}
protected ImageBundle(String suffix) {
__fileSuffix = suffix;
}
public ImageBundle() {
this("");
}
/**
* @return
* @uml.property name="__KEYS"
*/
public Enumeration<String> getKeys() {
return __KEYS.elements();
}
protected final Object handleGetObject(String key) {
return __loadImageFromJar(key);
}
}