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.IAsset;
020 import org.apache.tapestry.IMarkupWriter;
021 import org.apache.tapestry.IRender;
022 import org.apache.tapestry.IRequestCycle;
023 import org.apache.tapestry.contrib.table.model.ITableColumn;
024 import org.apache.tapestry.contrib.table.model.ITableColumnModel;
025
026 /**
027 * A low level Table component that renders the column headers in the table. This component must be
028 * wrapped by {@link org.apache.tapestry.contrib.table.components.TableView}.
029 * <p>
030 * The component iterates over all column objects in the
031 * {@link org.apache.tapestry.contrib.table.model.ITableColumnModel}and renders a header for each
032 * one of them using the renderer provided by the getColumnRender() method in
033 * {@link org.apache.tapestry.contrib.table.model.ITableColumn}. The headers are wrapped in 'th'
034 * tags by default.
035 * <p>
036 * Please see the Component Reference for details on how to use this component. [ <a
037 * href="../../../../../../../ComponentReference/contrib.TableColumns.html">Component Reference
038 * </a>]
039 *
040 * @author mindbridge
041 */
042 public abstract class TableColumns extends AbstractTableViewComponent
043 {
044 public static final String TABLE_COLUMN_ARROW_UP_ATTRIBUTE = "org.apache.tapestry.contrib.table.components.TableColumns.arrowUp";
045
046 public static final String TABLE_COLUMN_ARROW_DOWN_ATTRIBUTE = "org.apache.tapestry.contrib.table.components.TableColumns.arrowDown";
047
048 public static final String TABLE_COLUMN_CSS_CLASS_SUFFIX = "ColumnHeader";
049
050 public abstract IAsset getArrowDownAsset();
051
052 public abstract IAsset getArrowUpAsset();
053
054 public abstract void setColumn(ITableColumn column);
055
056 // Transient
057 private ITableColumn m_objTableColumn = null;
058
059 /**
060 * Returns the currently rendered table column. You can call this method to obtain the current
061 * column.
062 *
063 * @return ITableColumn the current table column
064 */
065 public ITableColumn getTableColumn()
066 {
067 return m_objTableColumn;
068 }
069
070 /**
071 * Sets the currently rendered table column. This method is for internal use only.
072 *
073 * @param tableColumn
074 * The current table column
075 */
076 public void setTableColumn(ITableColumn tableColumn)
077 {
078 m_objTableColumn = tableColumn;
079
080 if (isParameterBound("column"))
081 setColumn(tableColumn);
082 }
083
084 /**
085 * Get the list of all table columns to be displayed.
086 *
087 * @return an iterator of all table columns
088 */
089 public Iterator getTableColumnIterator()
090 {
091 ITableColumnModel objColumnModel = getTableModelSource().getTableModel().getColumnModel();
092 return objColumnModel.getColumns();
093 }
094
095 /**
096 * Returns the renderer to be used to generate the header of the current column
097 *
098 * @return the header renderer of the current column
099 */
100 public IRender getTableColumnRenderer()
101 {
102 return getTableColumn().getColumnRenderer(
103 getPage().getRequestCycle(),
104 getTableModelSource());
105 }
106
107 public abstract String getColumnClassParameter();
108
109 /**
110 * Returns the CSS class of the generated table cell. It uses the class parameter if it has been
111 * bound, or the default value of "[column name]ColumnHeader" otherwise.
112 *
113 * @return the CSS class of the cell
114 */
115 public String getColumnClass()
116 {
117 if (isParameterBound("class"))
118 return getColumnClassParameter();
119
120 return getTableColumn().getColumnName() + TABLE_COLUMN_CSS_CLASS_SUFFIX;
121 }
122
123 /**
124 * @see org.apache.tapestry.BaseComponent#renderComponent(IMarkupWriter, IRequestCycle)
125 */
126 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
127 {
128 Object oldValueUp = cycle.getAttribute(TABLE_COLUMN_ARROW_UP_ATTRIBUTE);
129 Object oldValueDown = cycle.getAttribute(TABLE_COLUMN_ARROW_DOWN_ATTRIBUTE);
130
131 try
132 {
133 cycle.setAttribute(TABLE_COLUMN_ARROW_UP_ATTRIBUTE, getArrowUpAsset());
134 cycle.setAttribute(TABLE_COLUMN_ARROW_DOWN_ATTRIBUTE, getArrowDownAsset());
135
136 super.renderComponent(writer, cycle);
137 }
138 finally
139 {
140 cycle.setAttribute(TABLE_COLUMN_ARROW_UP_ATTRIBUTE, oldValueUp);
141 cycle.setAttribute(TABLE_COLUMN_ARROW_DOWN_ATTRIBUTE, oldValueDown);
142
143 // set the current column to null when the component is not active
144 m_objTableColumn = null;
145 }
146 }
147
148 }