1 15 package org.apache.tapestry.contrib.table.components.inserted; 16 17 import org.apache.tapestry.BaseComponent; 18 import org.apache.tapestry.IAsset; 19 import org.apache.tapestry.IRequestCycle; 20 import org.apache.tapestry.contrib.table.components.TableColumns; 21 import org.apache.tapestry.contrib.table.model.ITableColumn; 22 import org.apache.tapestry.contrib.table.model.ITableModel; 23 import org.apache.tapestry.contrib.table.model.ITableModelSource; 24 import org.apache.tapestry.contrib.table.model.ITableRendererListener; 25 import org.apache.tapestry.contrib.table.model.ITableSortingState; 26 import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumn; 27 import org.apache.tapestry.event.PageDetachListener; 28 import org.apache.tapestry.event.PageEvent; 29 import org.apache.tapestry.util.ComponentAddress; 30 31 40 public abstract class SimpleTableColumnComponent 41 extends BaseComponent 42 implements ITableRendererListener, PageDetachListener 43 { 44 private ITableColumn m_objColumn; 46 private ITableModelSource m_objModelSource; 47 48 public SimpleTableColumnComponent() 49 { 50 init(); 51 } 52 53 56 public void pageDetached(PageEvent arg0) 57 { 58 init(); 59 } 60 61 private void init() 62 { 63 m_objColumn = null; 64 m_objModelSource = null; 65 } 66 67 68 71 public void initializeRenderer( 72 IRequestCycle objCycle, 73 ITableModelSource objSource, 74 ITableColumn objColumn, 75 Object objRow) 76 { 77 m_objModelSource = objSource; 78 m_objColumn = objColumn; 79 } 80 81 public ITableModel getTableModel() 82 { 83 return m_objModelSource.getTableModel(); 84 } 85 86 public boolean getColumnSorted() 87 { 88 return m_objColumn.getSortable(); 89 } 90 91 public String getDisplayName() 92 { 93 if (m_objColumn instanceof SimpleTableColumn) { 94 SimpleTableColumn objSimpleColumn = (SimpleTableColumn) m_objColumn; 95 return objSimpleColumn.getDisplayName(); 96 } 97 return m_objColumn.getColumnName(); 98 } 99 100 public boolean getIsSorted() 101 { 102 ITableSortingState objSortingState = getTableModel().getSortingState(); 103 String strSortColumn = objSortingState.getSortColumn(); 104 return m_objColumn.getColumnName().equals(strSortColumn); 105 } 106 107 public IAsset getSortImage() 108 { 109 IAsset objImageAsset; 110 111 IRequestCycle objCycle = getPage().getRequestCycle(); 112 ITableSortingState objSortingState = getTableModel().getSortingState(); 113 if (objSortingState.getSortOrder() 114 == ITableSortingState.SORT_ASCENDING) 115 { 116 objImageAsset = 117 (IAsset) objCycle.getAttribute( 118 TableColumns.TABLE_COLUMN_ARROW_UP_ATTRIBUTE); 119 if (objImageAsset == null) 120 objImageAsset = getAsset("sortUp"); 121 } 122 else 123 { 124 objImageAsset = 125 (IAsset) objCycle.getAttribute( 126 TableColumns.TABLE_COLUMN_ARROW_DOWN_ATTRIBUTE); 127 if (objImageAsset == null) 128 objImageAsset = getAsset("sortDown"); 129 } 130 131 return objImageAsset; 132 } 133 134 public Object [] getColumnSelectedParameters() 135 { 136 return new Object [] { 137 new ComponentAddress(m_objModelSource), 138 m_objColumn.getColumnName()}; 139 } 140 141 public void columnSelected(IRequestCycle objCycle) 142 { 143 Object [] arrArgs = objCycle.getListenerParameters(); 144 ComponentAddress objAddr = (ComponentAddress) arrArgs[0]; 145 String strColumnName = (String ) arrArgs[1]; 146 147 ITableModelSource objSource = 148 (ITableModelSource) objAddr.findComponent(objCycle); 149 ITableModel objModel = objSource.getTableModel(); 150 151 ITableSortingState objState = objModel.getSortingState(); 152 if (strColumnName.equals(objState.getSortColumn())) 153 objState.setSortColumn(strColumnName, !objState.getSortOrder()); 154 else 155 objState.setSortColumn( 156 strColumnName, 157 ITableSortingState.SORT_ASCENDING); 158 159 objSource.fireObservedStateChange(); 161 } 162 163 } 164 | Popular Tags |