base

/**
 * Copyright (c) HIDGroup All rights reserved.
 * created by arwen at 2011
 */
package com.hid.action.base;

import org.springframework.stereotype.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Menuitem;

import com.thinkway.framework.po.BasePO;
import com.thinkway.framework.service.BaseService;

/**
 * HidBaseComposer
 */
@Component
public abstract class  HidBaseComposer extends GenericForwardComposer{

	private static final long serialVersionUID = 713209169273849656L;

	protected Menuitem btnAdd;
	protected Menuitem btnEdit;
	protected Menuitem btnDelete;
	protected Menuitem btnSave;

	/*
	 * current data,当前处理的对象
	 */
	protected BasePO curData;
	
	/*
	 * id value, 加载curData时,需要先给idValue赋值
	 */
	protected String idValue; 

	protected DataBinder dataBinder = new DataBinder();

   



	public void doAfterCompose(org.zkoss.zk.ui.Component comp) throws Exception {
		super.doAfterCompose(comp);

		initCompoents();

		registerBinding();

		setButtonStatus();

		attachListener();

	}

	protected void initCompoents() {
		btnAdd = (Menuitem)this.page.getFellow("commonToolBar").getFellow("btnAdd");
		btnEdit = (Menuitem)this.page.getFellow("commonToolBar").getFellow("btnEdit");
		btnDelete = (Menuitem)this.page.getFellow("commonToolBar").getFellow("btnDelete");
		btnSave = (Menuitem)this.page.getFellow("commonToolBar").getFellow("btnSave");

	}

	/**
	 * need to overwrite 注册数据和控件的映射
	 */
	protected abstract void registerBinding();

	/**
	 * 绑定数据到控件
	 */
	protected void bindDataToCompnent() {

	}

	/**
	 * load data from component
	 */

	protected void loadDataFromComponent() {

	}

	/**
	 * set button status 设置按钮状态
	 */
	protected void setButtonStatus() {
		// TODO Auto-generated method stub

	}

	/**
	 * load data 加载数据
	 */
	protected void loadData(String id) throws Exception{
		// TODO Auto-generated method stub
		curData = getService().getValue(id, getEntityClass());

	}

	/*
	 * need to implements ,
	 */
	protected abstract BaseService getService();

	/*
	 * need to implements ,
	 */
	protected abstract Class  getEntityClass();

	/*
	 * need to overwrite 设置控件的事件
	 */
	protected void attachListener() {
         btnAdd.addEventListener("onClick", new BaseBtnAddEventListener());
         btnDelete.addEventListener("onClick", new BaseBtnDeleteEventListener());
         btnEdit.addEventListener("onClick", new BaseBtnEditEventListener());
         btnSave.addEventListener("onClick", new BaseBtnSaveEventListener());
	}
	
	protected void applyDefaultValue() {
		
	}

	 public void onClick$btnAdd(Event event) throws Exception {
		 beforeAdd();
		 curData = (BasePO) getEntityClass().newInstance();
		 applyDefaultValue();
		 this.dataBinder.loadData2Component(curData);
		 afterAdd();
	 }

     protected void beforeAdd() {
		
	 }
     
     protected void afterAdd() {
 		
	 }

	public void onClick$btnEdit(Event event) throws Exception {
    	 beforeEdit();
    	 loadData(idValue);
    	 this.dataBinder.loadData2Component(curData);
    	 afterEdit();
	 }
	 protected void beforeEdit() {
	 		
	  }		
	 
	 protected void afterEdit() {
	 		
	  }	

	 public void onClick$btnDelete(Event event) throws Exception {
         beforeDelete();
         this.getService().deletePO(curData);
         afterDelete();
	 }
	 protected void beforeDelete() {
	 		
	  }	
	 protected void afterDelete() {
	 		
	  }		 
	 
	 public void onClick$btnSave(Event event) throws Exception {
         beforeSave();
         this.dataBinder.storeCompoent2Data(curData);
         this.getService().savePO(curData);
         afterSave();
	 }
	 protected void beforeSave() {
	 		
	  }	
	 
	 protected void afterSave() {
	 		
	  }	
	 
	 protected class BaseBtnAddEventListener implements EventListener {

		/* (non-Javadoc)
		 * @see org.zkoss.zk.ui.event.EventListener#onEvent(org.zkoss.zk.ui.event.Event)
		 */
		public void onEvent(Event event) throws Exception {
			if (event.getName().equals("onClick")) {
				HidBaseComposer.this.onClick$btnAdd(event);
			}

		}

	 }
	 protected class BaseBtnEditEventListener implements EventListener {

		 /* (non-Javadoc)
		  * @see org.zkoss.zk.ui.event.EventListener#onEvent(org.zkoss.zk.ui.event.Event)
		  */
		 public void onEvent(Event event) throws Exception {
			 if (event.getName().equals("onClick")) {
				 HidBaseComposer.this.onClick$btnEdit(event);
			 }

		 }

	 }
	 protected class BaseBtnDeleteEventListener implements EventListener {

		 /* (non-Javadoc)
		  * @see org.zkoss.zk.ui.event.EventListener#onEvent(org.zkoss.zk.ui.event.Event)
		  */
		 public void onEvent(Event event) throws Exception {
			 if (event.getName().equals("onClick")) {
				 HidBaseComposer.this.onClick$btnDelete(event);
			 }

		 }

	 }
	 protected class BaseBtnSaveEventListener implements EventListener {

		 /* (non-Javadoc)
		  * @see org.zkoss.zk.ui.event.EventListener#onEvent(org.zkoss.zk.ui.event.Event)
		  */
		 public void onEvent(Event event) throws Exception {
			 if (event.getName().equals("onClick")) {
				 HidBaseComposer.this.onClick$btnSave(event);
			 }

		 }

	 }
}

你可能感兴趣的:(AS)