I've got some questions about FormModel.isDirty(). I apologize in advance if I'm stupid or if I missed something obvious in the javadocs, the user manual or the code.
On this page, http://forum.springframework.org/showthread.php?t=10914,
I was given this advice about my View that has a tree on the left and a "edit the selected tree node" form on the right:
The tree is initially loaded with countries, provinces and cities that are stored in a database. When the user selects one of these nodes (for example, a country node), the details are shown on a form on the right, where the user can edit the values, if desired. Before I show the form to the user, I call myForm.setFormObject(mySelectedObject).Originally Posted by pdbruycker
Now I noticed that just before the setFormObject call that myForm.getFormModel().isDirty() returns false and after the call it returns true! To me, that just doesn't seem right because the user hasn't made any changes to the values yet.
When I look at the implementation of DefaultFormModel.isDirty(), I understand what is happening. Here is that implementation:
My domain objects in the tree do NOT currently implement FormObject. Therefore, the actual isDirty check gets delegated to BufferedValueModel.isDirty(). Ultimately, this isDirty check boils down to the result of this comparison: bufferedValue != NO_VALUE. When BufferedValueModel is instantiated, bufferedValue is initialized to NO_VALUE. However, as soon as you call myForm.setFormObject(), this causes BufferedValueModel.setValue() to be called, which ends up setting bufferedValue to a value that is not NO_VALUE. Thus, the state of the dirty flag is not affected by user editing. To me, this just doesn't seem right; isDirty() should return false until the user edits the values.Code:public boolean isDirty() { if (getFormObject() instanceof FormObject) { return ((FormObject)getFormObject()).isDirty(); } else if (getBufferChangesDefault()) { Iterator it = displayValueModels.values().iterator(); while (it.hasNext()) { ValueModel model = unwrap((ValueModel)it.next()); if (model instanceof BufferedValueModel) { BufferedValueModel bufferable = (BufferedValueModel)model; if (bufferable.isDirty()) { return true; } } } } return false; }
Of course, I could make my domain objects implement FormObject so that I could have complete control over the isDirty logic but I'm somewhat reluctant to create a dependency between my domain objects and a GUI framework.
Questions:
1) Is there a workaround for the current isDirty behaviour?
2) Can isDirty be changed so that it only returns true when the user has actually edited a value?
Cheers,
Joe


Reply With Quote
Anyway, the new version of the file is not showing up yet in anonymous CVS. I'll check again in a few hours and I'll let you know the results.