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