1 7 package org.ejtools.adwt.util; 8 9 import java.awt.Image ; 10 import java.awt.Toolkit ; 11 import java.awt.image.ImageProducer ; 12 import java.io.IOException ; 13 import java.net.URL ; 14 import java.security.AccessController ; 15 import java.security.PrivilegedAction ; 16 import java.util.Hashtable ; 17 import java.util.Map ; 18 19 import javax.swing.Icon ; 20 21 25 public class DefaultObjectRenderer implements ObjectRenderer 26 { 27 28 protected Map objects = new Hashtable (); 29 30 31 37 public Icon getIcon(Object o) 38 { 39 return this.getObjectWrapper(o).getIcon(); 40 } 41 42 43 49 public String getLabel(Object o) 50 { 51 return this.getObjectWrapper(o).toString(); 52 } 53 54 55 61 protected ObjectWrapper getObjectWrapper(Object o) 62 { 63 ObjectWrapper wrapper = (ObjectWrapper) this.objects.get(o); 64 if (wrapper == null) 65 { 66 wrapper = new ObjectWrapper(o); 67 this.objects.put(o, wrapper); 68 } 69 return wrapper; 70 } 71 72 73 79 protected static Image loadImage(final String resourceName) 80 { 81 try 82 { 83 final Class c = DefaultObjectRenderer.class; 84 ImageProducer ip = (ImageProducer ) AccessController.doPrivileged( 85 new PrivilegedAction () 86 { 87 public Object run() 88 { 89 URL url; 90 if ((url = c.getResource(resourceName)) == null) 91 { 92 return null; 93 } 94 else 95 { 96 try 97 { 98 return url.getContent(); 99 } 100 catch (IOException ioe) 101 { 102 return null; 103 } 104 } 105 } 106 }); 107 if (ip == null) 108 { 109 return null; 110 } 111 Toolkit tk = Toolkit.getDefaultToolkit(); 112 return tk.createImage(ip); 113 } 114 catch (Exception ex) 115 { 116 return null; 117 } 118 } 119 } 120 | Popular Tags |