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.simple;
016
017 import java.io.Serializable;
018 import java.util.ArrayList;
019 import java.util.Arrays;
020 import java.util.Collection;
021 import java.util.Iterator;
022 import java.util.List;
023
024 import org.apache.tapestry.contrib.table.model.CTableDataModelEvent;
025 import org.apache.tapestry.contrib.table.model.common.AbstractTableDataModel;
026 import org.apache.tapestry.contrib.table.model.common.ArrayIterator;
027
028 /**
029 * A minimal list implementation of the
030 * {@link org.apache.tapestry.contrib.table.model.ITableDataModel} interface.
031 *
032 * @author mindbridge
033 */
034 public class SimpleListTableDataModel extends AbstractTableDataModel implements
035 Serializable
036 {
037
038 private static final long serialVersionUID = 1L;
039
040 private List m_arrRows;
041
042 public SimpleListTableDataModel(Object[] arrRows)
043 {
044 this(Arrays.asList(arrRows));
045 }
046
047 public SimpleListTableDataModel(List arrRows)
048 {
049 m_arrRows = arrRows;
050 }
051
052 public SimpleListTableDataModel(Collection arrRows)
053 {
054 m_arrRows = new ArrayList(arrRows);
055 }
056
057 public SimpleListTableDataModel(Iterator objRows)
058 {
059 m_arrRows = new ArrayList();
060 addAll(m_arrRows, objRows);
061 }
062
063 private void addAll(List arrRows, Iterator objRows)
064 {
065 while(objRows.hasNext())
066 arrRows.add(objRows.next());
067 }
068
069 /**
070 * @see org.apache.tapestry.contrib.table.model.ITableDataModel#getRowCount()
071 */
072 public int getRowCount()
073 {
074 return m_arrRows.size();
075 }
076
077 /**
078 * Returns the row element at the given position.
079 *
080 * @param nRow
081 * the index of the element to return
082 */
083 public Object getRow(int nRow)
084 {
085 if (nRow < 0 || nRow >= m_arrRows.size())
086 {
087 // error message
088 return null;
089 }
090 return m_arrRows.get(nRow);
091 }
092
093 /**
094 * Returns an Iterator with the elements from the given range.
095 *
096 * @param nFrom
097 * the start of the range (inclusive)
098 * @param nTo
099 * the stop of the range (exclusive)
100 */
101 public Iterator getRows(int nFrom, int nTo)
102 {
103 return new ArrayIterator(m_arrRows.toArray(), nFrom, nTo);
104 }
105
106 /**
107 * @see org.apache.tapestry.contrib.table.model.ITableDataModel#getRows()
108 */
109 public Iterator getRows()
110 {
111 return m_arrRows.iterator();
112 }
113
114 /**
115 * Method addRow. Adds a row object to the model at its end
116 *
117 * @param objRow
118 * the row object to add
119 */
120 public void addRow(Object objRow)
121 {
122 m_arrRows.add(objRow);
123
124 CTableDataModelEvent objEvent = new CTableDataModelEvent();
125 fireTableDataModelEvent(objEvent);
126 }
127
128 public void addRows(Collection arrRows)
129 {
130 m_arrRows.addAll(arrRows);
131
132 CTableDataModelEvent objEvent = new CTableDataModelEvent();
133 fireTableDataModelEvent(objEvent);
134 }
135
136 /**
137 * Method removeRow. Removes a row object from the model
138 *
139 * @param objRow
140 * the row object to remove
141 */
142 public void removeRow(Object objRow)
143 {
144 m_arrRows.remove(objRow);
145
146 CTableDataModelEvent objEvent = new CTableDataModelEvent();
147 fireTableDataModelEvent(objEvent);
148 }
149
150 public void removeRows(Collection arrRows)
151 {
152 m_arrRows.removeAll(arrRows);
153
154 CTableDataModelEvent objEvent = new CTableDataModelEvent();
155 fireTableDataModelEvent(objEvent);
156 }
157
158 }