package com.ebpm.webdemo.zk;

import java.util.Collection;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;

import com.ebpm.webdemo.common.PersonBean;
import com.ebpm.webdemo.common.PersonDAO;

/**
 * SimpleComposer6.java
 
 * Composer holds the models and bean
 
 */
public class SimpleComposer6 extends GenericForwardComposer {

  public PersonBean personBean = new PersonBean();
  private AnnotateDataBinder binder;
  private Component input;  // get the input-root element for partial load/save binding
  private ListModelList listModel;
  private Listbox personsList;

  // databean for the inputform
  public PersonBean getPersonBean() {
    return personBean;
  }
  // model for the listbox
  public Collection<PersonBean> getAllPersons() {
    return PersonDAO.getInstance().getAllPersons();
  }
  @Override
  public void doAfterCompose(Component winthrows Exception {
        super.doAfterCompose(win);
        win.setVariable("controller", this, false);  // make models visible for the databinder
  }
  
  public void onCreate$simpleWindow(ForwardEvent event) {
    // get the databinder for later extra load and save
    // note: binder is not ready in doAfterCompose, so do it here
    binder = (AnnotateDataBinderevent.getTarget().getVariable("binder"false);
    // get the model via Window / Listbox / Model
    listModel = (ListModelListpersonsList.getModel();
  }
  /*
   * Process Submitbutton
   */
  public void onClick$submit() {
    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
  }
  /*
   * process Cancelbutton
   */
  public void onClick$cancel() {
    Executions.sendRedirect("/index.jsp");
  }
}