Hi,
I have a checkbox. The checkbox's Model is a PropertyModel, on a property
that can be either a Boolean or a String, with values {S, N}. And I want to
set the user and date in which the checkbox's model has changed, in order to
provide traceability. The checkbox is using an implementation of IConverter
when it's about Strings and the standard BooleanConverter otherwise.
Now, the problem is, the value with which the checkbox is initialized *can*
be null, if it has never been set. So, I can not rely on implementing
'onModelChanged()' because it will always fire the first time (in fact,
having null value would be the same as having negative value as far as this
requirement is concerned)
I've tried different approaches, and I finally came up with the one that
seems more logical to me, as it is close to a translation of what I've
stated before. I'm overriding onModelChanged(), and implementing a custom
IModelComparator, so that onModelChanged does not fire when changing value
from null to 'nonselected'.
Here follows the implementation. My question is, can't my custom comparison
be implemented in a simpler way ? I can't get component.getInput(), because
it's only a Component. And I'd not like to implement a util method to return
a boolean from the ModelObject, doing instanceof's...
Maybe I'm asking too much :-)
public boolean compare(Component component, Object compareValue)
{
final Object currentValue = component.getModelObject();
if (currentValue == null && compareValue == null) {
return true;
}
if (currentValue == null || compareValue == null) {
return false;
}
// MY CUSTOM COMPARISON: Null value equals not checking the
checkbox
if (currentValue == null &&
!Strings.isTrue(component.getConverter(compareValue.getClass()).convertToString(compareValue,
component.getLocale()))){
return false;
}
else{
return currentValue.equals(compareValue);
}
}
Thanks ! feel free to comment :-)