/
ViewerUtil.java
48 lines (40 loc) · 1.16 KB
/
ViewerUtil.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
package nl.siegmann.epublib.viewer;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ViewerUtil {
private static Logger log = LoggerFactory.getLogger(ViewerUtil.class);
/**
* Creates a button with the given icon. The icon will be loaded from the classpath.
* If loading the icon is unsuccessful it will use the defaultLabel.
*
* @param iconName
* @param backupLabel
* @return
*/
// package
static JButton createButton(String iconName, String backupLabel) {
JButton result = null;
ImageIcon icon = createImageIcon(iconName);
if (icon == null) {
result = new JButton(backupLabel);
} else {
result = new JButton(icon);
}
return result;
}
static ImageIcon createImageIcon(String iconName) {
ImageIcon result = null;
String fullIconPath = "/viewer/icons/" + iconName + ".png";
try {
Image image = ImageIO.read(ViewerUtil.class.getResourceAsStream(fullIconPath));
result = new ImageIcon(image);
} catch(Exception e) {
log.error("Icon \'" + fullIconPath + "\' not found");
}
return result;
}
}