001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.corelib.components;
016:
017: import org.apache.tapestry.ComponentResources;
018: import org.apache.tapestry.Link;
019: import org.apache.tapestry.MarkupWriter;
020: import org.apache.tapestry.TapestryConstants;
021: import org.apache.tapestry.annotations.Inject;
022: import org.apache.tapestry.annotations.Parameter;
023: import org.apache.tapestry.grid.GridDataSource;
024: import org.apache.tapestry.ioc.Messages;
025:
026: /**
027: * Generates a series of links used to jump to a particular page index within the overall data set.
028: */
029: public class GridPager {
030: /**
031: * The source of the data displayed by the grid (this is used to determine
032: * {@link GridDataSource#getAvailableRows() how many rows are available}, which in turn
033: * determines the page count).
034: */
035: @Parameter(required=true)
036: private GridDataSource _source;
037:
038: /** The number of rows displayed per page. */
039: @Parameter(required=true)
040: private int _rowsPerPage;
041:
042: /** The current page number (indexed from 1). */
043: @Parameter(required=true)
044: private int _currentPage;
045:
046: /**
047: * Number of pages before and after the current page in the range. The pager always displays
048: * links for 2 * range + 1 pages, unless that's more than the total number of available pages.
049: */
050: @Parameter("5")
051: private int _range;
052:
053: private int _lastIndex;
054:
055: private int _maxPages;
056:
057: @Inject
058: private ComponentResources _resources;
059:
060: @Inject
061: private Messages _messages;
062:
063: void beginRender(MarkupWriter writer) {
064: int availableRows = _source.getAvailableRows();
065:
066: _maxPages = ((availableRows - 1) / _rowsPerPage) + 1;
067:
068: if (_maxPages < 2)
069: return;
070:
071: writer.element("div", "class", "t-data-grid-pager");
072:
073: _lastIndex = 0;
074:
075: for (int i = 1; i <= 2; i++)
076: writePageLink(writer, i);
077:
078: int low = _currentPage - _range;
079: int high = _currentPage + _range;
080:
081: if (low < 1) {
082: low = 1;
083: high = 2 * _range + 1;
084: } else {
085: if (high > _maxPages) {
086: high = _maxPages;
087: low = high - 2 * _range;
088: }
089: }
090:
091: for (int i = low; i <= high; i++)
092: writePageLink(writer, i);
093:
094: for (int i = _maxPages - 1; i <= _maxPages; i++)
095: writePageLink(writer, i);
096:
097: writer.end();
098: }
099:
100: private void writePageLink(MarkupWriter writer, int pageIndex) {
101: if (pageIndex < 1 || pageIndex > _maxPages)
102: return;
103:
104: if (pageIndex <= _lastIndex)
105: return;
106:
107: if (pageIndex != _lastIndex + 1)
108: writer.write(" ... "); // … is ellipsis
109:
110: _lastIndex = pageIndex;
111:
112: if (pageIndex == _currentPage) {
113: writer.element("span", "class", "current");
114: writer.write(Integer.toString(pageIndex));
115: writer.end();
116: return;
117: }
118:
119: Link link = _resources.createActionLink(
120: TapestryConstants.ACTION_EVENT, false, pageIndex);
121:
122: writer.element("a", "href", link, "title", _messages.format(
123: "goto-page", pageIndex));
124:
125: writer.write(Integer.toString(pageIndex));
126: writer.end();
127:
128: }
129:
130: void onAction(int newPage) {
131: // TODO: Validate newPage in range
132:
133: _currentPage = newPage;
134: }
135: }
|