View Javadoc

1   /**
2    * Licensed under the Artistic License; you may not use this file
3    * except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://displaytag.sourceforge.net/license.html
7    *
8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10   * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11   */
12  package org.displaytag.model;
13  
14  import java.util.Iterator;
15  import java.util.List;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.displaytag.decorator.TableDecorator;
20  
21  
22  /**
23   * Iterator on table rows.
24   * @author Fabrizio Giustina
25   * @version $Revision: 1081 $ ($Author: fgiust $)
26   */
27  public class RowIterator
28  {
29  
30      /**
31       * logger.
32       */
33      private static Log log = LogFactory.getLog(RowIterator.class);
34  
35      /**
36       * internal iterator for Rows.
37       */
38      private Iterator iterator;
39  
40      /**
41       * row number counter.
42       */
43      private int rowNumber;
44  
45      /**
46       * reference to the table TableDecorator.
47       */
48      private TableDecorator decorator;
49  
50      /**
51       * id inherited from the TableTag (needed only for logging).
52       */
53      private String id;
54  
55      /**
56       * Starting offset for items n the current page. Needed to calculare the index in the original list
57       */
58      private int pageOffset;
59  
60      /**
61       * Constructor for RowIterator.
62       * @param rowList List containing Row objects
63       * @param columnList List containing CellHeader objects
64       * @param tableDecorator TableDecorator
65       * @param offset Starting offset for items n the current page
66       */
67      protected RowIterator(List rowList, List columnList, TableDecorator tableDecorator, int offset)
68      {
69          this.iterator = rowList.iterator();
70          this.rowNumber = 0;
71          this.decorator = tableDecorator;
72          this.pageOffset = offset;
73      }
74  
75      /**
76       * Setter for the tablemodel id.
77       * @param tableId same id of table tag, needed for logging
78       */
79      public void setId(String tableId)
80      {
81          this.id = tableId;
82      }
83  
84      /**
85       * Check if a next row exist.
86       * @return boolean true if a new row
87       */
88      public boolean hasNext()
89      {
90          return this.iterator.hasNext();
91      }
92  
93      public int getPageOffset()
94      {
95          return this.pageOffset;
96      }
97  
98      /**
99       * Returns the next row object.
100      * @return Row
101      */
102     public Row next()
103     {
104 
105         int currentRowNumber = this.rowNumber++;
106 
107         if (log.isDebugEnabled())
108         {
109             log.debug("[" + this.id + "] rowIterator.next() row number=" + currentRowNumber);
110         }
111 
112         Object object = this.iterator.next();
113 
114         Row row = (Row) object;
115 
116         row.setRowNumber(currentRowNumber);
117 
118         if (this.decorator != null)
119         {
120             this.decorator.initRow(row.getObject(), currentRowNumber, currentRowNumber + getPageOffset());
121         }
122 
123         return row;
124 
125     }
126 
127 }