1 package siouxsie.hivemind;
2
3 import java.awt.Image;
4 import java.io.File;
5 import java.io.IOException;
6 import java.net.URL;
7
8 import javax.imageio.ImageIO;
9 import javax.swing.ImageIcon;
10
11 import org.apache.commons.lang.StringUtils;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.apache.hivemind.Location;
15 import org.apache.hivemind.internal.Module;
16 import org.apache.hivemind.service.ObjectProvider;
17
18 /**
19 * Provide icons.
20 * @author Arnaud Cogoluegnes
21 * @version $Id$
22 */
23 public class IconObjectProvider implements ObjectProvider {
24
25 private static final char TYPE_ICON_SEPARATOR = ':';
26
27 private static final String TYPE_CLASSPATH = "classpath";
28
29 private static final String TYPE_URL = "url";
30
31 private static final String TYPE_FILE = "file";
32
33 private static final Log LOG = LogFactory.getLog(IconObjectProvider.class);
34
35 public Object provideObject(Module contributingModule,
36 java.lang.Class propertyType,
37 java.lang.String locator,
38 Location location) {
39
40 String type,path;
41
42 String [] pattern = StringUtils.split(locator, TYPE_ICON_SEPARATOR);
43
44 if(pattern == null || pattern.length == 0) {
45 LOG.error("invalid pattern for icon object provider: "+location);
46 return null;
47 }else if(pattern.length == 1) {
48 type = TYPE_CLASSPATH;
49 } else {
50 type = pattern[0];
51 }
52
53 path = pattern[1];
54
55 Image image = createImage(type, path);
56
57
58 return new ImageIcon(image);
59 }
60
61 private Image createImage(String type, String path) {
62 Image res = null;
63 if(TYPE_CLASSPATH.equals(type)) {
64 try {
65 res = ImageIO.read(getClass().getClassLoader().getResourceAsStream(path));
66 } catch (IOException e) {
67 LOG.error("could not load icon from classpath",e);
68 }
69 } else if(TYPE_URL.equals(type)) {
70 try {
71 res = ImageIO.read(new URL(path));
72 } catch (IOException e) {
73 LOG.error("could not load icon from url",e);
74 }
75 } else if(TYPE_FILE.equals(path)) {
76 try {
77 res = ImageIO.read(new File(path));
78 } catch (IOException e) {
79 LOG.error("could not load icon from file",e);
80 }
81 } else {
82 LOG.error("unknown icon source type:"+type);
83 }
84
85 return res;
86 }
87
88 }