*/
public class SelectedItemConverter implements TypeConverter, java.io.Serializable {
private static final long serialVersionUID = 200808191439L;
public Object coerceToUi(Object val, Component comp) { //load
Listbox lbx = (Listbox) comp;
if (val != null) {
final ListModel xmodel = lbx.getModel();
if (xmodel instanceof BindingListModel) {
final BindingListModel model = (BindingListModel) xmodel;
int index = model.indexOf(val);
if (index >= 0) {
final Listitem item = (Listitem) lbx.getItemAtIndex(index);
//Bug #2728704: Listbox with databinding generates onSelect w/o user action
//Shall not fire event by spec. For backward compatibility(still want to
//fire onSelect event as usual), user can specifies in zk.xml
//<library-property>
// <name>org.zkoss.zkplus.databind.onSelectWhenLoad</name>
// <value>true</value>
//</library-property>
//then data binder will still fire the onSelect event as usual.
if (SelectedItemConverter.isOnSelectWhenLoad()) {
final int selIndex = lbx.getSelectedIndex();
//We need this to support load-when:onSelect when first load
//the page in (so it is called only once).
if (item != null && selIndex != index) { // bug 1647817, avoid endless-loop
Set items = new HashSet();
items.add(item);
//bug #2140491
Executions.getCurrent().setAttribute("zkoss.zkplus.databind.ON_SELECT"+lbx.getUuid(), Boolean.TRUE);
Events.postEvent(new SelectEvent("onSelect", lbx, items, item));
}
}
return item;
}
} else if (xmodel == null) { //no model case, assume Listitem.value to be used with selectedItem
//iterate to find the selected item assume the value (select mold)
for (final Iterator it = lbx.getItems().iterator(); it.hasNext();) {
final Listitem li = (Listitem) it.next();
if (val.equals(li.getValue())) {
return li;
}
}