001// Copyright 2004, 2005 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
015package org.apache.tapestry.contrib.table.components;
016
017import java.util.Iterator;
018
019import org.apache.tapestry.IBinding;
020import org.apache.tapestry.IMarkupWriter;
021import org.apache.tapestry.IRequestCycle;
022import org.apache.tapestry.contrib.table.model.IFullTableModel;
023import org.apache.tapestry.contrib.table.model.ITableModel;
024import org.apache.tapestry.contrib.table.model.ITableRowSource;
025
026/**
027 * A low level Table component that generates the rows of the current page in the table.
028 * This component must be wrapped by {@link org.apache.tapestry.contrib.table.components.TableView}.
029 * 
030 * <p>
031 * The component iterates over the rows of the current page in the table. 
032 * The rows are wrapped in 'tr' tags by default. 
033 * You can define columns manually within, or
034 * you can use {@link org.apache.tapestry.contrib.table.components.TableValues} 
035 * to generate the columns automatically.
036 * 
037 * <p> 
038 * Please see the Component Reference for details on how to use this component. 
039 * 
040 *  [<a href="../../../../../../../ComponentReference/contrib.TableRows.html">Component Reference</a>]
041 * 
042 * @author mindbridge
043 *
044 */
045public abstract class TableRows extends AbstractTableViewComponent implements ITableRowSource
046{
047
048        // Parameters
049        public abstract Object getFullSourceParameter();
050        
051    // Transient
052    private Object m_objTableRow = null;
053    private int m_nTableIndex;
054
055    /**
056     * Returns the currently rendered table row.
057     * You can call this method to obtain the current row.
058     *  
059     * @return Object the current table row
060     */
061    public Object getTableRow()
062    {
063        return m_objTableRow;
064    }
065
066    /**
067     * Sets the currently rendered table row. 
068     * This method is for internal use only.
069     * 
070     * @param tableRow The current table row
071     */
072    public void setTableRow(Object tableRow)
073    {
074        m_objTableRow = tableRow;
075
076        IBinding objRowBinding = getBinding("row");
077        if (objRowBinding != null)
078            objRowBinding.setObject(tableRow);
079    }
080
081    /**
082     * Returns the index of the currently rendered table row.
083     * You can call this method to obtain the index of the current row.
084     *  
085     * @return int the current table index
086     */
087    public int getTableIndex()
088    {
089        return m_nTableIndex;
090    }
091
092    /**
093     * Sets the index of the currently rendered table row. 
094     * This method is for internal use only.
095     * 
096     * @param tableIndex The index of the current table row
097     */
098    public void setTableIndex(int tableIndex)
099    {
100        m_nTableIndex = tableIndex;
101
102        IBinding objIndexBinding = getBinding("index");
103        if (objIndexBinding != null)
104            objIndexBinding.setObject(new Integer(tableIndex));
105    }
106
107    /**
108     * Get the list of all table rows to be displayed on this page.
109     * 
110     * @return an iterator of all table rows
111     */
112    public Iterator getTableRowsIterator()
113    {
114        ITableModel objTableModel = getTableModelSource().getTableModel();
115        return objTableModel.getCurrentPageRows();
116    }
117
118    public Object getFullSource()
119    {
120        ITableModel objTableModel = getTableModelSource().getTableModel();
121        if (objTableModel instanceof IFullTableModel)
122                return ((IFullTableModel) objTableModel).getRows();
123        return getFullSourceParameter();
124    }
125    
126    /**
127     * @see org.apache.tapestry.BaseComponent#renderComponent(IMarkupWriter, IRequestCycle)
128     */
129    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
130    {
131        Object objOldValue = cycle.getAttribute(ITableRowSource.TABLE_ROW_SOURCE_ATTRIBUTE);
132        cycle.setAttribute(ITableRowSource.TABLE_ROW_SOURCE_ATTRIBUTE, this);
133
134        super.renderComponent(writer, cycle);
135
136        cycle.setAttribute(ITableRowSource.TABLE_ROW_SOURCE_ATTRIBUTE, objOldValue);
137
138        // set the current row to null when the component is not active
139        m_objTableRow = null;
140    }
141
142    
143}