package com.rjconsultores.ventaboletos.web.utilerias; import java.util.ArrayList; import java.util.List; import org.zkoss.util.resource.Labels; import org.zkoss.zk.ui.WrongValueException; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.InputEvent; import org.zkoss.zkplus.databind.BindingListModel; import org.zkoss.zkplus.databind.BindingListModelList; import org.zkoss.zkplus.spring.SpringUtil; import org.zkoss.zul.Combobox; import org.zkoss.zul.Comboitem; import com.rjconsultores.ventaboletos.entidad.Ciudad; import com.rjconsultores.ventaboletos.service.CiudadService; /** * * @author rodrigo */ public class MyComboboxCiudad extends Combobox { private static final long serialVersionUID = -1332458046058719821L; private CiudadService ciudadService; private List lsCiudad; private Ciudad initialValue; private Integer indiceSelected = null; public MyComboboxCiudad() { super(); ciudadService = (CiudadService) SpringUtil.getBean("ciudadService"); lsCiudad = new ArrayList(); this.setAutodrop(false); this.setAutocomplete(false); this.addEventListener("onOK", new EventListener() { @Override public void onEvent(Event event) throws Exception { String strCiudad = MyComboboxCiudad.this.getText().toUpperCase(); if (strCiudad.length() < MyComboboxParada.minLength) { return; } int indiceSeparacaoEstado = strCiudad.indexOf(","); if (indiceSeparacaoEstado != -1) { strCiudad = strCiudad.substring(0, indiceSeparacaoEstado); } if (!strCiudad.isEmpty()) { lsCiudad = ciudadService.buscaLike(strCiudad); BindingListModel listModelCiudad = new BindingListModelList(lsCiudad, true); MyComboboxCiudad.this.setModel(listModelCiudad); indiceSelected = null; if (!lsCiudad.isEmpty()) { indiceSelected = 0; } MyComboboxCiudad.this.open(); } else { lsCiudad.clear(); BindingListModel listModelCiudad = new BindingListModelList(lsCiudad, true); MyComboboxCiudad.this.setModel(listModelCiudad); } } }); this.addEventListener("onChanging", new EventListener() { @Override public void onEvent(Event event) throws Exception { InputEvent ev = (InputEvent) event; String strCiudad = ev.getValue(); if (strCiudad.length() < 2) { lsCiudad.clear(); BindingListModel listModelCiudad = new BindingListModelList(lsCiudad, true); MyComboboxCiudad.this.setModel(listModelCiudad); MyComboboxCiudad.this.close(); } } }); } public Ciudad getInitialValue() { return initialValue; } public void setInitialValue(Ciudad initialValue) { if (initialValue == null) { return; } List ls = new ArrayList(); ls.add(initialValue); this.setModel(new BindingListModelList(ls, false)); } /** * * @param checaBusqueda * @throws WrongValueException */ public String getValue(boolean checaBusqueda) throws WrongValueException { if (checaBusqueda) { if (this.getSelectedItem() == null) { throw new WrongValueException(this, Labels.getLabel("MSG.Error.combobox.hacerBusqueda")); } } return super.getValue(); } public Comboitem getItemPorId(Integer id) { for (Ciudad cid : lsCiudad) { if(cid.getCiudadId().equals(id)) { Comboitem ciCidade = new Comboitem(cid.toString()); ciCidade.setAttribute("value", cid); ciCidade.setValue(cid); return ciCidade; } } return null; } public Integer getIndiceSelected() { return indiceSelected; } }