001: /*
002: * Copyright 2006-2007 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.module.crud;
018:
019: import org.romaframework.aspect.core.annotation.AnnotationConstants;
020: import org.romaframework.aspect.core.annotation.CoreClass;
021: import org.romaframework.aspect.i18n.I18NAspect;
022: import org.romaframework.aspect.persistence.PersistenceConstants;
023: import org.romaframework.aspect.persistence.annotation.Persistence;
024: import org.romaframework.aspect.view.annotation.ViewAction;
025: import org.romaframework.aspect.view.annotation.ViewClass;
026: import org.romaframework.aspect.view.annotation.ViewField;
027: import org.romaframework.core.flow.ObjectContext;
028:
029: /**
030: * Handles the paging in CRUD Main.
031: *
032: * @author Luca Garulli (luca.garulli@assetdata.it)
033: */
034: @CoreClass(orderFields="totalItemsLabel totalItems pageLabel pages",orderActions="first prev next last queryAll csv")
035: @ViewClass(label="")
036: public class CRUDPaging {
037:
038: public CRUDPaging(PagingListener iListener) {
039: currentPage = 1;
040: listener = iListener;
041: pages = new Integer[] {};
042: }
043:
044: @Persistence(mode=PersistenceConstants.MODE_NOTX)
045: @ViewAction(label="")
046: public void first() {
047: setCurrentPage(1);
048: }
049:
050: @Persistence(mode=PersistenceConstants.MODE_NOTX)
051: @ViewAction(label="")
052: public void prev() {
053: if (currentPage > 1)
054: setCurrentPage(currentPage - 1);
055: }
056:
057: @Persistence(mode=PersistenceConstants.MODE_NOTX)
058: @ViewAction(label="")
059: public void next() {
060: if (currentPage < pages.length)
061: setCurrentPage(currentPage + 1);
062: }
063:
064: @Persistence(mode=PersistenceConstants.MODE_NOTX)
065: @ViewAction(label="")
066: public void last() {
067: setCurrentPage(pages.length);
068: }
069:
070: @Persistence(mode=PersistenceConstants.MODE_NOTX)
071: @ViewAction(label="")
072: public void queryAll() {
073: setCurrentPage(0);
074: }
075:
076: public int getCurrentPage() {
077: return currentPage;
078: }
079:
080: @Persistence(mode=PersistenceConstants.MODE_NOTX)
081: public void setCurrentPage(int iCurrentPage) {
082: if (pages.length == 0)
083: // NO PAGES LOADED YET: TAKES NO EFFECT
084: return;
085:
086: if (iCurrentPage == currentPage)
087: return;
088:
089: this .currentPage = iCurrentPage;
090: listener.loadPage(iCurrentPage);
091:
092: ObjectContext.getInstance().refresh(this , "pages");
093: }
094:
095: @ViewField(label="",render="label",layout="form:actions")
096: public String getTotalItemsLabel() {
097: return ObjectContext.getInstance().getComponent(
098: I18NAspect.class).resolveString(getClass(),
099: TOTAL_ITEMS_LABEL);
100: }
101:
102: @ViewField(label="",render="label",layout="form:actions")
103: public String getPageLabel() {
104: return ObjectContext.getInstance().getComponent(
105: I18NAspect.class).resolveString(getClass(), PAGE_LABEL);
106: }
107:
108: public Integer[] getPages() {
109: return pages;
110: }
111:
112: /**
113: * Fill the select containing all available pages
114: *
115: * @param iPageTotal
116: * Total pages
117: */
118: public void setPages(int iPageTotal) {
119: pages = new Integer[iPageTotal];
120: for (int i = 0; i < iPageTotal; ++i)
121: pages[i] = i + 1;
122:
123: ObjectContext.getInstance().refresh(this , "pages");
124: }
125:
126: public int getTotalItems() {
127: return totalItems;
128: }
129:
130: public void setTotalItems(int totalItems) {
131: this .totalItems = totalItems;
132: }
133:
134: @ViewField(label="",render="text",enabled=AnnotationConstants.FALSE,layout="form:actions")
135: protected int totalItems;
136:
137: @ViewField(visible=AnnotationConstants.FALSE)
138: protected int currentPage;
139:
140: @ViewField(label="",visible=AnnotationConstants.TRUE,selectionField="currentPage",render="select",layout="form:actions")
141: protected Integer[] pages;
142:
143: protected PagingListener listener;
144:
145: private static final String PAGE_LABEL = "$page.label";
146: private static final String TOTAL_ITEMS_LABEL = "$totalItems.label";
147: }
|