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             m_objModelSource = getTable();
068             m_objColumn = getTable().getTableColumn();         
069         }
070    
071        /**
072         * @see org.apache.tapestry.contrib.table.model.ITableRendererListener#initializeRenderer(IRequestCycle,
073         *      ITableModelSource, ITableColumn, Object)
074         */
075        public void initializeRenderer(IRequestCycle objCycle,
076                ITableModelSource objSource, ITableColumn objColumn, Object objRow)
077        {
078            m_objModelSource = objSource;
079            m_objColumn = objColumn;
080        }
081    
082        public Object[] getColumnSelectedParameters()
083        {
084            return new Object[] { new ComponentAddress(m_objModelSource),
085                    m_objColumn.getColumnName() };
086        }
087    
088        public void columnSelected(IRequestCycle objCycle)
089        {
090            Object[] arrArgs = objCycle.getListenerParameters();
091            ComponentAddress objAddr = (ComponentAddress) arrArgs[0];
092            String strColumnName = (String) arrArgs[1];
093    
094            ITableModelSource objSource = (ITableModelSource) objAddr
095                    .findComponent(objCycle);
096            objSource.storeTableAction(new TableActionColumnSort(strColumnName));
097        }
098    
099    }