Thursday, January 31, 2013

Re: Cancel CellList selection

I also realized that the overriden setSelected method is called two times in PromptingSelectionModel. So if I selected Cell 7 and Cell 7 is currently selected and if I click on Cell 10, the the cell selection is about to changed the first call to overriden setSelected method passes Cell 7 object and selected flag as false and second call passed Cell 10 object and selected flag now is true. So having that unsavedFlag check shows me two Confirm dialog boxes assuming the form data on Cell 7 has changed. Hitting cancel twice keeps the Cell 7 selected but it also shows a mild yellow selection color on Cell 10. 

So it seems when a selection is changed GWT removes the first selection (selected off) and selects the next selection (selected true). So when I override the setSelected method, I somehow need to stop the second call if my form is not saved. Not sure how do I do that.


On Thursday, November 10, 2011 5:12:57 AM UTC-6, Thomas Broyer wrote:
It's not about cancelling the event; it's about *not* routing it to a DefaultSelectionEventManager (which changes selection depending on the event). It also means you shouldn't addCellPreviewHandler, but instead use the two-argument overload of setSelectionModel.

It's as easy as:
myCellList.setSelectionModel(mySelectionModel, CellPreviewEvent.Handler<MyObject>() {
   private final CellPreviewEvent.Handler<MyObject> defaultSelectionManager = DefaultSelectionEventManager.createDefaultManager();
   @Override
   public void onCellPreview(CellPreviewEvent<MyObject> event) {
      if (hasUnsavedChanged() && !Window.prompt("There are unsaved changes, are you sure you want to continue?") {
         return;
      }
      return defaultSelectionManager.onCellPreview(event);
   }
});

Beware though, if you have a cell that "handlesSelection()", you'll have to handle the case there (so that it doesn't change selection if there are unsaved changes).

Oh, and it just occurred to me that you could also simply code that within your SelectionModel too:
class PromptingSelectionModel<T> extends SingleSelectionModel<T> {
   @Override
   public void setSelected(T object, boolean selected) {
      if (hasUnsavedChanged() && !Window.prompt("There are unsaved changes, are you sure you want to continue?") {
         return;
      }
      super.setSelected(object, selected);
   }
}

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment