How to apply links

In Java links inside HTML documents are represented different to other HTML tags. As previously described a link usually is denoted by a tag such as

<a href="URI">link text</a>

But instead of being a branch element of type HTML.Tag.A inside a HTMLDocument, a link is represented as an AttributeSet for a given content element. To complicate things a little, images are represented as an AttributeSet for a content element too, so image links in turn are represented as two AttributeSets each nested inside the AttributeSet of a given content element.

Applying links

To apply a text or image link, class SHTMLEditorPane has two new methods both called setLink. The two methods only differ in the parameters they expect. One of the methods just wraps the other one into a more convenient call with fewer parameters for text links. Method setLink determines, whether or not the selection currently is inside a link with the help of method findLinkElementUp of class Util (see below).

Text links and image links

If inside a link, this link is replaced by the new link. If the selection is not inside a link, the new link just is created at the current caret position. Possibly selected text is replaced by the text of the new link in this case.

After it has determined, whether the selection is inside a link, method setLink splits into calls to methods setTextLink and setImageLink respectivley, whatever applies from the parameters received from the calling method. If no image file is passed ( linkImage is null ), a text link is assumed and vice versa ( linkText is null instead).

Method setTextLink

Method setTextLink takes the link reference and stores it as HTML.Attribute.HREF in a new AttributeSet. If a style name was passed, it is stored as HTML.Attribute.CLASS in the AttributeSet for the link. The new link then is applied to the selection depending on what is inside the selection.

If the selection contains a link, this link is replaced by the new link. Otherwise, the selected text is replaced by the new link text and the link attributes are applied to this new text.

Method setImageLink

Method setImageLink works similar to method setTextLink. The only difference is that it creates an additional AttributeSet for representing the image (file, width and height). This AttributeSet is applied instead of link text to the selection replacing any existing link or other text along with the new link attributes.

Method findLinkElementUp

To find a link element from the position of a given element upwards in the element strucutre of a document, the attribute sets of elements have to be inspected (not the element names). Method findLinkElementUp does this by iterating through parent elements of a given element and looking for an attribute with key HTML.Tag.A. If such an attribute is found, this attribute represents a nested AttributeSet . Method findLinkElementUp then looks for an attribute with key HTML.Attribute.HREF inside this nested AttributeSet. If HTML.Attribute.HREF is found, a content element with a link attached has been found and this element is returned.