29 import javax.swing.text.*;
30 import java.beans.*;
31 import java.lang.reflect.*;
32
33 import sun.reflect.misc.MethodUtil;
34 import sun.reflect.misc.ReflectUtil;
35
36 /**
37 * Component decorator that implements the view interface
38 * for <object> elements.
39 * <p>
40 * This view will try to load the class specified by the
41 * <code>classid</code> attribute. If possible, the Classloader
42 * used to load the associated Document is used.
43 * This would typically be the same as the ClassLoader
44 * used to load the EditorKit. If the document's
45 * ClassLoader is null, <code>Class.forName</code> is used.
46 * <p>
47 * If the class can successfully be loaded, an attempt will
48 * be made to create an instance of it by calling
49 * <code>Class.newInstance</code>. An attempt will be made
50 * to narrow the instance to type <code>java.awt.Component</code>
51 * to display the object.
52 * <p>
53 * This view can also manage a set of parameters with limitations.
54 * The parameters to the <object> element are expected to
55 * be present on the associated elements attribute set as simple
56 * strings. Each bean property will be queried as a key on
57 * the AttributeSet, with the expectation that a non-null value
58 * (of type String) will be present if there was a parameter
59 * specification for the property. Reflection is used to
60 * set the parameter. Currently, this is limited to a very
61 * simple single parameter of type String.
62 * <p>
63 * A simple example HTML invocation is:
64 * <pre>
65 * <object classid="javax.swing.JLabel">
66 * <param name="text" value="sample text">
67 * </object>
68 * </pre>
69 *
75 * Creates a new ObjectView object.
76 *
77 * @param elem the element to decorate
78 */
79 public ObjectView(Element elem) {
80 super(elem);
81 }
82
83 /**
84 * Create the component. The classid is used
85 * as a specification of the classname, which
86 * we try to load.
87 */
88 protected Component createComponent() {
89 AttributeSet attr = getElement().getAttributes();
90 String classname = (String) attr.getAttribute(HTML.Attribute.CLASSID);
91 try {
92 ReflectUtil.checkPackageAccess(classname);
93 Class<?> c = Class.forName(classname, true,Thread.currentThread().
94 getContextClassLoader());
95 Object o = c.newInstance();
96 if (o instanceof Component) {
97 Component comp = (Component) o;
98 setParameters(comp, attr);
99 return comp;
100 }
101 } catch (Throwable e) {
102 // couldn't create a component... fall through to the
103 // couldn't load representation.
104 }
105
106 return getUnloadableRepresentation();
107 }
108
109 /**
110 * Fetch a component that can be used to represent the
111 * object if it can't be created.
112 */
113 Component getUnloadableRepresentation() {
114 // PENDING(prinz) get some artwork and return something
115 // interesting here.
|
29 import javax.swing.text.*;
30 import java.beans.*;
31 import java.lang.reflect.*;
32
33 import sun.reflect.misc.MethodUtil;
34 import sun.reflect.misc.ReflectUtil;
35
36 /**
37 * Component decorator that implements the view interface
38 * for <object> elements.
39 * <p>
40 * This view will try to load the class specified by the
41 * <code>classid</code> attribute. If possible, the Classloader
42 * used to load the associated Document is used.
43 * This would typically be the same as the ClassLoader
44 * used to load the EditorKit. If the document's
45 * ClassLoader is null, <code>Class.forName</code> is used.
46 * <p>
47 * If the class can successfully be loaded, an attempt will
48 * be made to create an instance of it by calling
49 * <code>Class.getDeclaredConstructor().newInstance</code>. An attempt will be made
50 * to narrow the instance to type <code>java.awt.Component</code>
51 * to display the object.
52 * <p>
53 * This view can also manage a set of parameters with limitations.
54 * The parameters to the <object> element are expected to
55 * be present on the associated elements attribute set as simple
56 * strings. Each bean property will be queried as a key on
57 * the AttributeSet, with the expectation that a non-null value
58 * (of type String) will be present if there was a parameter
59 * specification for the property. Reflection is used to
60 * set the parameter. Currently, this is limited to a very
61 * simple single parameter of type String.
62 * <p>
63 * A simple example HTML invocation is:
64 * <pre>
65 * <object classid="javax.swing.JLabel">
66 * <param name="text" value="sample text">
67 * </object>
68 * </pre>
69 *
75 * Creates a new ObjectView object.
76 *
77 * @param elem the element to decorate
78 */
79 public ObjectView(Element elem) {
80 super(elem);
81 }
82
83 /**
84 * Create the component. The classid is used
85 * as a specification of the classname, which
86 * we try to load.
87 */
88 protected Component createComponent() {
89 AttributeSet attr = getElement().getAttributes();
90 String classname = (String) attr.getAttribute(HTML.Attribute.CLASSID);
91 try {
92 ReflectUtil.checkPackageAccess(classname);
93 Class<?> c = Class.forName(classname, true,Thread.currentThread().
94 getContextClassLoader());
95 Object o = c.getDeclaredConstructor().newInstance();
96 if (o instanceof Component) {
97 Component comp = (Component) o;
98 setParameters(comp, attr);
99 return comp;
100 }
101 } catch (Throwable e) {
102 // couldn't create a component... fall through to the
103 // couldn't load representation.
104 }
105
106 return getUnloadableRepresentation();
107 }
108
109 /**
110 * Fetch a component that can be used to represent the
111 * object if it can't be created.
112 */
113 Component getUnloadableRepresentation() {
114 // PENDING(prinz) get some artwork and return something
115 // interesting here.
|