Changing table and cell attributes

In the previous chapters basic methods for creating and manipulating a table structure are explained in detail. In this chapter it is discussed how to select and apply attribute changes to an existing table structure.

Structural changes vs. attribute changes

Structural changes to a table (insert row, delete column, etc.) all can be done in a single step. To add these functions to the GUI of an application, a single menu item or tool bar button is sufficient. A GUI for table attribute changes is achieved not as easy. There are many attributes each talbe element can have and it would be very tedious to change single attributes through single menu items each.

Most of the time, attribute changes are to be applied as a group of changes to a group of elements as one (for instance changing all cells of one column to a certain width and background color). With class TableDialog a new dialog for changing table attributes is created therefore.

Introducing TableDialog and DialogShell

It is called through new action FormatTableAction of class FrmMain. With class TableDialog the second formatting dialog is introduced after class FontDialog which is why a new base class DialogShell is created too. DialogShell has all methods shared by dialogs of application SimplyHTML thus avoiding code redundancies.

Class TableDialog

TableDialog wraps all attributes of tables and table cells changeable in SimplyHTML into one dialog. It partly uses components already used in class FontPanel and partly introduces additional components.

Common setting and getting of attributes

Class AttributeSet in package javax.swing.text provides a good way of grouping an arbitrary number of attributes and passing them between elements and components. For this reason application SimplyHTML uses interface AttributeComponent (renamend from FontComponent of stage 3) to define a common way of setting and getting attributes to and from GUI components via AttributeSets.

All components of TableDialog are implementing interface AttributeComponent. They are held in two Vectors, one for table attributes and one for table cell attributes. Whenever a TableDialog is created to reflect a current set of attributes existing for a table and table cell, simply respective attribute sets are passed to methods setTableAttributes and setCellAttributes.

Both methods then iterate through the mentioned component Vectors calling method setValue on each of their components. Each component then picks its attribute(s) from the attribute set and displays them accordingly. Similarly, attributes are returnd by TableDialog with methods getTableAttributes and getCellAttributes. Again these methods iterate through the component Vectors to call method getValue on each component returning attribute sets with the sum of all changed attributes.

Returning only changed attributes

All components of TagbleDialog 'remember' the original attribute value and only return an attribute when it was changed compared to that original value. This mechanism ensures only attributes being applied, that have been set through the dialog although other attributes were shown in the dialog as well. Without this mechanism always all attributes would be returned by the dialog regardless of wheteher they changed, returning only changed attributes avoids redundant storage of attributes.

Applying attributes returned by TableDialog

To apply table attributes method applyTableAttributes of class SHTMLEditorPane is called. It gets the table element from the current caret position and passes it to method addAttributes of class SHTMLDocument along with the attributes to apply.

Basically the same is done for applying cell attributes with the difference that a range of cells is passed in addition. Depending on the users choice to apply attributes to the current cell only, the current column, the current row or all cells of the table, method applyCellAttributes of class SHTMLEditorPane iterates through the appropriate range of table cells and calls method addAttributes of class SHTMLDocument accordingly.