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.IRender;
020    import org.apache.tapestry.IRequestCycle;
021    import org.apache.tapestry.contrib.table.model.ITableColumn;
022    import org.apache.tapestry.contrib.table.model.ITableColumnModel;
023    
024    /**
025     * A low level Table component that generates the columns in the current row in the table. This
026     * component must be wrapped by {@link org.apache.tapestry.contrib.table.components.TableRows}.
027     * <p>
028     * The component iterates over the columns in the table and automatically renders the column values
029     * for the current table row. The columns are wrapped in 'td' tags by default. <br>
030     * The column values are rendered using the renderer returned by the getValueRenderer() method in
031     * {@link org.apache.tapestry.contrib.table.model.ITableColumn}.
032     * <p>
033     * Please see the Component Reference for details on how to use this component. [ <a
034     * href="../../../../../../../ComponentReference/contrib.TableValues.html">Component Reference </a>]
035     * 
036     * @author mindbridge
037     */
038    public abstract class TableValues extends AbstractTableRowComponent
039    {
040        public static final String TABLE_VALUE_CSS_CLASS_SUFFIX = "ColumnValue";
041    
042        // Transient
043        private ITableColumn m_objTableColumn;
044    
045        /**
046         * Get the list of all table columns to be displayed.
047         * 
048         * @return an iterator of all table columns
049         */
050        public Iterator getTableColumnIterator()
051        {
052            ITableColumnModel objColumnModel = getTableModelSource().getTableModel().getColumnModel();
053            return objColumnModel.getColumns();
054        }
055    
056        /**
057         * Returns the currently rendered table column. You can call this method to obtain the current
058         * column.
059         * 
060         * @return ITableColumn the current table column
061         */
062        public ITableColumn getTableColumn()
063        {
064            return m_objTableColumn;
065        }
066    
067        /**
068         * Sets the currently rendered table column. This method is for internal use only.
069         * 
070         * @param tableColumn
071         *            The current table column
072         */
073        public void setTableColumn(ITableColumn tableColumn)
074        {
075            m_objTableColumn = tableColumn;
076    
077            if (isParameterBound("column"))
078                setColumnParameter(tableColumn);
079        }
080    
081        /**
082         * Returns the renderer to be used to generate the appearance of the current column
083         * 
084         * @return the value renderer of the current column
085         */
086        public IRender getTableValueRenderer()
087        {
088            Object objRow = getTableRowSource().getTableRow();
089            return getTableColumn().getValueRenderer(
090                    getPage().getRequestCycle(),
091                    getTableModelSource(),
092                    objRow);
093        }
094    
095        /**
096         * Returns the CSS class of the generated table cell. It uses the class parameter if it has been
097         * bound, or the default value of "[column name]ColumnValue" otherwise.
098         * 
099         * @return the CSS class of the cell
100         */
101        public String getValueClass()
102        {
103            if (isParameterBound("class"))
104                return getCellClass();
105    
106            return getTableColumn().getColumnName() + TABLE_VALUE_CSS_CLASS_SUFFIX;
107        }
108    
109        /** @since 4.0 */
110        protected void cleanupAfterRender(IRequestCycle cycle)
111        {
112            super.cleanupAfterRender(cycle);
113    
114            m_objTableColumn = null;
115    
116        }
117    
118        /** @since 4.0 */
119    
120        public abstract void setColumnParameter(ITableColumn column);
121    
122        /** @since 4.0 */
123    
124        public abstract String getCellClass();
125    }