package mycomp.paging;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.HtmlMacroComponent;
import org.zkoss.zk.ui.Page;
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.Events;
import org.zkoss.zul.event.PagingEvent;
import org.zkoss.zul.event.ZulEvents;
import org.zkoss.zul.ext.Paginal;
/**
* A component to handle paging behavior.
* @author Dennis.Chen
*
*/
public abstract class PagingBase extends HtmlMacroComponent implements Paginal {
private static final long serialVersionUID = 1L;
private int _pageIncrement;
private int _totalSize;
private boolean _detailed;
private int _activePage;
private int _pageCount;
private int _pageSize = 10;
List _interactor = new ArrayList();
boolean _init = false;
public PagingBase(){
this.addEventListener("onCreateLater",new EventListener(){
public void onEvent(Event arg0) throws Exception {
doCreateLater();
}}
);
this.addEventListener(ZulEvents.ON_PAGING,new EventListener(){
public void onEvent(Event evt) throws Exception {
Iterator iter = _interactor.iterator();
while(iter.hasNext()){
PagingBase cmp = (PagingBase)iter.next();
if(cmp==PagingBase.this) continue;
cmp.onGogo(((PagingEvent)evt).getActivePage());
}
}}
);
}
public void onPageAttached(Page newpage,Page oldpage){
super.onPageAttached(newpage, oldpage);
//defer initial.
if(!_init) Events.postEvent("onCreateLater",this,null);
}
public void addInteractor(PagingBase paging){
_interactor.add(paging);
}
public void removeInteractor(PagingBase paging){
_interactor.remove(paging);
}
protected boolean isInit(){
return _init;
}
abstract protected String getMacroRootId();
abstract protected String getMacroUri();
abstract protected void initial();
abstract protected void updateUI();
private void doCreateLater(){
if(_init) return;
if(getFellowIfAny(getMacroRootId())==null){
//if not found, mean this component is created by java code , not by zul
//so i need to load template
Executions.getCurrent().createComponents(getMacroUri(),this,null);
}
initial();
_init = true;
updateUI();
}
protected void firePagingEvent(PagingEvent event){
setActivePage(event.getActivePage());
Events.postEvent(event);
}
protected void onGogo(int i){
if(i<0){
i = 0;
}else if(i>=_pageCount){
i = _pageCount -1;
}
if(_totalSize>0 && _activePage!=i){
_activePage = i;
firePagingEvent(new PagingEvent(ZulEvents.ON_PAGING,this,_activePage));
}
updateUI();
}
public int getPageIncrement() {
return _pageIncrement;
}
public int getTotalSize() {
return _totalSize;
}
public boolean isDetailed() {
return _detailed;
}
public void setDetailed(boolean detailed) {
_detailed = detailed;
}
public void setPageIncrement(int pginc) throws WrongValueException {
_pageIncrement = pginc;
}
public void setTotalSize(int size) throws WrongValueException {
this.setTotalSize0(size);
Iterator iter = _interactor.iterator();
while(iter.hasNext()){
PagingBase cmp = (PagingBase)iter.next();
if(cmp==PagingBase.this) continue;
cmp.setTotalSize0(size);
}
}
private void setTotalSize0(int size) throws WrongValueException {
_totalSize = size;
_pageCount = (int)(_totalSize/_pageSize) + ((_totalSize%_pageSize==0)?0:1);
if(_activePage > (_pageCount-1)){
onGogo(_pageCount-1);
}
updateUI();
}
public int getActivePage() {
return _activePage;
}
public int getPageCount() {
return _pageCount;
}
public int getPageSize() {
return _pageSize;
}
public void setActivePage(int pg) throws WrongValueException {
this.setActivePage0(pg);
Iterator iter = _interactor.iterator();
while(iter.hasNext()){
PagingBase cmp = (PagingBase)iter.next();
if(cmp==PagingBase.this) continue;
cmp.setActivePage0(pg);
}
}
public void setActivePage0(int pg) throws WrongValueException {
_activePage = pg;
updateUI();
Events.postEvent(new PagingEvent("onPagingImpl", this, _activePage));
}
public void setPageSize(int size) throws WrongValueException {
this.setPageSize0(size);
Iterator iter = _interactor.iterator();
while(iter.hasNext()){
PagingBase cmp = (PagingBase)iter.next();
if(cmp==PagingBase.this) continue;
cmp.setPageSize0(size);
}
}
public void setPageSize0(int size) throws WrongValueException {
_pageSize = size;
_pageCount = (int)(_totalSize/_pageSize) + ((_totalSize%_pageSize==0)?0:1);
updateUI();
Events.postEvent(new PagingEvent("onPagingImpl", this, _activePage));
}
}