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.ArrayList;
15  import java.util.List;
16  import java.util.Map;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.builder.ToStringBuilder;
20  import org.apache.commons.lang.builder.ToStringStyle;
21  import org.displaytag.util.HtmlAttributeMap;
22  import org.displaytag.util.MultipleHtmlAttribute;
23  import org.displaytag.util.TagConstants;
24  
25  
26  /**
27   * Holds informations for a table row.
28   * @author Fabrizio Giustina
29   * @version $Revision: 1081 $ ($Author: fgiust $)
30   */
31  public class Row
32  {
33  
34      /**
35       * Object holding values for the current row.
36       */
37      private Object rowObject;
38  
39      /**
40       * List of cell objects.
41       */
42      private List staticCells;
43  
44      /**
45       * Row number.
46       */
47      private int rowNumber;
48  
49      /**
50       * TableModel which the row belongs to.
51       */
52      private TableModel tableModel;
53  
54      /**
55       * Constructor for Row.
56       * @param object Object
57       * @param number int
58       */
59      public Row(Object object, int number)
60      {
61          this.rowObject = object;
62          this.rowNumber = number;
63          this.staticCells = new ArrayList();
64      }
65  
66      /**
67       * Setter for the row number.
68       * @param number row number
69       */
70      public void setRowNumber(int number)
71      {
72          this.rowNumber = number;
73      }
74  
75      /**
76       * Getter for the row number.
77       * @return row number
78       */
79      public int getRowNumber()
80      {
81          return this.rowNumber;
82      }
83  
84      /**
85       * Adds a cell to the row.
86       * @param cell Cell
87       */
88      public void addCell(Cell cell)
89      {
90          this.staticCells.add(cell);
91      }
92  
93      /**
94       * getter for the list of Cell object.
95       * @return List containing Cell objects
96       */
97      public List getCellList()
98      {
99          return this.staticCells;
100     }
101 
102     /**
103      * getter for the object holding values for the current row.
104      * @return Object object holding values for the current row
105      */
106     public Object getObject()
107     {
108         return this.rowObject;
109     }
110 
111     /**
112      * Iterates on columns.
113      * @param columns List
114      * @return ColumnIterator
115      */
116     public ColumnIterator getColumnIterator(List columns)
117     {
118         return new ColumnIterator(columns, this);
119     }
120 
121     /**
122      * Setter for the table model the row belongs to.
123      * @param table TableModel
124      */
125     protected void setParentTable(TableModel table)
126     {
127         this.tableModel = table;
128     }
129 
130     /**
131      * Getter for the table model the row belongs to.
132      * @return TableModel
133      */
134     protected TableModel getParentTable()
135     {
136         return this.tableModel;
137     }
138 
139     /**
140      * Writes the open <tr> tag.
141      * @return String <tr> tag with the appropriate css class attribute
142      */
143     public String getOpenTag()
144     {
145         Map rowAttributes = new HtmlAttributeMap();
146 
147         MultipleHtmlAttribute cssAttribute = new MultipleHtmlAttribute(this.tableModel.getProperties().getCssRow(
148             this.rowNumber));
149 
150         if (this.tableModel.getTableDecorator() != null)
151         {
152             try
153             {
154                 String addStyle = this.tableModel.getTableDecorator().addRowClass();
155 
156                 if (StringUtils.isNotBlank(addStyle))
157                 {
158                     cssAttribute.addAttributeValue(addStyle);
159                 }
160 
161                 String id = this.tableModel.getTableDecorator().addRowId();
162                 if (StringUtils.isNotBlank(id))
163                 {
164                     rowAttributes.put(TagConstants.ATTRIBUTE_ID, id);
165                 }
166             }
167             catch (NoSuchMethodError e)
168             {
169                 // this catch is here to allow decorators compiled with displaytag 1.0 work with 1.1
170                 // since the addRowClass() and addRowId() are new in displaytag 1.1 earlier decorators could throw
171                 // a NoSuchMethodError... be nice with them till a next major release
172             }
173         }
174 
175         if (!cssAttribute.isEmpty())
176         {
177             rowAttributes.put(TagConstants.ATTRIBUTE_CLASS, cssAttribute);
178         }
179 
180         StringBuffer tag = new StringBuffer();
181         tag.append(TagConstants.TAG_OPEN);
182         tag.append(TagConstants.TAGNAME_ROW);
183 
184         tag.append(rowAttributes.toString());
185 
186         tag.append(TagConstants.TAG_CLOSE);
187 
188         return tag.toString();
189     }
190 
191     /**
192      * writes the </tr> tag.
193      * @return String </tr> tag
194      */
195     public String getCloseTag()
196     {
197         return TagConstants.TAG_TR_CLOSE;
198     }
199 
200     /**
201      * @see java.lang.Object#toString()
202      */
203     public String toString()
204     {
205         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) //
206             .append("rowNumber", this.rowNumber) //$NON-NLS-1$
207             .append("rowObject", this.rowObject) //$NON-NLS-1$
208             .toString();
209     }
210 }