KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.tapestry.contrib.table.model.CTableDataModelEvent;
25 import org.apache.tapestry.contrib.table.model.common.AbstractTableDataModel;
26 import org.apache.tapestry.contrib.table.model.common.ArrayIterator;
27
28 /**
29  * A minimal list implementation of the
30  * {@link org.apache.tapestry.contrib.table.model.ITableDataModel} interface
31  *
32  * @author mindbridge
33  */

34 public class SimpleListTableDataModel extends AbstractTableDataModel implements Serializable JavaDoc
35 {
36     private static final long serialVersionUID = 1L;
37     
38     private List JavaDoc m_arrRows;
39
40     public SimpleListTableDataModel(Object JavaDoc[] arrRows)
41     {
42         this(Arrays.asList(arrRows));
43     }
44
45     public SimpleListTableDataModel(List JavaDoc arrRows)
46     {
47         m_arrRows = arrRows;
48     }
49
50     public SimpleListTableDataModel(Collection JavaDoc arrRows)
51     {
52         m_arrRows = new ArrayList JavaDoc(arrRows);
53     }
54
55     public SimpleListTableDataModel(Iterator JavaDoc objRows)
56     {
57         m_arrRows = new ArrayList JavaDoc();
58         addAll(m_arrRows, objRows);
59     }
60
61     private void addAll(List JavaDoc arrRows, Iterator JavaDoc objRows)
62     {
63         while (objRows.hasNext())
64             arrRows.add(objRows.next());
65     }
66
67     /**
68      * @see org.apache.tapestry.contrib.table.model.ITableDataModel#getRowCount()
69      */

70     public int getRowCount()
71     {
72         return m_arrRows.size();
73     }
74
75     /**
76      * Returns the row element at the given position
77      * @param nRow the index of the element to return
78      */

79     public Object JavaDoc getRow(int nRow)
80     {
81         if (nRow < 0 || nRow >= m_arrRows.size())
82         {
83             // error message
84
return null;
85         }
86         return m_arrRows.get(nRow);
87     }
88
89     /**
90      * Returns an Iterator with the elements from the given range
91      * @param nFrom the start of the range (inclusive)
92      * @param nTo the stop of the range (exclusive)
93      */

94     public Iterator JavaDoc getRows(int nFrom, int nTo)
95     {
96         return new ArrayIterator(m_arrRows.toArray(), nFrom, nTo);
97     }
98
99     /**
100      * @see org.apache.tapestry.contrib.table.model.ITableDataModel#getRows()
101      */

102     public Iterator JavaDoc getRows()
103     {
104         return m_arrRows.iterator();
105     }
106
107     /**
108      * Method addRow.
109      * Adds a row object to the model at its end
110      * @param objRow the row object to add
111      */

112     public void addRow(Object JavaDoc objRow)
113     {
114         m_arrRows.add(objRow);
115
116         CTableDataModelEvent objEvent = new CTableDataModelEvent();
117         fireTableDataModelEvent(objEvent);
118     }
119
120     public void addRows(Collection JavaDoc arrRows)
121     {
122         m_arrRows.addAll(arrRows);
123
124         CTableDataModelEvent objEvent = new CTableDataModelEvent();
125         fireTableDataModelEvent(objEvent);
126     }
127
128     /**
129      * Method removeRow.
130      * Removes a row object from the model
131      * @param objRow the row object to remove
132      */

133     public void removeRow(Object JavaDoc objRow)
134     {
135         m_arrRows.remove(objRow);
136
137         CTableDataModelEvent objEvent = new CTableDataModelEvent();
138         fireTableDataModelEvent(objEvent);
139     }
140
141     public void removeRows(Collection JavaDoc arrRows)
142     {
143         m_arrRows.removeAll(arrRows);
144
145         CTableDataModelEvent objEvent = new CTableDataModelEvent();
146         fireTableDataModelEvent(objEvent);
147     }
148
149 }
150
Popular Tags