protected function insertRow(
        SwatTableViewRow $row,
        SwatTableViewRow $reference_row = null,
        $after = true
    ) {
        $this->validateRow($row);

        if ($reference_row !== null) {
            $key = array_search($reference_row, $this->extra_rows, true);

            if ($key === false) {
                throw new SwatWidgetNotFoundException(
                    'The reference row ' .
                        'could not be found in this table-view.'
                );
            }

            if ($after) {
                // insert after reference row
                array_splice($this->extra_rows, $key, 1, array(
                    $reference_row,
                    $row
                ));
            } else {
                // insert before reference row
                array_splice($this->extra_rows, $key, 1, array(
                    $row,
                    $reference_row
                ));
            }
        } else {
            if ($after) {
                // append to array
                $this->extra_rows[] = $row;
            } else {
                // prepend to array
                array_unshift($this->extra_rows, $row);
            }
        }

        if ($row->id !== null) {
            $this->rows_by_id[$row->id] = $row;
        }

        $row->view = $this; // deprecated reference
        $row->parent = $this;
    }