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.IRequestCycle;
019    import org.apache.tapestry.Tapestry;
020    import org.apache.tapestry.contrib.table.components.Table;
021    import org.apache.tapestry.contrib.table.model.ITableColumn;
022    import org.apache.tapestry.contrib.table.model.ITableModelSource;
023    import org.apache.tapestry.contrib.table.model.ITableRendererListener;
024    import org.apache.tapestry.event.PageDetachListener;
025    import org.apache.tapestry.event.PageEvent;
026    import org.apache.tapestry.util.ComponentAddress;
027    
028    /**
029     * A component that renders a sorting link - to be used with contrib:Table when 
030     * customizing a column's header.
031     *
032     *
033     * @author andyhot
034     */
035    public abstract class SimpleTableColumnSortLink extends BaseComponent
036            implements ITableRendererListener, PageDetachListener
037    {
038        // transient
039        private ITableColumn m_objColumn;
040        private ITableModelSource m_objModelSource;
041    
042        public SimpleTableColumnSortLink()
043        {
044            init();
045        }
046    
047        public abstract Table getTable();
048    
049        /**
050         * @see org.apache.tapestry.event.PageDetachListener#pageDetached(PageEvent)
051         */
052        public void pageDetached(PageEvent arg0)
053        {
054            init();
055        }
056    
057        private void init()
058        {
059            m_objColumn = null;
060            m_objModelSource = null;
061        }
062    
063        public void prepareForRender(IRequestCycle cycle)
064        {
065            if (getTable()==null)
066                throw Tapestry.createRequiredParameterException(this, "table");
067            
068            m_objModelSource = getTable();
069            m_objColumn = getTable().getTableColumn();
070        }
071    
072        /**
073         * @see org.apache.tapestry.contrib.table.model.ITableRendererListener#initializeRenderer(IRequestCycle,
074         *      ITableModelSource, ITableColumn, Object)
075         */
076        public void initializeRenderer(IRequestCycle objCycle, ITableModelSource objSource,
077                                       ITableColumn objColumn, Object objRow)
078        {
079            m_objModelSource = objSource;
080            m_objColumn = objColumn;
081        }
082    
083        public Object[] getColumnSelectedParameters()
084        {
085            return new Object[] {
086                    new ComponentAddress(m_objModelSource),
087                    m_objColumn.getColumnName()
088            };
089        }
090    
091        public void columnSelected(IRequestCycle objCycle)
092        {
093            Object[] arrArgs = objCycle.getListenerParameters();
094            ComponentAddress objAddr = (ComponentAddress) arrArgs[0];
095            String strColumnName = (String) arrArgs[1];
096    
097            ITableModelSource objSource = (ITableModelSource) objAddr.findComponent(objCycle);
098            
099            objSource.storeTableAction(new TableActionColumnSort(strColumnName));
100        }
101    }