1 15 package org.apache.tapestry.contrib.table.components; 16 17 import java.io.Serializable ; 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.tapestry.BaseComponent; 25 import org.apache.tapestry.IComponent; 26 import org.apache.tapestry.IMarkupWriter; 27 import org.apache.tapestry.IRequestCycle; 28 import org.apache.tapestry.contrib.table.model.IAdvancedTableColumnSource; 29 import org.apache.tapestry.contrib.table.model.IBasicTableModel; 30 import org.apache.tapestry.contrib.table.model.ITableColumn; 31 import org.apache.tapestry.contrib.table.model.ITableColumnModel; 32 import org.apache.tapestry.contrib.table.model.ITableDataModel; 33 import org.apache.tapestry.contrib.table.model.ITableModel; 34 import org.apache.tapestry.contrib.table.model.ITableModelSource; 35 import org.apache.tapestry.contrib.table.model.ITablePagingState; 36 import org.apache.tapestry.contrib.table.model.ITableSessionStateManager; 37 import org.apache.tapestry.contrib.table.model.ITableSessionStoreManager; 38 import org.apache.tapestry.contrib.table.model.common.BasicTableModelWrap; 39 import org.apache.tapestry.contrib.table.model.simple.SimpleListTableDataModel; 40 import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumnModel; 41 import org.apache.tapestry.contrib.table.model.simple.SimpleTableModel; 42 import org.apache.tapestry.contrib.table.model.simple.SimpleTableState; 43 import org.apache.tapestry.event.PageBeginRenderListener; 44 import org.apache.tapestry.event.PageDetachListener; 45 import org.apache.tapestry.event.PageEvent; 46 47 97 public abstract class TableView extends BaseComponent implements PageDetachListener, 98 PageBeginRenderListener, ITableModelSource 99 { 100 101 public abstract TableColumnModelSource getModelSource(); 102 103 104 public abstract IAdvancedTableColumnSource getColumnSource(); 105 106 private ITableSessionStateManager m_objDefaultSessionStateManager = null; 108 109 private ITableColumnModel m_objColumnModel = null; 110 111 private ITableModel m_objTableModel; 113 114 private ITableModel m_objCachedTableModelValue; 115 116 public abstract ITableModel getTableModelValue(); 118 119 public abstract Object getSource(); 120 121 public abstract Object getColumns(); 122 123 public abstract int getInitialPage(); 124 125 public abstract String getInitialSortColumn(); 126 127 public abstract boolean getInitialSortOrder(); 128 129 public abstract ITableSessionStateManager getTableSessionStateManager(); 130 131 public abstract ITableSessionStoreManager getTableSessionStoreManager(); 132 133 public abstract IComponent getColumnSettingsContainer(); 134 135 public abstract int getPageSize(); 136 137 public abstract String getPersist(); 138 139 public abstract Serializable getSessionState(); 141 142 public abstract void setSessionState(Serializable sessionState); 143 144 public abstract Serializable getClientState(); 145 146 public abstract void setClientState(Serializable sessionState); 147 148 public abstract Serializable getClientAppState(); 149 150 public abstract void setClientAppState(Serializable sessionState); 151 152 155 public TableView() 156 { 157 initialize(); 158 } 159 160 165 public void pageDetached(PageEvent objEvent) 166 { 167 initialize(); 168 } 169 170 173 private void initialize() 174 { 175 m_objTableModel = null; 176 m_objCachedTableModelValue = null; 177 } 178 179 183 public void reset() 184 { 185 initialize(); 186 storeSessionState(null); 187 } 188 189 public ITableModel getCachedTableModelValue() 190 { 191 if (m_objCachedTableModelValue == null) 192 m_objCachedTableModelValue = getTableModelValue(); 193 return m_objCachedTableModelValue; 194 } 195 196 201 public ITableModel getTableModel() 202 { 203 if (m_objTableModel == null) 205 { 206 Serializable objState = loadSessionState(); 207 m_objTableModel = getTableSessionStateManager().recreateTableModel(objState); 208 } 209 210 if (m_objTableModel == null) 212 m_objTableModel = getCachedTableModelValue(); 213 214 if (m_objTableModel == null) 216 m_objTableModel = generateTableModel(null); 217 218 if (m_objTableModel == null) 219 throw new ApplicationRuntimeException(TableMessages.missingTableModel(this)); 220 221 return m_objTableModel; 222 } 223 224 229 protected ITableModel generateTableModel(SimpleTableState objState) 230 { 231 if (objState == null) 233 { 234 objState = new SimpleTableState(); 235 objState.getSortingState().setSortColumn(getInitialSortColumn(), getInitialSortOrder()); 236 objState.getPagingState().setCurrentPage(getInitialPage()); 237 } 238 239 if (isParameterBound("pageSize")) 241 objState.getPagingState().setPageSize(getPageSize()); 242 243 ITableColumnModel objColumnModel = getTableColumnModel(); 245 if (objColumnModel == null) 246 return null; 247 248 Object objSourceValue = getSource(); 249 if (objSourceValue == null) 250 return null; 251 252 if (objSourceValue instanceof IBasicTableModel) 255 return new BasicTableModelWrap((IBasicTableModel) objSourceValue, objColumnModel, 256 objState); 257 258 ITableDataModel objDataModel = null; 260 if (objSourceValue instanceof Object []) 261 objDataModel = new SimpleListTableDataModel((Object []) objSourceValue); 262 else if (objSourceValue instanceof List ) 263 objDataModel = new SimpleListTableDataModel((List ) objSourceValue); 264 else if (objSourceValue instanceof Collection ) 265 objDataModel = new SimpleListTableDataModel((Collection ) objSourceValue); 266 else if (objSourceValue instanceof Iterator ) 267 objDataModel = new SimpleListTableDataModel((Iterator ) objSourceValue); 268 269 if (objDataModel == null) 270 throw new ApplicationRuntimeException(TableMessages.invalidTableSource( 271 this, 272 objSourceValue)); 273 274 return new SimpleTableModel(objDataModel, objColumnModel, objState); 275 } 276 277 284 protected ITableColumnModel getTableColumnModel() 285 { 286 Object objColumns = getColumns(); 287 288 if (objColumns == null) 289 return null; 290 291 if (objColumns instanceof ITableColumnModel) 292 { 293 return (ITableColumnModel) objColumns; 294 } 295 296 if (objColumns instanceof Iterator ) 297 { 298 Iterator objColumnsIterator = (Iterator ) objColumns; 300 List arrColumnsList = new ArrayList (); 301 addAll(arrColumnsList, objColumnsIterator); 302 objColumns = arrColumnsList; 303 } 304 305 if (objColumns instanceof List ) 306 { 307 List arrColumnsList = (List ) objColumns; 309 int nColumnsNumber = arrColumnsList.size(); 310 for (int i = 0; i < nColumnsNumber; i++) 311 { 312 if (!(arrColumnsList.get(i) instanceof ITableColumn)) 313 throw new ApplicationRuntimeException(TableMessages.columnsOnlyPlease(this)); 314 } 315 return new SimpleTableColumnModel(arrColumnsList); 317 } 318 319 if (objColumns instanceof ITableColumn[]) 320 { 321 return new SimpleTableColumnModel((ITableColumn[]) objColumns); 322 } 323 324 if (objColumns instanceof String ) 325 { 326 String strColumns = (String ) objColumns; 327 if (getBinding("columns").isInvariant()) 328 { 329 if (m_objColumnModel == null) 331 m_objColumnModel = generateTableColumnModel(strColumns); 332 return m_objColumnModel; 333 } 334 335 return generateTableColumnModel(strColumns); 337 } 338 339 throw new ApplicationRuntimeException(TableMessages.invalidTableColumns(this, objColumns)); 340 } 341 342 private void addAll(List arrColumnsList, Iterator objColumnsIterator) 343 { 344 while (objColumnsIterator.hasNext()) 345 arrColumnsList.add(objColumnsIterator.next()); 346 } 347 348 359 protected ITableColumnModel generateTableColumnModel(String strDesc) 360 { 361 IComponent objColumnSettingsContainer = getColumnSettingsContainer(); 362 IAdvancedTableColumnSource objColumnSource = getColumnSource(); 363 364 return getModelSource().generateTableColumnModel(objColumnSource, strDesc, this, objColumnSettingsContainer); 365 } 366 367 373 public ITableSessionStateManager getDefaultTableSessionStateManager() 374 { 375 if (m_objDefaultSessionStateManager == null) 376 m_objDefaultSessionStateManager = new TableViewSessionStateManager(this); 377 return m_objDefaultSessionStateManager; 378 } 379 380 385 public void fireObservedStateChange() 386 { 387 saveSessionState(); 388 } 389 390 396 public void pageBeginRender(PageEvent event) 397 { 398 if (m_objTableModel != null) 403 saveSessionState(); 404 } 405 406 410 protected void saveSessionState() 411 { 412 ITableModel objModel = getTableModel(); 413 Serializable objState = getTableSessionStateManager().getSessionState(objModel); 414 storeSessionState(objState); 415 } 416 417 422 protected Serializable loadSessionState() 423 { 424 ITableSessionStoreManager objManager = getTableSessionStoreManager(); 425 if (objManager != null) 426 return objManager.loadState(getPage().getRequestCycle()); 427 String strPersist = getPersist(); 428 if (strPersist.equals("client") || strPersist.equals("client:page")) 429 return getClientState(); 430 else if (strPersist.equals("client:app")) 431 return getClientAppState(); 432 else 433 return getSessionState(); 434 } 435 436 442 protected void storeSessionState(Serializable objState) 443 { 444 ITableSessionStoreManager objManager = getTableSessionStoreManager(); 445 if (objManager != null) 446 objManager.saveState(getPage().getRequestCycle(), objState); 447 else { 448 String strPersist = getPersist(); 449 if (strPersist.equals("client") || strPersist.equals("client:page")) 450 setClientState(objState); 451 else if (strPersist.equals("client:app")) 452 setClientAppState(objState); 453 else 454 setSessionState(objState); 455 } 456 } 457 458 462 protected void validateValues() 463 { 464 ITableModel objModel = getTableModel(); 465 466 ITablePagingState objPagingState = objModel.getPagingState(); 468 int nCurrentPage = objPagingState.getCurrentPage(); 469 int nPageCount = objModel.getPageCount(); 470 if (nCurrentPage >= nPageCount) 471 { 472 nCurrentPage = nPageCount - 1; 474 objPagingState.setCurrentPage(nCurrentPage); 475 } 476 if (nCurrentPage < 0) 477 { 478 nCurrentPage = 0; 480 objPagingState.setCurrentPage(nCurrentPage); 481 } 482 } 483 484 490 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 491 { 492 Object objOldValue = cycle.getAttribute(ITableModelSource.TABLE_MODEL_SOURCE_ATTRIBUTE); 493 cycle.setAttribute(ITableModelSource.TABLE_MODEL_SOURCE_ATTRIBUTE, this); 494 495 initialize(); 496 validateValues(); 497 super.renderComponent(writer, cycle); 498 499 cycle.setAttribute(ITableModelSource.TABLE_MODEL_SOURCE_ATTRIBUTE, objOldValue); 500 } 501 502 } | Popular Tags |