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.model.common;
016
017 import java.io.Serializable;
018
019 import org.apache.tapestry.contrib.table.model.ITableModel;
020 import org.apache.tapestry.contrib.table.model.ITablePagingState;
021 import org.apache.tapestry.contrib.table.model.ITableSortingState;
022 import org.apache.tapestry.contrib.table.model.simple.SimpleTableState;
023
024 /**
025 * A base table model that implements the handling of the model state.
026 * Used by most standard ITableModel implementations.
027 *
028 * @author mindbridge
029 */
030 public abstract class AbstractTableModel implements ITableModel, Serializable
031 {
032 private SimpleTableState m_objTableState;
033
034 protected AbstractTableModel()
035 {
036 this(new SimpleTableState());
037 }
038
039 protected AbstractTableModel(SimpleTableState objTableState)
040 {
041 m_objTableState = objTableState;
042 }
043
044 /**
045 * @see org.apache.tapestry.contrib.table.model.ITableModel#getPagingState()
046 */
047 public ITablePagingState getPagingState()
048 {
049 return getState().getPagingState();
050 }
051
052 /**
053 * @see org.apache.tapestry.contrib.table.model.ITableModel#getSortingState()
054 */
055 public ITableSortingState getSortingState()
056 {
057 return getState().getSortingState();
058 }
059
060 /**
061 * Returns the tableState.
062 * @return SimpleTableState
063 */
064 public SimpleTableState getState()
065 {
066 return m_objTableState;
067 }
068
069 protected abstract int getRowCount();
070
071 public int getPageCount()
072 {
073 int nRowCount = getRowCount();
074 if (nRowCount == 0)
075 return 1;
076
077 int nPageSize = getPagingState().getPageSize();
078 if (nPageSize <= 0)
079 return 1;
080
081 return (nRowCount - 1) / nPageSize + 1;
082 }
083
084 }