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