001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037: package com.sun.portal.iwayutil.sorting;
038:
039: /**
040: *
041: * This class provides the utility methods sort the table data
042: *
043: * @version 1.0
044: * @author Deepak H P
045: * @date 02 May 2005
046: *
047: **/
048:
049: import java.util.*;
050: import javax.portlet.*;
051:
052: public class TablePagination {
053:
054: public static void setPaginationRow(RenderRequest request,
055: RenderResponse response, String paginationType,
056: String query, int size) {
057:
058: StringBuffer pagRowBuf = new StringBuffer();
059: String tableImagesLocation = response.encodeURL(request
060: .getContextPath())
061: + "/images/table/";
062: String imgPagFirst = tableImagesLocation
063: + "pagination_first.gif";
064: String imgPagLast = tableImagesLocation + "pagination_last.gif";
065: String imgPagNext = tableImagesLocation + "pagination_next.gif";
066: String imgPagPrev = tableImagesLocation + "pagination_prev.gif";
067: String imgPagDot = tableImagesLocation + "dot.gif";
068: String curPageStr = (String) request
069: .getAttribute("CurrentPage");
070: String curSumPageStr = (String) request
071: .getAttribute("CurrentSummaryPage");
072:
073: String actionURL = response.createActionURL().toString();
074: String firstActionURL = null;
075: String lastActionURL = null;
076: String prevActionURL = null;
077: String nextActionURL = null;
078:
079: String firstPageTitle = null;
080: String prevPageTitle = null;
081: String nextPageTitle = null;
082: String lastPageTitle = null;
083: String pageTitle = null;
084: String ofTitle = null;
085: String goButtonTitle = null;
086:
087: int firstPage = 0;
088: int lastPage = 0;
089: int prevPage = 0;
090: int nextPage = 0;
091: int curPage = 0;
092: int curSumPage = 0;
093:
094: try {
095: ResourceBundle rb = ResourceBundle
096: .getBundle("IWayUtilMessages");
097: firstPageTitle = rb.getString("GO_TO_FIRST_PAGE_TITLE");
098: prevPageTitle = rb.getString("GO_TO_PREV_PAGE_TITLE");
099: nextPageTitle = rb.getString("GO_TO_NEXT_PAGE_TITLE");
100: lastPageTitle = rb.getString("GO_TO_LAST_PAGE_TITLE");
101: pageTitle = rb.getString("PAGE_TITLE");
102: ofTitle = rb.getString("OF_TITLE");
103: goButtonTitle = rb.getString("GO_BUTTON_TITLE");
104: } catch (Exception ex) {
105: firstPageTitle = "Go to First Page";
106: prevPageTitle = "Go to Previous Page";
107: nextPageTitle = "Go to Next Page";
108: lastPageTitle = "Go to Last Page";
109: pageTitle = "Page";
110: ofTitle = "of";
111: goButtonTitle = "GO";
112: }
113: debug("curPageStr : " + curPageStr + " curSumPageStr : "
114: + curSumPageStr);
115:
116: try {
117: curSumPage = Integer.parseInt(curSumPageStr);
118: if (curSumPage < 1) {
119: curSumPageStr = "1";
120: }
121: } catch (Exception ex) {
122: curSumPageStr = "1";
123: }
124:
125: try {
126: curPage = Integer.parseInt(curPageStr);
127: } catch (Exception ex) {
128:
129: if (size > 0) {
130: curPage = 1;
131: } else {
132: curPage = 0;
133: }
134: }
135:
136: debug("RowData Size: " + size);
137:
138: if (size > 0) {
139: firstPage = 1;
140: lastPage = size;
141:
142: if (curPage < lastPage) {
143: nextPage = curPage + 1;
144: } else {
145: nextPage = lastPage;
146: }
147:
148: if (curPage > firstPage) {
149: prevPage = curPage - 1;
150: } else {
151: prevPage = firstPage;
152: }
153: }
154:
155: firstActionURL = actionURL + "&PaginationType="
156: + paginationType + "&CurrentSummaryPage="
157: + curSumPageStr
158: + "&RequestType=Paginate&QueryField=first&QueryValue="
159: + firstPage;
160: lastActionURL = actionURL + "&PaginationType=" + paginationType
161: + "&CurrentSummaryPage=" + curSumPageStr
162: + "&RequestType=Paginate&QueryField=last&QueryValue="
163: + lastPage;
164: prevActionURL = actionURL + "&PaginationType=" + paginationType
165: + "&CurrentSummaryPage=" + curSumPageStr
166: + "&RequestType=Paginate&QueryField=prev&QueryValue="
167: + prevPage;
168: nextActionURL = actionURL + "&PaginationType=" + paginationType
169: + "&CurrentSummaryPage=" + curSumPageStr
170: + "&RequestType=Paginate&QueryField=next&QueryValue="
171: + nextPage;
172:
173: if (query != null) {
174: String cQuery = "&CurrentQuery=" + query;
175: firstActionURL = firstActionURL + cQuery;
176: lastActionURL = lastActionURL + cQuery;
177: prevActionURL = prevActionURL + cQuery;
178: nextActionURL = nextActionURL + cQuery;
179: }
180:
181: if (curPage == firstPage) {
182: imgPagFirst = tableImagesLocation
183: + "pagination_first_dis.gif";
184: imgPagPrev = tableImagesLocation
185: + "pagination_prev_dis.gif";
186: firstActionURL = "#";
187: prevActionURL = "#";
188: }
189:
190: if (curPage == lastPage) {
191: imgPagLast = tableImagesLocation
192: + "pagination_last_dis.gif";
193: imgPagNext = tableImagesLocation
194: + "pagination_next_dis.gif";
195: nextActionURL = "#";
196: lastActionURL = "#";
197: }
198: debug("curPage : " + curPage + " fPage : " + firstPage
199: + " lPage : " + lastPage);
200: pagRowBuf.append("<form action='");
201: pagRowBuf.append(actionURL);
202: pagRowBuf.append("' method=post>");
203: pagRowBuf.append("<tr>");
204: pagRowBuf
205: .append("<td class=\"TblActTdLst\" colspan=\"5\" nowrap=\"nowrap\">");
206: pagRowBuf.append("<a href=" + "\"" + firstActionURL + "\""
207: + " title=\"" + firstPageTitle + "\">");
208: pagRowBuf
209: .append("<img src="
210: + imgPagFirst
211: + " alt=\""
212: + firstPageTitle
213: + "\" align=\"top\" border=\"0\" height=\"20\" width=\"23\">");
214: pagRowBuf.append("</a>");
215: pagRowBuf.append("<a href=" + "\"" + prevActionURL + "\""
216: + " title=\"" + prevPageTitle + "\">");
217: pagRowBuf
218: .append("<img src="
219: + imgPagPrev
220: + " alt=\""
221: + prevPageTitle
222: + "\" align=\"top\" border=\"0\" height=\"20\" width=\"23\">");
223: pagRowBuf.append("</a>");
224: pagRowBuf.append("<img src=" + imgPagDot
225: + " alt=\"\" height=\"1\" width=\"10\">");
226: pagRowBuf
227: .append("<span class=\"LblLev2Txt\"> <label for=\"pagenumfield\">"
228: + pageTitle + ": </label> </span>");
229:
230: pagRowBuf
231: .append("<input type='hidden' name='RequestType' value='Paginate'>");
232: pagRowBuf
233: .append("<input type='hidden' name='PaginationType' value='");
234: pagRowBuf.append(paginationType);
235: pagRowBuf.append("'>");
236: pagRowBuf
237: .append("<input type='hidden' name='TableSize' value='");
238: pagRowBuf.append(size);
239: pagRowBuf.append("'>");
240: pagRowBuf
241: .append("<input type='hidden' name='CurrentSummaryPage' value='");
242: pagRowBuf.append(curSumPageStr);
243: pagRowBuf.append("'>");
244: pagRowBuf
245: .append("<input type='hidden' name='QueryField' value='goTo'>");
246:
247: if (query != null) {
248: pagRowBuf
249: .append("<input type='hidden' name='CurrentQuery' value=\""
250: + query + "\">");
251: }
252: pagRowBuf
253: .append("<input name='QueryValue' class=\"TxtFld\" value="
254: + curPage + " size=\"3\" type=\"text\"> ");
255: pagRowBuf.append("<span class=\"TblPgnTxt\">" + ofTitle + " "
256: + size + " </span>");
257: pagRowBuf
258: .append("<input class=\"Btn1\" type=\"submit\" name=\"goButton\" value=\""
259: + goButtonTitle + "\" />");
260: pagRowBuf.append("<img src=" + imgPagDot
261: + " alt=\"\" height=\"1\" width=\"10\">");
262: pagRowBuf.append("<a href=" + "\"" + nextActionURL + "\""
263: + " title=\"" + nextPageTitle + "\">");
264: pagRowBuf
265: .append("<img src="
266: + imgPagNext
267: + " alt=\""
268: + nextPageTitle
269: + "\" align=\"top\" border=\"0\" height=\"20\" width=\"23\">");
270: pagRowBuf.append("</a>");
271: pagRowBuf.append("<a href=" + "\"" + lastActionURL + "\""
272: + " title=\"" + lastPageTitle + "\">");
273: pagRowBuf
274: .append("<img src="
275: + imgPagLast
276: + " alt=\""
277: + lastPageTitle
278: + "\" align=\"top\" border=\"0\" height=\"20\" width=\"23\">");
279: pagRowBuf.append("</a>");
280: pagRowBuf.append("<img src=" + imgPagDot
281: + " alt=\"\" height=\"1\" width=\"10\">");
282: pagRowBuf.append("</td>");
283: pagRowBuf.append("</tr>");
284: pagRowBuf.append("</form>");
285:
286: String pagRowStr = pagRowBuf.toString();
287: request.setAttribute("PaginationRow", pagRowStr);
288:
289: }
290:
291: /**
292: * This method will print the debug messages.
293: **/
294: private static void debug(Object msg) {
295: System.out.println("TablePagination : " + msg);
296: }
297: }
|