KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > ObjectView


1 /*
2  * @(#)ObjectView.java 1.13 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text.html;
8
9 import java.util.Enumeration JavaDoc;
10 import java.awt.*;
11 import javax.swing.*;
12 import javax.swing.text.*;
13 import java.beans.*;
14 import java.lang.reflect.*;
15
16 /**
17  * Component decorator that implements the view interface
18  * for <object> elements.
19  * <p>
20  * This view will try to load the class specified by the
21  * <code>classid</code> attribute. If possible, the Classloader
22  * used to load the associated Document is used.
23  * This would typically be the same as the ClassLoader
24  * used to load the EditorKit. If the document's
25  * ClassLoader is null, <code>Class.forName</code> is used.
26  * <p>
27  * If the class can successfully be loaded, an attempt will
28  * be made to create an instance of it by calling
29  * <code>Class.newInstance</code>. An attempt will be made
30  * to narrow the instance to type <code>java.awt.Component</code>
31  * to display the object.
32  * <p>
33  * This view can also manage a set of parameters with limitations.
34  * The parameters to the &lt;object&gt; element are expected to
35  * be present on the associated elements attribute set as simple
36  * strings. Each bean property will be queried as a key on
37  * the AttributeSet, with the expectation that a non-null value
38  * (of type String) will be present if there was a parameter
39  * specification for the property. Reflection is used to
40  * set the parameter. Currently, this is limited to a very
41  * simple single parameter of type String.
42  * <p>
43  * A simple example HTML invocation is:
44  * <pre>
45  * &lt;object classid="javax.swing.JLabel"&gt;
46  * &lt;param name="text" value="sample text"&gt;
47  * &lt;/object&gt;
48  * </pre>
49  *
50  * @author Timothy Prinzing
51  * @version 1.13 12/19/03
52  */

53 public class ObjectView extends ComponentView {
54
55     /**
56      * Creates a new ObjectView object.
57      *
58      * @param elem the element to decorate
59      */

60     public ObjectView(Element elem) {
61     super(elem);
62     }
63
64     /**
65      * Create the component. The classid is used
66      * as a specification of the classname, which
67      * we try to load.
68      */

69     protected Component createComponent() {
70     AttributeSet attr = getElement().getAttributes();
71     String JavaDoc classname = (String JavaDoc) attr.getAttribute(HTML.Attribute.CLASSID);
72     try {
73             Class JavaDoc c = Class.forName(classname, true,Thread.currentThread().
74                                     getContextClassLoader());
75         Object JavaDoc o = c.newInstance();
76         if (o instanceof Component) {
77         Component comp = (Component) o;
78         setParameters(comp, attr);
79         return comp;
80         }
81     } catch (Throwable JavaDoc e) {
82         // couldn't create a component... fall through to the
83
// couldn't load representation.
84
}
85     
86     return getUnloadableRepresentation();
87     }
88
89     /**
90      * Fetch a component that can be used to represent the
91      * object if it can't be created.
92      */

93     Component getUnloadableRepresentation() {
94     // PENDING(prinz) get some artwork and return something
95
// interesting here.
96
Component comp = new JLabel("??");
97     comp.setForeground(Color.red);
98     return comp;
99     }
100
101     /**
102      * Get a Class object to use for loading the
103      * classid. If possible, the Classloader
104      * used to load the associated Document is used.
105      * This would typically be the same as the ClassLoader
106      * used to load the EditorKit. If the documents
107      * ClassLoader is null,
108      * <code>Class.forName</code> is used.
109      */

110     private Class JavaDoc getClass(String JavaDoc classname) throws ClassNotFoundException JavaDoc {
111     Class JavaDoc klass;
112
113     Class JavaDoc docClass = getDocument().getClass();
114     ClassLoader JavaDoc loader = docClass.getClassLoader();
115     if (loader != null) {
116         klass = loader.loadClass(classname);
117     } else {
118         klass = Class.forName(classname);
119     }
120     return klass;
121     }
122
123     /**
124      * Initialize this component according the KEY/VALUEs passed in
125      * via the &lt;param&gt; elements in the corresponding
126      * &lt;object&gt; element.
127      */

128     private void setParameters(Component comp, AttributeSet attr) {
129     Class JavaDoc k = comp.getClass();
130     BeanInfo bi;
131     try {
132         bi = Introspector.getBeanInfo(k);
133     } catch (IntrospectionException ex) {
134         System.err.println("introspector failed, ex: "+ex);
135         return; // quit for now
136
}
137     PropertyDescriptor props[] = bi.getPropertyDescriptors();
138     for (int i=0; i < props.length; i++) {
139         // System.err.println("checking on props[i]: "+props[i].getName());
140
Object JavaDoc v = attr.getAttribute(props[i].getName());
141         if (v instanceof String JavaDoc) {
142         // found a property parameter
143
String JavaDoc value = (String JavaDoc) v;
144         Method writer = props[i].getWriteMethod();
145         if (writer == null) {
146             // read-only property. ignore
147
return; // for now
148
}
149         Class JavaDoc[] params = writer.getParameterTypes();
150         if (params.length != 1) {
151             // zero or more than one argument, ignore
152
return; // for now
153
}
154         String JavaDoc [] args = { value };
155         try {
156             writer.invoke(comp, args);
157         } catch (Exception JavaDoc ex) {
158             System.err.println("Invocation failed");
159             // invocation code
160
}
161         }
162     }
163     }
164
165 }
166
167
Popular Tags