KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > util > DefaultObjectRenderer


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt.util;
8
9 import java.awt.Image JavaDoc;
10 import java.awt.Toolkit JavaDoc;
11 import java.awt.image.ImageProducer JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.security.AccessController JavaDoc;
15 import java.security.PrivilegedAction JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import javax.swing.Icon JavaDoc;
20
21 /**
22  * @author Laurent Etiemble
23  * @version $Revision: 1.3 $
24  */

25 public class DefaultObjectRenderer implements ObjectRenderer
26 {
27    /** Description of the Field */
28    protected Map JavaDoc objects = new Hashtable JavaDoc();
29
30
31    /**
32     * Gets the icon attribute of the DefaultObjectRenderer object
33     *
34     * @param o Description of the Parameter
35     * @return The icon value
36     */

37    public Icon JavaDoc getIcon(Object JavaDoc o)
38    {
39       return this.getObjectWrapper(o).getIcon();
40    }
41
42
43    /**
44     * Gets the label attribute of the DefaultObjectRenderer object
45     *
46     * @param o Description of the Parameter
47     * @return The label value
48     */

49    public String JavaDoc getLabel(Object JavaDoc o)
50    {
51       return this.getObjectWrapper(o).toString();
52    }
53
54
55    /**
56     * Gets the objectWrapper attribute of the DefaultObjectRenderer object
57     *
58     * @param o Description of the Parameter
59     * @return The objectWrapper value
60     */

61    protected ObjectWrapper getObjectWrapper(Object JavaDoc 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    /**
74     * Description of the Method
75     *
76     * @param resourceName Description of the Parameter
77     * @return Description of the Return Value
78     */

79    protected static Image JavaDoc loadImage(final String JavaDoc resourceName)
80    {
81       try
82       {
83          final Class JavaDoc c = DefaultObjectRenderer.class;
84          ImageProducer JavaDoc ip = (ImageProducer JavaDoc) AccessController.doPrivileged(
85             new PrivilegedAction JavaDoc()
86             {
87                public Object JavaDoc run()
88                {
89                   URL JavaDoc 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 JavaDoc ioe)
101                      {
102                         return null;
103                      }
104                   }
105                }
106             });
107          if (ip == null)
108          {
109             return null;
110          }
111          Toolkit JavaDoc tk = Toolkit.getDefaultToolkit();
112          return tk.createImage(ip);
113       }
114       catch (Exception JavaDoc ex)
115       {
116          return null;
117       }
118    }
119 }
120
Popular Tags