01: package org.swingml.component;
02:
03: import java.awt.*;
04: import java.net.*;
05: import java.util.*;
06:
07: import javax.swing.*;
08:
09: import org.swingml.*;
10:
11: public class IconFactory {
12:
13: private static WeakHashMap iconCache;
14:
15: public static ImageIcon getIcon(SwingMLModel aModel) {
16: return getIconForUrl(aModel.getIcon());
17: }
18:
19: protected static ImageIcon getIconForUrl(String url) {
20: ImageIcon result = null;
21: if (url != null) {
22: String fullURL = url;
23: if (iconCache == null) {
24: iconCache = new WeakHashMap();
25: }
26:
27: if (iconCache.containsKey(url)) {
28: result = (ImageIcon) iconCache.get(url);
29: } else {
30: try {
31: if (url.indexOf("http") < 1) {
32: // missing protocol/address... find and append
33: fullURL = SwingMLRenderer.getRenderer()
34: .getDocumentBase()
35: + url;
36: }
37: result = new ImageIcon(new URL(fullURL));
38: iconCache.put(url, result);
39: } catch (MalformedURLException e) {
40:
41: }
42: }
43: }
44: return result;
45: }
46:
47: public static Image getIconImage() {
48: return getIconImage(SwingMLRenderer.getWindowIconURL());
49: }
50:
51: public static Image getIconImage(String iconFileName) {
52: Image result = null;
53: if (iconFileName != null && iconFileName.trim().length() > 0) {
54: try {
55: URL aUrl = new URL(iconFileName);
56: result = Toolkit.getDefaultToolkit().getImage(aUrl);
57: } catch (MalformedURLException mue) {
58: result = Toolkit.getDefaultToolkit().getImage(
59: iconFileName);
60: }
61: }
62:
63: return result;
64: }
65: }
|