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.inserted;
016    
017    import org.apache.tapestry.BaseComponent;
018    import org.apache.tapestry.IAsset;
019    import org.apache.tapestry.IRequestCycle;
020    import org.apache.tapestry.contrib.table.components.TableColumns;
021    import org.apache.tapestry.contrib.table.model.*;
022    import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumn;
023    
024    /**
025     * A component that renders the default column header in a form. If the current
026     * column is sortable, it renders the header as a link. Clicking on the link
027     * causes the table to be sorted on that column. Clicking on the link again
028     * causes the sorting order to be reversed. This component renders links that
029     * cause the form to be submitted. This ensures that the updated data in the
030     * other form fields is preserved.
031     * 
032     * @author mindbridge
033     */
034    public abstract class SimpleTableColumnFormComponent extends BaseComponent
035            implements ITableRendererListener
036    {
037    
038        public abstract ITableColumn getTableColumn();
039    
040        public abstract void setTableColumn(ITableColumn objColumn);
041    
042        public abstract ITableModelSource getTableModelSource();
043    
044        public abstract void setTableModelSource(ITableModelSource objSource);
045    
046        public abstract String getSelectedColumnName();
047    
048        /**
049         * @see org.apache.tapestry.contrib.table.model.ITableRendererListener#initializeRenderer(IRequestCycle,
050         *      ITableModelSource, ITableColumn, Object)
051         */
052        public void initializeRenderer(IRequestCycle objCycle, ITableModelSource objSource,
053                                       ITableColumn objColumn, Object objRow)
054        {
055            setTableModelSource(objSource);
056            setTableColumn(objColumn);
057        }
058    
059        public ITableModel getTableModel()
060        {
061            return getTableModelSource().getTableModel();
062        }
063    
064        public boolean getColumnSorted()
065        {
066            return getTableColumn().getSortable();
067        }
068    
069        public String getDisplayName()
070        {
071            ITableColumn objColumn = getTableColumn();
072    
073            if (objColumn instanceof SimpleTableColumn)
074            {
075                SimpleTableColumn objSimpleColumn = (SimpleTableColumn) objColumn;
076    
077                return objSimpleColumn.getDisplayName();
078            }
079            
080            return objColumn.getColumnName();
081        }
082    
083        public boolean getIsSorted()
084        {
085            ITableSortingState objSortingState = getTableModel().getSortingState();
086            String strSortColumn = objSortingState.getSortColumn();
087            return getTableColumn().getColumnName().equals(strSortColumn);
088        }
089    
090        public IAsset getSortImage()
091        {
092            IAsset objImageAsset;
093    
094            IRequestCycle objCycle = getPage().getRequestCycle();
095            ITableSortingState objSortingState = getTableModel().getSortingState();
096            
097            if (objSortingState.getSortOrder() == ITableSortingState.SORT_ASCENDING)
098            {
099                objImageAsset = (IAsset) objCycle.getAttribute(TableColumns.TABLE_COLUMN_ARROW_UP_ATTRIBUTE);
100    
101                if (objImageAsset == null)
102                    objImageAsset = getAsset("sortUp");
103            }
104            else
105            {
106                objImageAsset = (IAsset) objCycle.getAttribute(TableColumns.TABLE_COLUMN_ARROW_DOWN_ATTRIBUTE);
107    
108                if (objImageAsset == null)
109                    objImageAsset = getAsset("sortDown");
110            }
111    
112            return objImageAsset;
113        }
114    
115        public void columnSelected(IRequestCycle objCycle)
116        {
117            String strColumnName = getSelectedColumnName();
118            ITableModelSource objSource = getTableModelSource();
119            
120            objSource.storeTableAction(new TableActionColumnSort(strColumnName));
121        }
122    
123    }