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