📄 mypaging.java
字号:
package mycomp.paging;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.zkoss.zk.ui.HtmlMacroComponent;
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.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.event.PagingEvent;
import org.zkoss.zul.event.ZulEvents;
import org.zkoss.zul.ext.Paginal;
/**
* @author Dennis.Chen
*
*/
public class MyPaging 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;
private Toolbarbutton _firstBtn;
private Toolbarbutton _prevBtn;
private Toolbarbutton _nextBtn;
private Toolbarbutton _lastBtn;
private Intbox _intBox;
private Label _totalPageLab;
private Label _totalSizeLab;
private String _fisrtImageSrc = "~./mycomp/images/pagination_first.png";
private String _prevImageSrc = "~./mycomp/images/pagination_prev.png";
private String _nextImageSrc = "~./mycomp/images/pagination_next.png";
private String _lastImageSrc = "~./mycomp/images/pagination_last.png";
private String _fisrtImageDisSrc = "~./mycomp/images/pagination_first_dis.png";
private String _prevImageDisSrc = "~./mycomp/images/pagination_prev_dis.png";
private String _nextImageDisSrc = "~./mycomp/images/pagination_next_dis.png";
private String _lastImageDisSrc = "~./mycomp/images/pagination_last_dis.png";
List _interactor = new ArrayList();
boolean _init = false;
public void onCreate(){
//defer initial.
this.addEventListener("onCreateLater",new EventListener(){
public void onEvent(Event arg0) throws Exception {
doCreateLater();
}}
);
Events.postEvent("onCreateLater",this,null);
this.addEventListener(ZulEvents.ON_PAGING,new EventListener(){
public void onEvent(Event evt) throws Exception {
Iterator iter = _interactor.iterator();
while(iter.hasNext()){
MyPaging cmp = (MyPaging)iter.next();
if(cmp==MyPaging.this) continue;
cmp.onGogo(((PagingEvent)evt).getActivePage());
}
}}
);
}
public void addInteractor(MyPaging paging){
_interactor.add(paging);
}
public void removeInteractor(MyPaging paging){
_interactor.remove(paging);
}
private void doCreateLater(){
_firstBtn = (Toolbarbutton)getFellow("firstBtn");
_prevBtn = (Toolbarbutton)getFellow("prevBtn");
_nextBtn = (Toolbarbutton)getFellow("nextBtn");
_lastBtn = (Toolbarbutton)getFellow("lastBtn");
_intBox = (Intbox)getFellow("intBox");
_totalPageLab = (Label)getFellow("totalPageLab");
_totalSizeLab = (Label)getFellow("totalSizeLab");
_firstBtn.addEventListener("onClick",new EventListener(){
public void onEvent(Event arg0) throws Exception {
onFirstBtnClick();
}});
_prevBtn.addEventListener("onClick",new EventListener(){
public void onEvent(Event arg0) throws Exception {
onPreviousBtnClick();
}});
_nextBtn.addEventListener("onClick",new EventListener(){
public void onEvent(Event arg0) throws Exception {
onNextBtnClick();
}});
_lastBtn.addEventListener("onClick",new EventListener(){
public void onEvent(Event arg0) throws Exception {
onLastBtnClick();
}});
_intBox.addEventListener("onOK",new EventListener(){
public void onEvent(Event arg0) throws Exception {
if(_intBox.getValue()==null){
updateUI();
}else{
onGogo(_intBox.getValue().intValue()-1);
}
}});
_intBox.addEventListener("onChange",new EventListener(){
public void onEvent(Event arg0) throws Exception {
if(_intBox.getValue()==null){
updateUI();
}else{
onGogo(_intBox.getValue().intValue()-1);
}
}});
_init = true;
this.setStyle("");//clear
this.setSclass("");//clear
updateUI();
}
protected void fireEvent(PagingEvent event){
setActivePage(event.getActivePage());
Events.postEvent(event);
}
private void onFirstBtnClick(){
if(_activePage!=0){
onGogo(0);
}
}
private void onPreviousBtnClick(){
if(_activePage>0){
onGogo(_activePage-1);
}
}
private void onNextBtnClick(){
if(_activePage<_pageCount){
onGogo(_activePage+1);
}
}
private void onLastBtnClick(){
if(_activePage!=_pageCount-1){
onGogo(_pageCount-1);
}
}
private void updateUI(){
if(!_init) return;
_totalPageLab.setValue(new Integer(_pageCount).toString());
_totalSizeLab.setValue(new Integer(_totalSize).toString());
_intBox.setValue(new Integer((_activePage+1)));
if(_activePage<=0){
_firstBtn.setSrc(_fisrtImageDisSrc);
_prevBtn.setSrc(_prevImageDisSrc);
}else{
_firstBtn.setSrc(_fisrtImageSrc);
_prevBtn.setSrc(_prevImageSrc);
}
if(_activePage>=_pageCount-1){
_lastBtn.setSrc(_lastImageDisSrc);
_nextBtn.setSrc(_nextImageDisSrc);
}else{
_lastBtn.setSrc(_lastImageSrc);
_nextBtn.setSrc(_nextImageSrc);
}
}
private void onGogo(int i){
if(i<0){
i = 0;
}else if(i>=_pageCount){
i = _pageCount -1;
}
if(_activePage!=i){
_activePage = i;
fireEvent(new PagingEvent(ZulEvents.ON_PAGING,this,_activePage));
}
updateUI();
}
/* (non-Javadoc)
* @see org.zkoss.zul.ext.Paginal#getPageIncrement()
*/
public int getPageIncrement() {
return _pageIncrement;
}
/* (non-Javadoc)
* @see org.zkoss.zul.ext.Paginal#getTotalSize()
*/
public int getTotalSize() {
return _totalSize;
}
/* (non-Javadoc)
* @see org.zkoss.zul.ext.Paginal#isDetailed()
*/
public boolean isDetailed() {
// TODO Auto-generated method stub
return _detailed;
}
/* (non-Javadoc)
* @see org.zkoss.zul.ext.Paginal#setDetailed(boolean)
*/
public void setDetailed(boolean detailed) {
_detailed = detailed;
}
/* (non-Javadoc)
* @see org.zkoss.zul.ext.Paginal#setPageIncrement(int)
*/
public void setPageIncrement(int pginc) throws WrongValueException {
_pageIncrement = pginc;
}
/* (non-Javadoc)
* @see org.zkoss.zul.ext.Paginal#setTotalSize(int)
*/
public void setTotalSize(int size) throws WrongValueException {
this.setTotalSize0(size);
Iterator iter = _interactor.iterator();
while(iter.hasNext()){
MyPaging cmp = (MyPaging)iter.next();
if(cmp==MyPaging.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();
}
/* (non-Javadoc)
* @see org.zkoss.zul.ext.Pageable#getActivePage()
*/
public int getActivePage() {
return _activePage;
}
/* (non-Javadoc)
* @see org.zkoss.zul.ext.Pageable#getPageCount()
*/
public int getPageCount() {
return _pageCount;
}
/* (non-Javadoc)
* @see org.zkoss.zul.ext.Pageable#getPageSize()
*/
public int getPageSize() {
return _pageSize;
}
/* (non-Javadoc)
* @see org.zkoss.zul.ext.Pageable#setActivePage(int)
*/
public void setActivePage(int pg) throws WrongValueException {
_activePage = pg;
Events.postEvent(new PagingEvent("onPagingImpl", this, _activePage));
}
public void setPageSize(int size) throws WrongValueException {
this.setPageSize0(size);
Iterator iter = _interactor.iterator();
while(iter.hasNext()){
MyPaging cmp = (MyPaging)iter.next();
if(cmp==MyPaging.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));
}
public String getFisrtImageSrc() {
return _fisrtImageSrc;
}
public void setFisrtImageSrc(String fisrtImageSrc) {
this._fisrtImageSrc = fisrtImageSrc;
}
public String getPrevImageSrc() {
return _prevImageSrc;
}
public void setPrevImageSrc(String prevImageSrc) {
this._prevImageSrc = prevImageSrc;
}
public String getNextImageSrc() {
return _nextImageSrc;
}
public void setNextImageSrc(String nextImageSrc) {
this._nextImageSrc = nextImageSrc;
}
public String getLastImageSrc() {
return _lastImageSrc;
}
public void setLastImageSrc(String lastImageSrc) {
this._lastImageSrc = lastImageSrc;
}
public String getFisrtImageDisSrc() {
return _fisrtImageDisSrc;
}
public void setFisrtImageDisSrc(String fisrtImageDisSrc) {
this._fisrtImageDisSrc = fisrtImageDisSrc;
}
public String getPrevImageDisSrc() {
return _prevImageDisSrc;
}
public void setPrevImageDisSrc(String prevImageDisSrc) {
this._prevImageDisSrc = prevImageDisSrc;
}
public String getNextImageDisSrc() {
return _nextImageDisSrc;
}
public void setNextImageDisSrc(String nextImageDisSrc) {
this._nextImageDisSrc = nextImageDisSrc;
}
public String getLastImageDisSrc() {
return _lastImageDisSrc;
}
public void setLastImageDisSrc(String lastImageDisSrc) {
this._lastImageDisSrc = lastImageDisSrc;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -