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.Tapestry; 021 import org.apache.tapestry.contrib.table.components.Table; 022 import org.apache.tapestry.contrib.table.components.TableColumns; 023 import org.apache.tapestry.contrib.table.model.ITableColumn; 024 import org.apache.tapestry.contrib.table.model.ITableModel; 025 import org.apache.tapestry.contrib.table.model.ITableModelSource; 026 import org.apache.tapestry.contrib.table.model.ITableRendererListener; 027 import org.apache.tapestry.contrib.table.model.ITableSortingState; 028 import org.apache.tapestry.event.PageDetachListener; 029 import org.apache.tapestry.event.PageEvent; 030 031 /** 032 * A component that renders the proper sort image for the current column - to be 033 * used with contrib:Table when customizing a column's header. 034 * 035 * @author Andreas Andreou 036 */ 037 public abstract class SimpleTableColumnSortImage extends BaseComponent 038 implements PageDetachListener, ITableRendererListener 039 { 040 // transient 041 private ITableModelSource m_objModelSource; 042 private ITableColumn m_objColumn; 043 044 public SimpleTableColumnSortImage() 045 { 046 init(); 047 } 048 049 public abstract Table getTable(); 050 051 /** 052 * @see org.apache.tapestry.event.PageDetachListener#pageDetached(PageEvent) 053 */ 054 public void pageDetached(PageEvent arg0) 055 { 056 init(); 057 } 058 059 private void init() 060 { 061 m_objModelSource = null; 062 m_objColumn = null; 063 } 064 065 /** 066 * @see org.apache.tapestry.contrib.table.model.ITableRendererListener#initializeRenderer(IRequestCycle, 067 * ITableModelSource, ITableColumn, Object) 068 */ 069 public void initializeRenderer(IRequestCycle objCycle, 070 ITableModelSource objSource, ITableColumn objColumn, Object objRow) 071 { 072 m_objModelSource = objSource; 073 m_objColumn = objColumn; 074 } 075 076 public void prepareForRender(IRequestCycle cycle) 077 { 078 if (getTable()==null) 079 throw Tapestry.createRequiredParameterException(this, "table"); 080 081 m_objModelSource = getTable(); 082 m_objColumn = getTable().getTableColumn(); 083 084 } 085 086 087 public ITableModel getTableModel() 088 { 089 return m_objModelSource.getTableModel(); 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 boolean getIsSorted() 115 { 116 ITableSortingState objSortingState = getTableModel().getSortingState(); 117 String strSortColumn = objSortingState.getSortColumn(); 118 return m_objColumn.getColumnName().equals(strSortColumn); 119 } 120 }