1

I have CellTable with MultipleSelectionModel attached to it. After some modification of data the table has to be refreshed and new data has to be reloaded from server.

However I need to update checkboxes state for newly loaded data. So I am able to query selection boxes with selectionModel.getSelectedSet() - but now I need to find these objects in table and "check" them.

Because content of objects changes and since they are used as keys in Maps internally in GWT components- I was forced to write "wrapper" over these objects which uses only ID in equals/hashCode.

So basically I save selectedSet before firing event, then iterate over it and invoke setSelected method:

Set<T> selectedSet = selectionModel.getSelectedSet();
RangeChangeEvent.fire(table,...)
if (selectedSet != null)
    for (T obj : selectedSet) {
        selectionModel.setSelected(obj,true);
    }
}

Is there any better approach?

jdevelop
  • 12,176
  • 10
  • 56
  • 112

1 Answers1

3

This is what the ProvidesKey is for: create a ProvidesKey instance that returns the ID of your objects to be used as their keys, and pass that instance to your selection model when you build it:

MultiSelectionModel<X> selectionModel = new MultiSelectionModel<X>(new ProvidesKey<X>() {
   @Override
   public Object getKey(X item) {
      return item.getId();
   }
});

That way, you shouldn't have anything special to do with your selection model after retrieving updated data: push it to your table and it'll ask the selection model for each object whether it's selected or not, and the selection model will be able to answer based solely on the object's ID, therefore reusing the same selected set as before.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • so does that mean I don't have to override equals/hashCode in my classes but simply pass the "selected" objects after content of table is updated? – jdevelop Dec 01 '11 at 11:03
  • Absolutely. By default, selection models use a [`SimpleKeyProvider`](http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/view/client/SimpleKeyProvider.html) which uses the object itself as its key; which means `equals` and `hashCode` will be used on the object itself. With a `ProvidesKey` that returns the object's ID, `equals` and `hashCode` will used on the ID, not your object. You shouldn't have anything special to do re. the selection model after retrieving the updated data, simply `setRowData` on the cell table and you're done: the selection model will store IDs only – Thomas Broyer Dec 01 '11 at 12:11