This repository was archived by the owner on Jan 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlwcomp.html
More file actions
214 lines (202 loc) · 10.5 KB
/
lwcomp.html
File metadata and controls
214 lines (202 loc) · 10.5 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--
* Copyright © 2003 Sun Microsystems, Inc
* All rights reserved.
* Notice of copyright on this source code
* product does not indicate publication.
*
* RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by
* the U.S. Government is subject to restrictions as set forth
* in subparagraph (c)(1)(ii) of the Rights in Technical Data
* and Computer Software Clause at DFARS 252.227-7013 (Oct. 1988)
* and FAR 52.227-19 (c) (June 1987).
* Sun Microsystems, Inc., 4150 Network Circle,
* Santa Clara, California 95054, U.S.A.
*
-->
<html>
<head>
<title>Creating Lightweight Java Components</title>
<link rel="stylesheet" type="text/css" href="../jhug.css" title="Style">
</head>
<body bgcolor="#ffffff">
<h2>Creating Lightweight Java Components</h2>
<p>
This topic describes how you can create <a
href="lwcomp.html#lwcomp">lightweight Java components</a> and
add them to HTML topics using the HTML
<code><OBJECT></code> tag. The last section in this
topic contains references to supplemental information about
lightweight components and the HTML <code><OBJECT></code>
tag.
<p> <img src="../../images/hg_note.gif" width="18" height="13"> References to
supplemental information are included at the end of this topic.
<h3>Lightweight Components for HTML Topics</h3>
<p> Components intended for HTML topic pages are very similar to generic lightweight
components. Components that do not require information about the View, or have
parameters that can be set, can be used without modification.
<h4>View Information</h4>
<p> Lightweight components that require information about the <code>View</code>
must implement <code>javax/javahelp/impl/ViewAwareComponent</code>. These components
implement the method <code>setViewData()</code>. The component can determine
information from the <code>View</code> about the environment in which it is
executing. For example, in the code snippet below the <code>Document</code>
object is derived from the <code>View</code>:
<pre> private View myView;
static private URL base;
public void setViewData(View v) {
myView = v;
Document d = myView.getDocument();
// System.err.println("myDocument is: "+d);
base = ((HTMLDocument) d).getBase();
// System.err.println(" base is: "+base);
}</pre>
For more information about the <code>Document</code> interface see the following
Swing API:
<pre> http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/text/Document.html</pre>
Text formatting information can be derived from the <code>View</code> by querying
its attribute set. Use the method <code>getAttributes</code> as shown below:
<pre> AttributeSet as = v.getAttributes();</pre>
Format attributes can be used by the component when the <code>AttributeSet</code>
is passed as a parameter to a <code>StyleConstants</code> method. There are methods
that can be used to determine a number of attributes, including the font family,
font size, font weight, font style, underlining, background color, and foreground
color. For example, to determine the default background color of an object, you
can do the following:
<pre> Color color=StyleContants.getBackground(as)</pre>
For a full list of formatting attributes and corresponding methods see:
<pre> http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/text/StyleConstants.html</pre>
<h4>Using Parameters</h4>
<p>
If your component takes parameters, you should follow these
two additional steps:
<ol>
<li>Add accessor methods for each parameter that can be set.
<li>Create a <code>BeanInfo</code> class that corresponds to the lightweight
component class.
</ol>
<img src="../../images/hg_note.gif" width="18" height="13"> The component
must accept parameter elements in any order.
<h4>Accessor Methods</h4>
<p> Add accessor methods that enable the component to access the parameters through
the Java reflection mechanism. In the following example, the <code>AButton</code>
class has implemented accessor methods for the parameter "data" in the methods
<code>getData</code> and <code>setData</code>:
<pre> private String data = "";
public void setData(String s) {
data = s;
}
public String getData() {
return data;
}</pre>
<table width="95%">
<tr valign="top">
<td nowrap width="2%"><img src="../../images/hg_note.gif" width="18" height="13">
</td>
<td width="98%">Even if the internal representation is not a <code>String</code>,
both the returned value for the getter method and the parameter in the setter
method must be a <code>String</code>. </td>
</tr>
</table>
<h4>BeanInfo Class</h4>
<p> Create a <code>BeanInfo</code> class that provides explicit information about
the lightweight component. The only method used by the <code>ContentViewer</code>
from the <code>BeanInfo</code> classes is <code>getPropertyDescriptors</code>.
In the complete example below, <code>JHSecondaryViewerBeanInfo</code> defines
the property data accessible through the <code>getData()</code> and <code>setData()</code>
methods in <code>JHSecondaryViewer</code>: <br>
<pre>public class JHSecondaryViewerBeanInfo extends SimpleBeanInfo {
public JHSecondaryViewerBeanInfo() {
}
public PropertyDescriptor[] getPropertyDescriptors() {
PropertyDescriptor back[] = new PropertyDescriptor[15];
try {
back[0] = new PropertyDescriptor("content", JHSecondaryViewer.class);
back[1] = new PropertyDescriptor("id", JHSecondaryViewer.class);
back[2] = new PropertyDescriptor("viewerName", JHSecondaryViewer.class);
back[3] = new PropertyDescriptor("viewerActivator", JHSecondaryViewer.class);
back[4] = new PropertyDescriptor("viewerStyle", JHSecondaryViewer.class);
back[5] = new PropertyDescriptor("viewerLocation", JHSecondaryViewer.class);
back[6] = new PropertyDescriptor("viewerSize", JHSecondaryViewer.class);
back[7] = new PropertyDescriptor("iconByName", JHSecondaryViewer.class);
back[8] = new PropertyDescriptor("iconByID", JHSecondaryViewer.class);
back[9] = new PropertyDescriptor("text", JHSecondaryViewer.class);
back[10] = new PropertyDescriptor("textFontFamily", JHSecondaryViewer.class);
back[11] = new PropertyDescriptor("textFontSize", JHSecondaryViewer.class);
back[12] = new PropertyDescriptor("textFontWeight", JHSecondaryViewer.class);
back[13] = new PropertyDescriptor("textFontStyle", JHSecondaryViewer.class);
back[14] = new PropertyDescriptor("textColor", JHSecondaryViewer.class);
return back;
} catch (Exception ex) {
return null;
}
}
}</pre>
<h4>Parameter Names</h4>
<p> When naming parameters, be sure to avoid names reserved in the HTML 4.0 specification
for use as <code><OBJECT></code> tag attributes. For a complete list of
<code><OBJECT> </code>attributes see the HTML 4.0 specification:
<pre> http://w3c.org/TR/REC-html40/</pre>
<h3>Using the <code><OBJECT></code> Tag</h3>
<p> You add lightweight components to JavaHelp topics by means of the <code><OBJECT></code>
tag and its <code>classid</code> attribute. The help viewer only recognizes
<code>classid</code> values prefixed with the "<code>java:</code>" tag. All
other <code>classid</code> tags are ignored. The following example creates an
<code>ALabel</code> within the HTML topic:
<pre> <OBJECT CLASSID="java:sunw.demo.object.ALabel"</OBJECT></pre>
You can use standard <code><OBJECT></code> tag attributes (see the HTML
4.0 specification for more details), but to be recognized the lightweight component
must have getter and setter methods for those attributes. A getter or setter
method must operate on a <code>String</code>. For example, in the following example
width and height for the <code>ALabel</code> are set if there are <code>getWidth</code>/<code>setWidth</code>
and <code>getHeight</code>/<code>setHeight</code> methods in <code>ALabel</code>:
<pre> <OBJECT
CLASSID="java:sunw.demo.object.ALabel"
width="400" height="500">
</OBJECT></pre>
<p> Parameters are passed to lightweight components by using the <code><param></code>
tag. A parameter is only recognized if the component has getter and setter
methods for that parameter. A getter or setter method must operate on a
<code>String</code>. In the example below, the help viewer passes a number of
parameters and their values to a the <code>JHSecondaryViewer</code> component:
<br>
<pre> <OBJECT classid="java:com.sun.java.help.impl.JHSecondaryViewer">
<param name="content" value="../topicB/glossary_def.html">
<param name="viewerActivator" value="javax.help.LinkLabel">
<param name="viewerStyle" value="javax.help.Popup">
<param name="viewerSize" value="300,400">
<param name="text" value="Click here">
<param name="textFontFamily" value="SansSerif">
<param name="textFontSize" value="x-large">
<param name="textFontWeight" value="plain">
<param name="textFontStyle" value="italic">
<param name="textColor" value="red">
</OBJECT></pre>
<h3>Supplemental Information</h3>
<p> The following information supplements the information in this topic.
<p>
<b>Lightweight Java Components</b>
<a name="lwcomp"></a>
<p>
For general information about lightweight Java components see:
<pre><a href="http://java.sun.com/j2se/1.4.1/docs/guide/awt/demos/lightweight/index.html">http://java.sun.com/j2se/1.4.1/docs/guide/awt/demos/lightweight/index.html</a></pre>
<b>JavaHelp Components</b>
<p>
As a reference, the sources to the lightweight components
that implement JavaHelp system popups and secondary windows
(<code>JHSecondaryViewer.java</code> and
<code>JHSecondaryViewerBeanInfo.java</code>) can be found in
<code>src.jar</code> at:
<pre>com\sun\java\javahelp\impl</pre>
For a description of how the <code><OBJECT></code> tag is used to implement
popups and secondary windows, see <a href="popup.html">Using Popup and Secondary
Windows</a>.
<p>
<b>HTML 4.0 Specification</b>
<p>
You can find a detailed description of the
<code><OBJECT></code> tag as part of the HTML 4.0
specification:
<pre><a href="http://w3c.org/TR/REC-html40/">http://w3c.org/TR/REC-html40/</a></pre>
</body>
</html>