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.Collection;
019    import java.util.Iterator;
020    import java.util.Set;
021    
022    import org.apache.tapestry.contrib.table.model.CTableDataModelEvent;
023    import org.apache.tapestry.contrib.table.model.common.AbstractTableDataModel;
024    
025    /**
026     * A minimal set implementation of the 
027     * {@link org.apache.tapestry.contrib.table.model.ITableDataModel} interface
028     * 
029     * @author mindbridge
030     */
031    public class SimpleSetTableDataModel extends AbstractTableDataModel implements Serializable
032    {
033            private static final long serialVersionUID = 1L;
034            
035        private Set m_setRows;
036    
037        public SimpleSetTableDataModel(Set setRows)
038        {
039            m_setRows = setRows;
040        }
041    
042        /**
043         * @see org.apache.tapestry.contrib.table.model.ITableDataModel#getRowCount()
044         */
045        public int getRowCount()
046        {
047            return m_setRows.size();
048        }
049    
050        /**
051         * @see org.apache.tapestry.contrib.table.model.ITableDataModel#getRows()
052         */
053        public Iterator getRows()
054        {
055            return m_setRows.iterator();
056        }
057    
058        /**
059         * Method addRow.
060         * Adds a row object to the model at its end
061         * @param objRow the row object to add
062         */
063        public void addRow(Object objRow)
064        {
065            if (m_setRows.contains(objRow)) return;
066            m_setRows.add(objRow);
067    
068            CTableDataModelEvent objEvent = new CTableDataModelEvent();
069            fireTableDataModelEvent(objEvent);
070        }
071    
072        public void addRows(Collection arrRows)
073        {
074            m_setRows.addAll(arrRows);
075    
076            CTableDataModelEvent objEvent = new CTableDataModelEvent();
077            fireTableDataModelEvent(objEvent);
078        }
079    
080        /**
081         * Method removeRow.
082         * Removes a row object from the model
083         * @param objRow the row object to remove
084         */
085        public void removeRow(Object objRow)
086        {
087            if (!m_setRows.contains(objRow)) return;
088            m_setRows.remove(objRow);
089    
090            CTableDataModelEvent objEvent = new CTableDataModelEvent();
091            fireTableDataModelEvent(objEvent);
092        }
093    
094        public void removeRows(Collection arrRows)
095        {
096            m_setRows.removeAll(arrRows);
097    
098            CTableDataModelEvent objEvent = new CTableDataModelEvent();
099            fireTableDataModelEvent(objEvent);
100        }
101    
102    }