001: /*
002: * Copyright 2006 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: * any nuclear facility.
035: */
036:
037: package com.sun.portal.app.communityportlets.faces;
038:
039: import javax.faces.component.html.HtmlDataTable;
040: import javax.faces.model.ArrayDataModel;
041:
042: /**
043: *
044: * @author #ac120954
045: */
046: public class PaginatingTable {
047: private HtmlDataTable dataTable = new HtmlDataTable();
048: private ArrayDataModel dataModel;
049:
050: /** Creates a new instance of PaginatingTable */
051: public PaginatingTable(ArrayDataModel dataModel) {
052: this .dataModel = dataModel;
053: }
054:
055: public HtmlDataTable getDataTable() {
056: return dataTable;
057: }
058:
059: public void setDataTable(HtmlDataTable dataTable) {
060: this .dataTable = dataTable;
061: }
062:
063: public ArrayDataModel getDataModel() {
064: return dataModel;
065: }
066:
067: private void adjustCurrentPage() {
068: if (dataTable != null && dataModel != null) {
069: int first = dataTable.getFirst();
070: int rows = dataTable.getRows();
071: int total = dataModel.getRowCount();
072: for (; first > 0 && first >= total; first = first - rows)
073: ;
074: dataTable.setFirst(first);
075: }
076: }
077:
078: public void setDataModel(ArrayDataModel dataModel) {
079: this .dataModel = dataModel;
080: adjustCurrentPage();
081: }
082:
083: public boolean isTableHasNext() {
084: int first = dataTable.getFirst();
085: int rows = dataTable.getRows();
086: if (rows == 0)
087: return false;
088: int total = dataModel.getRowCount();
089: return (first + rows) < total;
090: }
091:
092: public String nextPage() {
093: int first = dataTable.getFirst();
094: int rows = dataTable.getRows();
095: dataTable.setFirst(first + rows);
096: return null;
097: }
098:
099: public String previousPage() {
100: int first = dataTable.getFirst();
101: int rows = dataTable.getRows();
102: dataTable.setFirst(first - rows);
103: return null;
104: }
105:
106: public boolean isTableHasPrevious() {
107: int first = dataTable.getFirst();
108: return first > 0;
109: }
110:
111: public int getStartIndex() {
112: if (dataModel.getRowCount() == 0) {
113: return 0;
114: }
115: return dataTable.getFirst() + 1;
116: }
117:
118: public int getEndIndex() {
119: int first = dataTable.getFirst();
120: int rows = dataTable.getRows();
121: int total = dataModel.getRowCount();
122: if (first + rows <= total) {
123: return first + rows;
124: } else {
125: return total;
126: }
127:
128: }
129:
130: public int getTotal() {
131: return dataModel.getRowCount();
132: }
133:
134: }
|