1

finally I got a custom DataGrid running. It is the same like that from the showcase of gwt. So I got some subitems and some mainitems. But now I want to detect ClickEvents. After a while I found out, that I need a SelectionHandler. But after implementing it, it only select the mainitems. Is there a way to catch the selection of the subitems?

I also set one as selected via selectionHandler.setSelected(subitem, true); and it worked great, but how do I get the selection on my subitems from the userinput/ClickEvent?

The basic code is just:
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() { @Override public void onSelectionChange(final SelectionChangeEvent event) { File selected = ((SingleSelectionModel) selectionModel).getSelectedObject(); if(selected != null){ Window.alert(selected.getFileName()); } } } });

/**
 * Renders the data rows that display each contact in the table.
 */
private class CustomTableBuilder extends AbstractCellTableBuilder<File> {

    private final String childCell = " " + FileTreeViewImpl.this.resources.styles().childCell();
    private final String rowStyle;
    private final String selectedRowStyle;
    private final String cellStyle;
    private final String selectedCellStyle;

    public CustomTableBuilder() {
        super(FileTreeViewImpl.this.fileDataGrid);

        DefaultSelectionEventManager.createCustomManager(new DefaultSelectionEventManager
                .WhitelistEventTranslator<File>(0));

        // Cache styles for faster access.
        com.google.gwt.user.cellview.client.AbstractCellTable.Style style = FileTreeViewImpl.this.fileDataGrid.getResources().style();
        this.rowStyle = style.evenRow();
        this.selectedRowStyle = " " + style.selectedRow();
        this.cellStyle = style.cell() + " " + style.evenRowCell();
        this.selectedCellStyle = " " + style.selectedRowCell();
    }

    @Override
    protected void buildRowImpl(final File rowValue, final int absRowIndex) {
        buildContactRow(rowValue, absRowIndex, false);

        // Display list of files.
        if (FileTreeViewImpl.this.displayFolderContent.contains(rowValue.getFileName())) {
            Set<File> files = FileTreeViewImpl.this.folderMap.get(rowValue.getFileName());
            for (File file : files) {
                buildContactRow(file, absRowIndex, true);
            }
        }
    }


    /**
     * Build a row.
     * 
     * @param rowValue
     *            the file info
     * @param absRowIndex
     *            the absolute row index
     * @param isFriend
     *            true if this is a subrow, false if a top level row
     */
    private void buildContactRow(final File rowValue, final int absRowIndex, final boolean isFriend) {
        // Calculate the row styles.
        SelectionModel<? super File> selectionModel = FileTreeViewImpl.this.fileDataGrid.getSelectionModel();
        boolean isSelected = (selectionModel == null || rowValue == null) ? false : selectionModel.isSelected(rowValue);
        StringBuilder trClasses = new StringBuilder(this.rowStyle);
        if (isSelected) {
            trClasses.append(this.selectedRowStyle);
        }

        // Calculate the cell styles.
        String cellStyles = this.cellStyle;
        if (isSelected) {
            cellStyles += this.selectedCellStyle;
        }
        if (isFriend) {
            cellStyles += this.childCell;
        }

        TableRowBuilder row = startRow();
        row.className(trClasses.toString());

        // Column one to expand the submenu.
        TableCellBuilder td = row.startTD();
        td.style().outlineStyle(OutlineStyle.NONE).endStyle();
        if (!isFriend) {
            renderCell(td, createContext(0), FileTreeViewImpl.this.viewFolderContent, rowValue);
        }
        td.endTD();

        // Filename goes here.
        td = row.startTD();
        td.className(cellStyles);
        td.style().outlineStyle(OutlineStyle.NONE).endStyle();
        if (isFriend) {
            td.text(rowValue.getFileName());
        } else {
            renderCell(td, createContext(1), FileTreeViewImpl.this.nameColumn, rowValue);
        }
        td.endTD();

        // Filesize goes here.
        td = row.startTD();
        td.className(cellStyles);
        td.style().outlineStyle(OutlineStyle.NONE).endStyle();
        if (isFriend) {
            td.text(rowValue.getFileSizeAsString());
        } else {
            renderCell(td, createContext(2), FileTreeViewImpl.this.sizeColumn, rowValue);
        }
        td.endTD();

        // Last Editor goes here.
        td = row.startTD();
        td.className(cellStyles);
        td.style().outlineStyle(OutlineStyle.NONE).endStyle();
        if (isFriend) {
            td.text(rowValue.getLastEditedBy());
        } else {
            renderCell(td, createContext(3), FileTreeViewImpl.this.editedColumn, rowValue);
        }
        td.endTD();

        row.endTR();
    }
}</code>

Greetings

oruckdeschel
  • 88
  • 1
  • 8
  • I am still not able to understand the problem properly in terms of your requirement. You want to get the selected records of the custom grid OR you want to get the values entered by user run time ?? Also, is your custom grid is editable ? – PVR Jul 03 '14 at 10:20
  • The userinput I mentioned is the ClickEvent on one of my subitems. So I want to get the linked Object of the Grid. But every time I clicked on one of the subitems only the rootitem gets selected and I get this object. – oruckdeschel Jul 03 '14 at 13:16
  • Can you post your code ?? – PVR Jul 04 '14 at 05:59
  • i added a small example with the selectionhandler and the rowbuilder – oruckdeschel Jul 04 '14 at 06:23

0 Answers0