Manipulating the table structure

Manipulation of an existing table structure is necessary for following actions

- append row
- append column
- insert row
- insert column
- delete row
- delete column

Each of the above table manipulations is implemented in class SHTMLEditorPane with respective methods ( insertTableColumn, appendTableCol, etc.). All table manipulation methods of class SHTMLEditorPane have following similarities.

Common logic

Insertions and additions of rows and columns are all done by using methods insertAfterEnd and insertBeforeStart of class HTMLDocument respectively. Deletions of rows and columns are both done by using methods remove and removeElements of class HTMLDocument respectively.

All table manipulation methods assume that they are called while the caret is somewhere inside a table cell. If not, they do nothing. As opposed to attribute changes the table manipulation methods are designed for being called with a single action command ('delete row', 'insert column', etc.).

Adding rows

To add a row, the current (insert) or last (append) row is copied by iterating the row and cell elements and creating an HTML string making up that element structure including attributes but without text content. The resulting HTML code is inserted before the current row element (insert) or inserted after the last row element (append) by use of method insertAfterEnd and insertBeforeStart of class HTMLDocument .

How it works

To accomplish the above functionality method createNewRow is shared by methods insertTableRow and appendTableRow of class SHTMLEditorPane. Method createNewRow uses getTableRowHTML of class SHTMLEditorPane to do the actual assembling of HTML code. Method getTableRowHTML in turn uses methods startTag and endTag of class SHTMLWriter to generate HTML.

Entry point for Actions

Methods insertTableRow and appendTableRow are as well the entry points for respective actions of class FrmMain to connect this functionality with GUI elements such as menus and tool bar buttons. They both find out the table row (current or last) by determining the current table cell with the help of method getCurTableCell. Method getCurTableCell is discussed in more detail in the chapter about how to implement a customized caret movement and key mapping.

Removing rows

Removing a table row is comparably simple. Because a table row is represented by a single element with child elements belonging to that row only, it is sufficient to just delete this particular element from the document structure.

To remove a row method deleteTableRow is called. It is as well the method used in FrmMain's respective action. Method deleteTableRow gets the row the caret currently is in and deletes it by calling method removeElement.

Adding columns

As opposed to working with rows, table columns are harder to manipulate because the cells of a column are spread over all row elements. To add a column, the same logic is used as in adding rows except that method createTableColumn iterates through all rows of a table working on the particular cell belonging to the column in question in each row.

Retaining table width

Another exception is that SimplyHTML adjusts cell widths by taking half of the width of the current column for the new column. In method creatTableColumn the half width is applied to the column the new column is to be inserted before. Then the new column is created with the same width so that in total the table width did not change.

Removing columns

To remove a column again the same logic is used as with rows but respective method deleteTableCol is the most complicated of table manipulation methods.

Retaining table width

deleteTableCol first determines which column to increase in width after removal of the current column. By default the column on the left of the current column is taken. If the current column is the first in the table the column right of the current column is taken instead.

The method then gets the width values of both columns and finds out the sum of both widths. The sum is only taken if the unit of both width values is the same (both percent or point). If a sum could be taken, it is added to an attribute set.

Removing cells

deleteTableCol then iterates through all rows in the table removing the cell of each row belonging to the column to remove and then adds the new width to its adjacent cell left or right respectively. To remove a cell method removeElements of class SHTMLDocument is used. For some reason I did not find out up to now why but method remove of class HTMLDocument does not work when used on the last column in a table.