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    import org.apache.tapestry.event.PageDetachListener;
024    import org.apache.tapestry.event.PageEvent;
025    import org.apache.tapestry.util.ComponentAddress;
026    
027    /**
028     * A component that renders the default column header. If the current column is
029     * sortable, it renders the header as a link. Clicking on the link causes the
030     * table to be sorted on that column. Clicking on the link again causes the
031     * sorting order to be reversed.
032     *
033     * @author mindbridge
034     */
035    public abstract class SimpleTableColumnComponent extends BaseComponent
036            implements ITableRendererListener, PageDetachListener
037    {
038    
039        // transient
040        private ITableColumn m_objColumn;
041        private ITableModelSource m_objModelSource;
042    
043        public SimpleTableColumnComponent()
044        {
045            init();
046        }
047    
048        /**
049         * @see org.apache.tapestry.event.PageDetachListener#pageDetached(PageEvent)
050         */
051        public void pageDetached(PageEvent arg0)
052        {
053            init();
054        }
055    
056        private void init()
057        {
058            m_objColumn = null;
059            m_objModelSource = null;
060        }
061    
062        /**
063         * @see org.apache.tapestry.contrib.table.model.ITableRendererListener#initializeRenderer(IRequestCycle,
064         *      ITableModelSource, ITableColumn, Object)
065         */
066        public void initializeRenderer(IRequestCycle objCycle, ITableModelSource objSource,
067                                       ITableColumn objColumn, Object objRow)
068        {
069            m_objModelSource = objSource;
070            m_objColumn = objColumn;
071        }
072    
073        public ITableModel getTableModel()
074        {
075            return m_objModelSource.getTableModel();
076        }
077    
078        public boolean getColumnSorted()
079        {
080            return m_objColumn.getSortable();
081        }
082    
083        public String getDisplayName()
084        {
085            if (m_objColumn instanceof SimpleTableColumn)
086            {
087                SimpleTableColumn objSimpleColumn = (SimpleTableColumn) m_objColumn;
088    
089                return objSimpleColumn.getDisplayName();
090            }
091            
092            return m_objColumn.getColumnName();
093        }
094    
095        public boolean getIsSorted()
096        {
097            ITableSortingState objSortingState = getTableModel().getSortingState();
098            String strSortColumn = objSortingState.getSortColumn();
099            
100            return m_objColumn.getColumnName().equals(strSortColumn);
101        }
102    
103        public IAsset getSortImage()
104        {
105            IAsset objImageAsset;
106    
107            IRequestCycle objCycle = getPage().getRequestCycle();
108            ITableSortingState objSortingState = getTableModel().getSortingState();
109            
110            if (objSortingState.getSortOrder() == ITableSortingState.SORT_ASCENDING)
111            {
112                objImageAsset = (IAsset) objCycle.getAttribute(TableColumns.TABLE_COLUMN_ARROW_UP_ATTRIBUTE);
113                
114                if (objImageAsset == null)
115                    objImageAsset = getAsset("sortUp");
116            }
117            else
118            {
119                objImageAsset = (IAsset) objCycle.getAttribute(TableColumns.TABLE_COLUMN_ARROW_DOWN_ATTRIBUTE);
120    
121                if (objImageAsset == null)
122                    objImageAsset = getAsset("sortDown");
123            }
124    
125            return objImageAsset;
126        }
127    
128        public Object[] getColumnSelectedParameters()
129        {
130            return new Object[] {
131                    new ComponentAddress(m_objModelSource),
132                    m_objColumn.getColumnName()
133            };
134        }
135    
136        public void columnSelected(IRequestCycle objCycle)
137        {
138            Object[] arrArgs = objCycle.getListenerParameters();
139            ComponentAddress objAddr = (ComponentAddress) arrArgs[0];
140            String strColumnName = (String) arrArgs[1];
141    
142            ITableModelSource objSource = (ITableModelSource) objAddr
143                    .findComponent(objCycle);
144            objSource.storeTableAction(new TableActionColumnSort(strColumnName));
145        }
146    
147    }