One way to allow it would be to do something like this:
trait ValueChangeListener extends AbstractField {
def addValueChangeListener(valueChangeListener: (Property.ValueChangeEvent) => Unit) = {
addListener(new Property.ValueChangeListener {
def valueChange(e: Property.ValueChangeEvent) = valueChangeListener(e)
})
}
}
You can mixin that to any Field, and after that it’s easy to add an anonymous function to handle the ValueChangeEvents, eg:
val dateField = new DateField with ValueChangeListener
dateField.addValueChangeListener(_ => {/* handle event */})
But traits do not support arguments, and adding a single ValueChangeListener to a Field is very common, so it might be even better to do add a subclass doing this:
class EnhancedDateField(caption: String = null, dataSource: Property = null, value: Date = null,
valueChangeListener: (Property.ValueChangeEvent) => Unit = null)
extends DateField(caption) with ValueChangeListener {
if(dataSource != null) setPropertyDataSource(dataSource)
if(value != null) setValue(value)
if(valueChangeListener != null) addValueChangeListener(valueChangeListener)
}
Now usage is even simpler, and I can use named constructor parameters to set up the DateField:
val dateField = new EnhancedDateField(caption = "Caption", value = new Date,
valueChangeListener = _ => {/* handle event */})
The downside is that we’d need to subclass (almost?) all Vaadin components. However, I think that would be prudent anyway, since we need to do it to add other enhancements to them. For example, I would like to see helper stuff like:
def getDateValue = getValue.asInstanceOf[Date]
in an enhanced DateField.