KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > model > simple > SimpleTableModel


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.table.model.simple;
16
17 import java.util.Arrays JavaDoc;
18 import java.util.Comparator JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.tapestry.contrib.table.model.CTableDataModelEvent;
22 import org.apache.tapestry.contrib.table.model.ITableColumn;
23 import org.apache.tapestry.contrib.table.model.ITableColumnModel;
24 import org.apache.tapestry.contrib.table.model.ITableDataModel;
25 import org.apache.tapestry.contrib.table.model.ITableDataModelListener;
26 import org.apache.tapestry.contrib.table.model.ITableSortingState;
27 import org.apache.tapestry.contrib.table.model.common.AbstractTableModel;
28 import org.apache.tapestry.contrib.table.model.common.ArrayIterator;
29 import org.apache.tapestry.contrib.table.model.common.ReverseComparator;
30
31 /**
32  * A simple generic table model implementation.
33  *
34  * @author mindbridge
35  */

36 public class SimpleTableModel extends AbstractTableModel implements ITableDataModelListener
37 {
38     private static final long serialVersionUID = 1L;
39     
40     private ITableDataModel m_objDataModel = null;
41     private Object JavaDoc[] m_arrRows = null;
42     private ITableColumnModel m_objColumnModel = null;
43
44     private SimpleTableSortingState m_objLastSortingState;
45
46     public SimpleTableModel(Object JavaDoc[] arrData, ITableColumn[] arrColumns)
47     {
48         this(new SimpleListTableDataModel(arrData), new SimpleTableColumnModel(arrColumns));
49     }
50
51     public SimpleTableModel(Object JavaDoc[] arrData, ITableColumnModel objColumnModel)
52     {
53         this(new SimpleListTableDataModel(arrData), objColumnModel);
54     }
55
56     public SimpleTableModel(ITableDataModel objDataModel, ITableColumnModel objColumnModel)
57     {
58         this(objDataModel, objColumnModel, new SimpleTableState());
59     }
60
61     public SimpleTableModel(ITableDataModel objDataModel, ITableColumnModel objColumnModel, SimpleTableState objState)
62     {
63         super(objState);
64         
65         m_arrRows = null;
66         m_objColumnModel = objColumnModel;
67         m_objLastSortingState = new SimpleTableSortingState();
68
69         setDataModel(objDataModel);
70     }
71
72     public ITableColumnModel getColumnModel()
73     {
74         return m_objColumnModel;
75     }
76
77     public Iterator JavaDoc getCurrentPageRows()
78     {
79         sortRows();
80
81         int nPageSize = getPagingState().getPageSize();
82         if (nPageSize <= 0)
83             return new ArrayIterator(m_arrRows);
84
85         int nCurrentPage = getPagingState().getCurrentPage();
86         int nFrom = nCurrentPage * nPageSize;
87         int nTo = (nCurrentPage + 1) * nPageSize;
88
89         return new ArrayIterator(m_arrRows, nFrom, nTo);
90     }
91
92     public int getRowCount()
93     {
94         updateRows();
95         return m_arrRows.length;
96     }
97
98     private void updateRows()
99     {
100         // If it is not null, then there is no need to extract the data
101
if (m_arrRows != null)
102             return;
103
104         // Extract the data from the model
105
m_objLastSortingState = new SimpleTableSortingState();
106
107         int nRowCount = m_objDataModel.getRowCount();
108         Object JavaDoc[] arrRows = new Object JavaDoc[nRowCount];
109
110         int i = 0;
111         for (Iterator JavaDoc it = m_objDataModel.getRows(); it.hasNext();)
112             arrRows[i++] = it.next();
113
114         m_arrRows = arrRows;
115     }
116
117     protected void sortRows()
118     {
119         updateRows();
120
121         ITableSortingState objSortingState = getSortingState();
122
123         // see if there is sorting required
124
String JavaDoc strSortColumn = objSortingState.getSortColumn();
125         if (strSortColumn == null)
126             return;
127
128         boolean bSortOrder = objSortingState.getSortOrder();
129
130         // See if the table is already sorted this way. If so, return.
131
if (strSortColumn.equals(m_objLastSortingState.getSortColumn())
132             && m_objLastSortingState.getSortOrder() == bSortOrder)
133             return;
134
135         ITableColumn objColumn = getColumnModel().getColumn(strSortColumn);
136         if (objColumn == null || !objColumn.getSortable())
137             return;
138
139         Comparator JavaDoc objCmp = objColumn.getComparator();
140         if (objCmp == null)
141             return;
142
143         // Okay, we have everything in place. Sort the rows.
144
if (bSortOrder == ITableSortingState.SORT_DESCENDING)
145             objCmp = new ReverseComparator(objCmp);
146
147         Arrays.sort(m_arrRows, objCmp);
148
149         m_objLastSortingState.setSortColumn(strSortColumn, bSortOrder);
150     }
151
152     public void tableDataChanged(CTableDataModelEvent objEvent)
153     {
154         m_arrRows = null;
155     }
156
157     /**
158      * Returns the dataModel.
159      * @return ITableDataModel
160      */

161     public ITableDataModel getDataModel()
162     {
163         return m_objDataModel;
164     }
165
166     /**
167      * Sets the dataModel.
168      * @param dataModel The dataModel to set
169      */

170     public void setDataModel(ITableDataModel dataModel)
171     {
172         if (m_objDataModel != null)
173             m_objDataModel.removeTableDataModelListener(this);
174             
175         m_objDataModel = dataModel;
176         m_objDataModel.addTableDataModelListener(this);
177         
178         m_arrRows = null;
179     }
180
181 }
182
Popular Tags