KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > components > TableFormRows


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.table.components;
16
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.tapestry.IRequestCycle;
21 import org.apache.tapestry.contrib.table.model.*;
22
23
24 /**
25  * A low level Table component that generates the rows of the current page in the table.
26  *
27  * This component is a variant of {@link org.apache.tapestry.contrib.table.components.TablePages},
28  * but is designed for operation in a form. The displayed rows are stored in
29  * hidden form fields, which are then read during a rewind. This ensures that
30  * the form will rewind in exactly the same was as it was rendered even if the
31  * TableModel has changed and no StaleLink exceptions will occur.
32  *
33  * The component must be wrapped by {@link org.apache.tapestry.contrib.table.components.TableView}.
34  *
35  * <p>
36  * The component iterates over the rows of the current page in the table.
37  * The rows are wrapped in 'tr' tags by default.
38  * You can define columns manually within, or
39  * you can use {@link org.apache.tapestry.contrib.table.components.TableValues}
40  * to generate the columns automatically.
41  * <p>
42  * Please see the Component Reference for details on how to use this component.
43  *
44  * [<a HREF="../../../../../../../ComponentReference/contrib.TableFormRows.html">Component Reference</a>]
45  *
46  * @author mindbridge
47  *
48  */

49 public abstract class TableFormRows extends TableRows
50 {
51     public abstract IPrimaryKeyConvertor getConvertor();
52     public abstract IPrimaryKeyConvertor getConvertorCache();
53     public abstract void setConvertorCache(IPrimaryKeyConvertor convertor);
54     public abstract Map JavaDoc getConvertedValues();
55
56     /**
57      * Returns the PK convertor cached within the realm of the current request cycle.
58      *
59      * @return the cached PK convertor
60      */

61     public IPrimaryKeyConvertor getCachedConvertor()
62     {
63         IPrimaryKeyConvertor objConvertor = getConvertorCache();
64         
65         if (objConvertor == null) {
66             objConvertor = getConvertor();
67             setConvertorCache(objConvertor);
68         }
69         
70         return objConvertor;
71     }
72
73     /**
74      * Get the list of all table rows to be displayed on this page, converted
75      * using the PK.convertor.
76      *
77      * @return an iterator of all converted table rows
78      */

79     public Iterator JavaDoc getConvertedTableRowsIterator()
80     {
81         final Iterator JavaDoc objTableRowsIterator = getTableRowsIterator();
82         final IPrimaryKeyConvertor objConvertor = getCachedConvertor();
83         if (objConvertor == null)
84             return objTableRowsIterator;
85             
86         return new Iterator JavaDoc()
87         {
88             public boolean hasNext()
89             {
90                 return objTableRowsIterator.hasNext();
91             }
92
93             public Object JavaDoc next()
94             {
95                 Object JavaDoc objValue = objTableRowsIterator.next();
96                 Object JavaDoc objPrimaryKey = objConvertor.getPrimaryKey(objValue);
97                 Map JavaDoc mapConvertedValues = getConvertedValues();
98                 mapConvertedValues.put(objPrimaryKey, objValue);
99                 return objPrimaryKey;
100             }
101
102             public void remove()
103             {
104                 objTableRowsIterator.remove();
105             }
106         };
107     }
108
109     /**
110      * Sets the current table row PK and invokes {@link #setTableRow(Object)} as a result.
111      * This method is for internal use only.
112      *
113      * @param objConvertedTableRow The current converted table row (PK)
114      */

115     public void setConvertedTableRow(Object JavaDoc objConvertedTableRow)
116     {
117         Object JavaDoc objValue = objConvertedTableRow;
118
119         IPrimaryKeyConvertor objConvertor = getCachedConvertor();
120         if (objConvertor != null) {
121             IRequestCycle objCycle = getPage().getRequestCycle();
122             if (objCycle.isRewinding()) {
123                 objValue = objConvertor.getValue(objConvertedTableRow);
124             }
125             else {
126                 Map JavaDoc mapConvertedValues = getConvertedValues();
127                 objValue = mapConvertedValues.get(objConvertedTableRow);
128             }
129         }
130
131         setTableRow(objValue);
132     }
133 }
134
Popular Tags