Enhancing cell border rendering

A prerequisite to manipulation of table and cell attributes as described separately is to provide some enhancement to the way cell borders are rendered by Java.

Up to J2SE 1.4 cell borders are not rendered individually and there is no way to have different colors for borders of different sides of a cell. Either a border is drawn around all sides of a table cell or no border is drawn. There is no way for example to draw a vertical border between two cells only while the other sides of these cells have no borders.

Rendering mechanism

In general Elements of an HTMLDocument are rendered through the pluggable design construct of HTMLEditorKit.HTMLFactory. The idea behind this design is to provide individual Views to render Elements .

Parts involved in cell border rendering

A table cell is rendered by class BoxPainter which is an inner class of class StyleSheet. BoxPainter is used in class BlockView which is a superclass of class CellView. CellView in turn is an inner class of class TableView (terrible isn't it?).

To change how borders are painted, StyleSheet.BoxPainter needs to be replaced by an own class. TableView could be subclassed and its create method could be reimplemented to provide a replacement of CellView replacing StyleSheet.BoxPainter.

Enabling for individual border rendering

The constructor of TableView is public but unfortunately the class itself is protected so there is no way to simply subclass TableView to replace the ViewFactory of TableView with an own CellView. It is dificult to change the rendering while leaving the underlying classes untouched due to TableView being protected (I did not want to write a complete new view or view factory only to change a little part - a complete new table view would be hard to write too...).

Solution

SHTMLBoxPainter is created allowing to draw borders around a table cell independently from each other. Width and color for each side are drawn independently and borders of adjacent cells are adjusted so that only one border is drawn instead of two when the adjacent cells have no margin..

To enable SHTMLBoxPainter in place of StyleSheet.BoxPainter the sources of the superclasses BlockView and TableView are copied unchanged into new ones and only bring in SHTMLBoxPainter where appropriate. This is done by classes SHTMLBlockView and SHTMLTableView respectively. Both classes had to be put into package javax.swing.text.html to do so.

Due to class TableView being protected admittedly this is an ugly solution so any other and more elegant and effortless one is welcome and highly appreciated!

Highly appreciated also would be an explanation why TableView is protected...