0
votes

Basically what I am trying to do is a have a CellTable with multiple sections, separated by a custom row that spans all of the columns to delineate the sections. So far I am using a ListDataProvider for the table data, and I am extending AbstractCellTableProvider and overriding buildRowImpl in order to determine when the custom row needs to be added. It is somewhat working, but not all of the rows are showing up, and for some strange reason when I click to select one of the rows in the CellTable it randomly adds duplicate rows to the CellTable. I'm not sure if something strange is going on with the SelectionModel I am using, but if anyone is able to help with this it would be appreciated.

Here is the class I created which extends AbstractCellTableBuilder. The default row builder is just basically copied from what buildRowImpl normally does. If the ListDataProvider rowValue matches a certain criteria using an if statement then it gets sent to my buildExtraRow where the single cell row is created.

    public class CustomCellTableBuilder extends AbstractCellTableBuilder<SearchColumn>{
    public CustomCellTableBuilder() {

        super(cellTable_2);
        System.out.println("Getting into CustomCellTableBuilder super");
    }

    @Override
    protected void buildRowImpl(SearchColumn rowValue, int absRowIndex){
       //building main rows logic
        System.out.println("Getting into custom buildRowImpl, labelrow = " + labelrow);

        if (rowValue.quantity.equals("test")){
            System.out.println("Going to build extra row if");
            buildExtraRow(absRowIndex, rowValue);
        }
        else {
            System.out.println("rowValue: " + rowValue);
             final String evenRowStyle;
               final String oddRowStyle;
               final String selectedRowStyle;
               final String cellStyle;
               final String evenCellStyle;
               final String oddCellStyle;
               final String firstColumnStyle;
               final String lastColumnStyle;
               final String selectedCellStyle;

               Style style = cellTable_2.getResources().style();
                evenRowStyle = style.evenRow();
                oddRowStyle = style.oddRow();
                selectedRowStyle = " " + style.selectedRow();
                cellStyle = style.cell();
                evenCellStyle = " " + style.evenRowCell();
                oddCellStyle = " " + style.oddRowCell();
                firstColumnStyle = " " + style.firstColumn();
                lastColumnStyle = " " + style.lastColumn();
                selectedCellStyle = " " + style.selectedRowCell();

            // Calculate the row styles.
            SelectionModel<? super SearchColumn> selectionModel = cellTable_2.getSelectionModel();
            boolean isSelected =
                (selectionModel == null || rowValue == null) ? false : selectionModel.isSelected(rowValue);
            boolean isEven = absRowIndex % 2 == 0;
            StringBuilder trClasses = new StringBuilder(isEven ? evenRowStyle : oddRowStyle);
            if (isSelected) {
              trClasses.append(selectedRowStyle);
            }

            // Add custom row styles.
            RowStyles<SearchColumn> rowStyles = cellTable_2.getRowStyles();
            if (rowStyles != null) {
              String extraRowStyles = rowStyles.getStyleNames(rowValue, absRowIndex);
              if (extraRowStyles != null) {
                trClasses.append(" ").append(extraRowStyles);
              }
            }

            // Build the row.
            TableRowBuilder tr = startRow();
            tr.className(trClasses.toString());

            // Build the columns.
            int columnCount = cellTable_2.getColumnCount();
            System.out.println("column count " + columnCount);
            for (int curColumn = 0; curColumn < columnCount; curColumn++) {
              Column<SearchColumn, ?> column = cellTable_2.getColumn(curColumn);
              // Create the cell styles.
              StringBuilder tdClasses = new StringBuilder(cellStyle);
              tdClasses.append(isEven ? evenCellStyle : oddCellStyle);
              if (curColumn == 0) {
                tdClasses.append(firstColumnStyle);
              }
              if (isSelected) {
                tdClasses.append(selectedCellStyle);
              }
              // The first and last column could be the same column.
              if (curColumn == columnCount - 1) {
                tdClasses.append(lastColumnStyle);
              }

              // Add class names specific to the cell.
              Cell.Context context = new Cell.Context(absRowIndex, curColumn, cellTable_2.getValueKey(rowValue));
              String cellStyles = column.getCellStyleNames(context, rowValue);
              if (cellStyles != null) {
                tdClasses.append(" " + cellStyles);
              }

              // Build the cell.
              HorizontalAlignmentConstant hAlign = column.getHorizontalAlignment();
              VerticalAlignmentConstant vAlign = column.getVerticalAlignment();
              TableCellBuilder td = tr.startTD();
              td.className(tdClasses.toString());
              if (hAlign != null) {
                td.align(hAlign.getTextAlignString());
              }
              if (vAlign != null) {
                td.vAlign(vAlign.getVerticalAlignString());
              }
              // Add the inner div.
              DivBuilder div = td.startDiv();
              div.style().outlineStyle(OutlineStyle.NONE).endStyle();

              // Render the cell into the div.
              renderCell(div, context, column, rowValue);

              // End the cell.
              div.endDiv();
              td.endTD();
            }

            // End the row.
            tr.endTR();
        }
    }

    private void buildExtraRow(int absRowIndex, SearchColumn rowValue){
        System.out.println("In buildExtraRow");
        start(true);
        TableRowBuilder row = startRow();
        TableCellBuilder td = row.startTD().colSpan(getColumns().size());
        td.text(label).endTD();
        row.endTR();

        //cellTable_2.redrawRow(absRowIndex);
    }}

And here is basically how I would be adding a row to the table:

searchProvider.getList().add(new SearchColumn("test","","","","","","","",""));

Something else I'm confused about related to this is how ListDataProvider works with the CellTable. I can't seem to figure out when exactly the CellTable is refreshing based on what is in the ListDataProvider, and how the rows get added to the CellTable based on what is in the ListDataProvider.

1

1 Answers

0
votes

I figured out what the problem was in case anyone has issues in the future. When you click a row it redraws the row based on your selection model. So it was redrawing the row, but since I have overridden the buildRowImpl it was just recalling my custom row creation so it would add to the table instead of redrawing the clicked row.