Creating a GUI to define link anchors

Link anchors denote certain positions inside a document making it possible to link to that particular position directly. Link anchors are identified by a link anchor name which is internally stored in the document at the position it denotes. Link anchors typically are not visible in the document, they are present only in the HTML code making up such document.

to enable a user to create and manipulate link anchors requires some extra work because SimplyHTML hides HTML code with the intention to let an author concentrate on content rather than HTML coding.

Class AnchorDialog

With class AnchorDialog a GUI is built for making link anchors of a document visible and for creating or deleting link anchors. As several dialogs of application SimplyHTML have been explained in earlier stages already, we only look at some additional 'specialties' of class AnchorDialog here.

Document as object or file

AnchorDialog can be constructed either with a Document object or with a URL leading to a document. When constructed with a URL, AnchorDialog loads the Document found at that location. When link anchors are added to or removed from a document loaded from a URL, the changes are saved to the document before AnchorDialog is destroyed.

Method getAnchors

Class AnchorDialog displays a list of link anchors existing in the document associated to the dialog with the help of method getAnchors. Method getAnchors iterates through all elements of the document and looks for HTML.Tag.A in the attribute set of each element. If such attribute is found, getAnchors looks for HTML.Attribute.NAME. Any element having HTML.Attribute.NAME inside HTML.Tag.A is listed as a link anchor.

Whenever method getAnchors is called to find all anchors in a document, a Hashtable anchorTable is filled with the anchors found. Hashtable anchorTable references elements with anchor names. The Hashtable is used to fill the list of available anchors too.

Making anchors visible

As mentioned previously, anchors are only visible in the HTML code of a document. To make a link anchor visible a Highlighter is used in class AnchorDialog. A ListSelectionListener is implemented by class AnchorDialog and registered with the list of anchors. Whenever an item is selected in the list, the element the anchor name refers to is taken from the anchorTable Hashtable. The element is used to determine the anchor position in the document and the position is passed to the Highlighter to be shown.