最后我运行了一个自定义DataGrid。它与gwt的展示相同。所以我得到了一些子项和一些主题。但现在我想检测ClickEvents。过了一会儿我发现了,我需要一个SelectionHandler。但在实施之后,它只选择了主题。 有没有办法捕捉子项目的选择?
我还通过selectionHandler.setSelected(subitem, true);
设置了一个选项并且效果很好,但是如何从userinput / ClickEvent获取我的子项目的选择?
基本代码只是:
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>
问候