package com.ebpm.webdemo.zk;

import java.util.Collection;
import java.util.Map;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Grid;
import org.zkoss.zul.ListModelArray;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Window;
import com.ebpm.webdemo.common.PersonBean;
import com.ebpm.webdemo.common.PersonDAO;

/**
 * SimpleWindow3.java
 
 * 1) get and use model from listbox for new entries
 * 2) define an inputgroup to controll the area for databinding
 */
public class SimpleWindow3 extends Window {

  private static final long serialVersionUID = 3975852223657115755L;
  public PersonBean personBean = new PersonBean();
  private AnnotateDataBinder binder;
  private Component input;
  private ListModelList listModel;

  public PersonBean getPersonBean() {
    return personBean;
  }

  public Collection getAllPersons() {
    return  PersonDAO.getInstance().getAllPersons();
  }

  public void onCreate() {
    // get the databinder for later extra load and save
    binder = (AnnotateDataBinderthis.getVariable("binder"false);

    // get the input-root element for partial load/save binding
    input = this.getFellow("input");

    // get the model via Window / Listbox / Model
    Listbox listbox = (Listboxthis.getFellow("personsList");
    listModel = (ListModelListlistbox.getModel();
  }
  /*
   * Process Submitbutton and enter-key
   */
  public void onOK() {
    binder.saveComponent(input)//load Inputfields from Form, Constraints will be performed
    PersonDAO.getInstance().addPerson(personBean);  // add the person (from inputfields) to model
    listModel.add(personBean);                      // add only the new person to UI   
    this.personBean = new PersonBean()// prepare the next input

    binder.loadComponent(input)// reload only input-area 
  }
  /*
   * process Cancelbutton and esc-key
   */
  public void onCancel() {
    Executions.sendRedirect("/index.jsp");
  }
}