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 Serializable 035 { 036 private static final long serialVersionUID = 1L; 037 038 private List m_arrRows; 039 040 public SimpleListTableDataModel(Object[] arrRows) 041 { 042 this(Arrays.asList(arrRows)); 043 } 044 045 public SimpleListTableDataModel(List arrRows) 046 { 047 m_arrRows = arrRows; 048 } 049 050 public SimpleListTableDataModel(Collection arrRows) 051 { 052 m_arrRows = new ArrayList(arrRows); 053 } 054 055 public SimpleListTableDataModel(Iterator objRows) 056 { 057 m_arrRows = new ArrayList(); 058 addAll(m_arrRows, objRows); 059 } 060 061 private void addAll(List arrRows, Iterator objRows) 062 { 063 while (objRows.hasNext()) 064 arrRows.add(objRows.next()); 065 } 066 067 /** 068 * @see org.apache.tapestry.contrib.table.model.ITableDataModel#getRowCount() 069 */ 070 public int getRowCount() 071 { 072 return m_arrRows.size(); 073 } 074 075 /** 076 * Returns the row element at the given position 077 * @param nRow the index of the element to return 078 */ 079 public Object getRow(int nRow) 080 { 081 if (nRow < 0 || nRow >= m_arrRows.size()) 082 { 083 // error message 084 return null; 085 } 086 return m_arrRows.get(nRow); 087 } 088 089 /** 090 * Returns an Iterator with the elements from the given range 091 * @param nFrom the start of the range (inclusive) 092 * @param nTo the stop of the range (exclusive) 093 */ 094 public Iterator getRows(int nFrom, int nTo) 095 { 096 return new ArrayIterator(m_arrRows.toArray(), nFrom, nTo); 097 } 098 099 /** 100 * @see org.apache.tapestry.contrib.table.model.ITableDataModel#getRows() 101 */ 102 public Iterator 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 objRow) 113 { 114 m_arrRows.add(objRow); 115 116 CTableDataModelEvent objEvent = new CTableDataModelEvent(); 117 fireTableDataModelEvent(objEvent); 118 } 119 120 public void addRows(Collection 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 objRow) 134 { 135 m_arrRows.remove(objRow); 136 137 CTableDataModelEvent objEvent = new CTableDataModelEvent(); 138 fireTableDataModelEvent(objEvent); 139 } 140 141 public void removeRows(Collection arrRows) 142 { 143 m_arrRows.removeAll(arrRows); 144 145 CTableDataModelEvent objEvent = new CTableDataModelEvent(); 146 fireTableDataModelEvent(objEvent); 147 } 148 149 }