- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathObjectView.java
174 lines (163 loc) · 6.28 KB
/
ObjectView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing.text.html;
importjava.awt.*;
importjavax.swing.*;
importjavax.swing.text.*;
importjava.beans.*;
importjava.lang.reflect.*;
importsun.reflect.misc.MethodUtil;
/**
* Component decorator that implements the view interface
* for <object> elements.
* <p>
* This view will try to load the class specified by the
* <code>classid</code> attribute. If possible, the Classloader
* used to load the associated Document is used.
* This would typically be the same as the ClassLoader
* used to load the EditorKit. If the document's
* ClassLoader is null, <code>Class.forName</code> is used.
* <p>
* If the class can successfully be loaded, an attempt will
* be made to create an instance of it by calling
* <code>Class.newInstance</code>. An attempt will be made
* to narrow the instance to type <code>java.awt.Component</code>
* to display the object.
* <p>
* This view can also manage a set of parameters with limitations.
* The parameters to the <object> element are expected to
* be present on the associated elements attribute set as simple
* strings. Each bean property will be queried as a key on
* the AttributeSet, with the expectation that a non-null value
* (of type String) will be present if there was a parameter
* specification for the property. Reflection is used to
* set the parameter. Currently, this is limited to a very
* simple single parameter of type String.
* <p>
* A simple example HTML invocation is:
* <pre>
* <object classid="javax.swing.JLabel">
* <param name="text" value="sample text">
* </object>
* </pre>
*
* @author Timothy Prinzing
*/
publicclassObjectViewextendsComponentView {
privatebooleancreateComp = true; // default
/**
* Creates a new ObjectView object.
*
* @param elem the element to decorate
*/
publicObjectView(Elementelem) {
super(elem);
}
ObjectView(Elementelem, booleancreateComp) {
super(elem);
this.createComp = createComp;
}
/**
* Create the component. The classid is used
* as a specification of the classname, which
* we try to load.
*/
@SuppressWarnings("deprecation")
protectedComponentcreateComponent() {
if (!createComp) {
returngetUnloadableRepresentation();
}
AttributeSetattr = getElement().getAttributes();
Stringclassname = (String) attr.getAttribute(HTML.Attribute.CLASSID);
try {
Class<?> c = Class.forName(classname, false,Thread.currentThread().
getContextClassLoader());
if (Component.class.isAssignableFrom(c)) {
Objecto = c.newInstance();
if (oinstanceofComponent) {
Componentcomp = (Component) o;
setParameters(comp, attr);
returncomp;
}
}
} catch (Throwablee) {
// couldn't create a component... fall through to the
// couldn't load representation.
}
returngetUnloadableRepresentation();
}
/**
* Fetch a component that can be used to represent the
* object if it can't be created.
*/
ComponentgetUnloadableRepresentation() {
// PENDING(prinz) get some artwork and return something
// interesting here.
Componentcomp = newJLabel("??");
comp.setForeground(Color.red);
returncomp;
}
/**
* Initialize this component according the KEY/VALUEs passed in
* via the <param> elements in the corresponding
* <object> element.
*/
privatevoidsetParameters(Componentcomp, AttributeSetattr) {
Class<?> k = comp.getClass();
BeanInfobi;
try {
bi = Introspector.getBeanInfo(k);
} catch (IntrospectionExceptionex) {
System.err.println("introspector failed, ex: "+ex);
return; // quit for now
}
PropertyDescriptor[] props = bi.getPropertyDescriptors();
for (inti=0; i < props.length; i++) {
// System.err.println("checking on props[i]: "+props[i].getName());
Objectv = attr.getAttribute(props[i].getName());
if (vinstanceofString) {
// found a property parameter
Stringvalue = (String) v;
Methodwriter = props[i].getWriteMethod();
if (writer == null) {
// read-only property. ignore
return; // for now
}
Class<?>[] params = writer.getParameterTypes();
if (params.length != 1) {
// zero or more than one argument, ignore
return; // for now
}
Object [] args = { value };
try {
MethodUtil.invoke(writer, comp, args);
} catch (Exceptionex) {
System.err.println("Invocation failed");
// invocation code
}
}
}
}
}