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    
015    package org.apache.tapestry.contrib.table.components;
016    
017    import java.util.Iterator;
018    
019    import org.apache.tapestry.IBinding;
020    import org.apache.tapestry.IMarkupWriter;
021    import org.apache.tapestry.IRequestCycle;
022    import org.apache.tapestry.contrib.table.model.IFullTableModel;
023    import org.apache.tapestry.contrib.table.model.ITableModel;
024    import org.apache.tapestry.contrib.table.model.ITableRowSource;
025    
026    /**
027     * A low level Table component that generates the rows of the current page in
028     * the table. This component must be wrapped by
029     * {@link org.apache.tapestry.contrib.table.components.TableView}.
030     * <p>
031     * The component iterates over the rows of the current page in the table. The
032     * rows are wrapped in 'tr' tags by default. You can define columns manually
033     * within, or you can use
034     * {@link org.apache.tapestry.contrib.table.components.TableValues} to generate
035     * the columns automatically.
036     * <p>
037     * Please see the Component Reference for details on how to use this component. [<a
038     * href="../../../../../../../ComponentReference/contrib.TableRows.html">Component
039     * Reference</a>]
040     * 
041     * @author mindbridge
042     */
043    public abstract class TableRows extends AbstractTableViewComponent implements
044            ITableRowSource
045    {
046    
047        // Transient
048        private Object m_objTableRow = null;
049        private int m_nTableIndex;
050        
051        // Parameters
052        public abstract Object getFullSourceParameter();
053    
054        /**
055         * Returns the currently rendered table row. You can call this method to
056         * obtain the current row.
057         * 
058         * @return Object the current table row
059         */
060        public Object getTableRow()
061        {
062            return m_objTableRow;
063        }
064    
065        /**
066         * Sets the currently rendered table row. This method is for internal use
067         * only.
068         * 
069         * @param tableRow
070         *            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) objRowBinding.setObject(tableRow);
078        }
079    
080        /**
081         * Returns the index of the currently rendered table row. You can call this
082         * method to obtain the index of the current row.
083         * 
084         * @return int the current table index
085         */
086        public int getTableIndex()
087        {
088            return m_nTableIndex;
089        }
090    
091        /**
092         * Sets the index of the currently rendered table row. This method is for
093         * internal use only.
094         * 
095         * @param tableIndex
096         *            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,
128         *      IRequestCycle)
129         */
130        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
131        {
132            Object objOldValue = cycle
133                    .getAttribute(ITableRowSource.TABLE_ROW_SOURCE_ATTRIBUTE);
134            cycle.setAttribute(ITableRowSource.TABLE_ROW_SOURCE_ATTRIBUTE, this);
135    
136            super.renderComponent(writer, cycle);
137    
138            cycle.setAttribute(ITableRowSource.TABLE_ROW_SOURCE_ATTRIBUTE,
139                    objOldValue);
140    
141            // set the current row to null when the component is not active
142            m_objTableRow = null;
143        }
144    
145    }