tags:

views:

43

answers:

2

If I get a bean and a dialog, and they are coupled with bidirectional data-binding, what is the best way to roll back to the original bean when user canceled the editing.

EDIT 1

If user opened up the dialog in edit mode, he then did some modification and pressed "OK", then this dialog closed and the underlying bean got updated. When I said "canceled the editing", I mean the user opened up the dialog and did some modification but pressed "cancel" button. In this case, the underlying bean should keep untouched, but due to data-binding, it become dirty, I want the original bean back.

I can just clone a bean when the dialog opens, if user presses "OK" the cloned bean will be copied back to original bean; if user presses "cancel" the cloned bean will be abandoned. I don't know if this is a good approach.

A: 

What do you mean by "canceled the editing?" Without some example code, it's hard to help. The most generic solution is to store the previous value as a variable somewhere.

Perhaps implement a PropertyChangeListener? You can grab the old value via PropertyChangeEvent.getOldValue()? Maybe a VetoableChangeListener may work for you too.

The Alchemist
hi, I've updated the question, thanks.
solotim
+3  A: 

I have always used the clone approach quite successfully. The clone approach comes in two varieties: bind to clone and bind to original.

Bind to clone will make it so any other binding to the same field on the screen will not update while your dialog box is up. When OK is pressed you copy the clone to the original object, on cancel you simply throw the clone away.

Bind to original allows screen updates to others components bound to the same field. When OK is pressed you throw the clone away. When cancel is pressed you copy the clone to the original.

I favor the bind to clone approach since I think it is confusing to see other on screen components updating while a dialog box is up. I think it creates confusion as to whether cancel will rollback changes that are appearing outside the dialog box.

The alternative is to use a flushable binding strategy where a binding can be set up in such a way that it will not update the object until some kind of flush() method has been called. If your databinding framework does not support this then it can be a lot of work to tack this on later.

rancidfishbreath
+1 I always use a clone and update the real model only if OK was selected. I think it would be interesting that you develop your solution further, by telling where the bean is cloned (in the View, the Controller...) and what is the added value of binding in this situation (I don't see any, compared with "manual" copy GUI<->bean fields) since the clone is "local" to the dialog.
jfpoilpret
Binding dialogs creates consistent code. If you bind during a non-dialog screen and forgo binding on a dialog screen I feel you are using two paradigms to update your objects. Also some times the content part of the dialog comes from a different UI object that can be used in both dialogs or non-dialog sections of the code. Being able to have the dialog clone the object before passing it into the second UI object allows good reuse.
rancidfishbreath
I use a model delegate approach where I have ui objects and domain objects. This clone would happen in the ui object since it is a byproduct of how the presentation works. If I had an explicit controller layer I may think differently, I don't know. Really depends on how your presentation framework is set up.
rancidfishbreath