Parsing formatting markup
The most powerful use of the FormText
control is
when formatting tags are added to the text. The expected root tag is form
.
It can have one or more children that can either be <p>
or <li>
.
Either of these can have normal text, text between <b>
or
<span>
tags, images, links and SWT
controls. Images are
declared using <img href="image key"/>
(no content), while
links are expressed using <a href="href">text</a>
.
Some of the tags mentioned above have additional attributes. Tag
<a>
can accept nowrap="true"
to block the link from
being wrapped into the new line. Tag <p>
can have attribute
vspace="false"
(true
by default) that adds additional space
between paragraphs. Tag <li>
has more attributes:
-
style - can be text
, bullet
and image
(default is bullet
)
-
value - not used for bullet
; if style is
text
, the value will be rendered instead in place of a bullet;
if style is image
, value represents a key in the image table of
an image to be rendered in place of a bullet
-
vspace - the same as for the 'p
' tag.
-
indent - the number of pixels to indent text
-
bindent - the number of pixels to indent the bullet
(this number is independent from 'indent' - be careful not to overlap them)
Tags that affect appearance of the normal text are <b>
(works as
expected), and <span>
. The later allows you to change font and/or
color of the text within the tag. Soft line breaks can be added using
<br/>
tag. The text is parsed as XML, this means that there
must be an end tag for each element so you cannot use open <br>
as in HTML). The XML entity & is recognized and replaced with &, and it is required that ampersand (&)
characters which are not part of an entity declaration be represented as &.
Numeric entities may also be used in formatted text.
Since release 3.1, FormText
can be used to mix SWT widgets
inside text, hyperlinks and images. SWT controls are created as children of
FormText
, which makes FormText
a layout manager of a
sort, with instruction on where to place the control relative to text embedded
directly in the XML.
One common theme that can be observed is that FormText
is not
responsible for loading images, fonts, resolving links or colors. This is not a
browser and it is much better to separate concerns and simply assign images and
colors managed elsewhere. Both links and images simply have 'href
'
attribute to reference them. For links, the value of this attribute will be
provided in the hyperlink event when listeners are notified. Images need to be
registered with the text control using the matching 'href' key. This way, the
control does not need to worry about loading the images - it has them in the
hash table and can render them immediately.
Similar approach has been used for colors and fonts. Colors are already
handled by the toolkit, so you can allocate as many as you want using a unique
key and RGB values by calling toolkit.getColors().createColor()
.
What is left is to set all the colors referenced in the 'span
' tag
so that the control will be able to use them during rendering.