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 java.util.List;
018:
019: import org.apache.tapestry.annotations.Parameter;
020: import org.apache.tapestry.beaneditor.PropertyModel;
021: import org.apache.tapestry.grid.GridDataSource;
022: import org.apache.tapestry.grid.GridModelProvider;
023:
024: /**
025: * Renders out a series of rows within the table.
026: */
027: public class GridRows {
028: /**
029: * Parameter used to set the CSS class for each row (each <tr> element) within the
030: * <tbody>). This is not cached, so it will be recomputed for each row.
031: */
032: @Parameter(cache=false)
033: private String _rowClass;
034:
035: /** Object that provides access to the bean and data models used to render the Grid. */
036: @Parameter(value="componentResources.container")
037: private GridModelProvider _provider;
038:
039: /** Number of rows displayed on each page. Long result sets are split across multiple pages. */
040: @Parameter(required=true)
041: private int _rowsPerPage;
042:
043: /** The current page number within the available pages (indexed from 1). */
044: @Parameter(required=true)
045: private int _currentPage;
046:
047: /**
048: * The current row being rendered, this is primarily an output parameter used to allow the Grid,
049: * and the Grid's container, to know what object is being rendered.
050: */
051: @Parameter(required=true)
052: private Object _row;
053:
054: private int _startRow;
055:
056: private int _endRow;
057:
058: private int _rowIndex;
059:
060: private String _propertyName;
061:
062: private PropertyModel _columnModel;
063:
064: public String getRowClass() {
065: return _rowClass;
066: }
067:
068: public String getCellClass() {
069: String id = _provider.getDataModel().get(_propertyName).getId();
070:
071: return id + "-cell";
072: }
073:
074: void setupRender() {
075: GridDataSource dataSource = _provider.getDataSource();
076:
077: int availableRows = dataSource.getAvailableRows();
078:
079: int maxPages = ((availableRows - 1) / _rowsPerPage) + 1;
080:
081: // This can sometimes happen when the number of items shifts between requests.
082:
083: if (_currentPage > maxPages)
084: _currentPage = maxPages;
085:
086: _startRow = (_currentPage - 1) * _rowsPerPage;
087: _endRow = Math.min(availableRows - 1, _startRow + _rowsPerPage
088: - 1);
089:
090: _rowIndex = _startRow;
091: }
092:
093: void beginRender() {
094: _row = _provider.getDataSource().getRowValue(_rowIndex);
095: }
096:
097: boolean afterRender() {
098: _rowIndex++;
099:
100: return _rowIndex > _endRow;
101: }
102:
103: public List<String> getPropertyNames() {
104: return _provider.getDataModel().getPropertyNames();
105: }
106:
107: public String getPropertyName() {
108: return _propertyName;
109: }
110:
111: public void setPropertyName(String propertyName) {
112: _propertyName = propertyName;
113:
114: _columnModel = _provider.getDataModel().get(propertyName);
115: }
116:
117: public Object getRow() {
118: return _row;
119: }
120:
121: public PropertyModel getColumnModel() {
122: return _columnModel;
123: }
124: }
|