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 org.apache.tapestry.contrib.table.model.ITableColumn;
018    import org.apache.tapestry.contrib.table.model.ITableColumnModel;
019    import org.apache.tapestry.contrib.table.model.common.ArrayIterator;
020    
021    import java.io.Serializable;
022    import java.util.HashMap;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.Map;
026    
027    /**
028     * A minimal implementation of the
029     * {@link org.apache.tapestry.contrib.table.model.ITableColumnModel} interface
030     * that stores columns as an array.
031     * 
032     * @author mindbridge
033     */
034    public class SimpleTableColumnModel implements ITableColumnModel, Serializable
035    {
036        private static final long serialVersionUID = 1L;
037    
038        private ITableColumn[] m_arrColumns;
039        private Map m_mapColumns;
040    
041        public SimpleTableColumnModel(ITableColumn[] arrColumns)
042        {
043            m_arrColumns = arrColumns;
044            m_mapColumns = new HashMap();
045    
046            for(int i = 0; i < m_arrColumns.length; i++)
047            {
048                m_mapColumns.put(m_arrColumns[i].getColumnName(), m_arrColumns[i]);
049            }
050        }
051    
052        public SimpleTableColumnModel(List arrColumns)
053        {
054            this((ITableColumn[]) arrColumns.toArray(new ITableColumn[arrColumns.size()]));
055        }
056    
057        public int getColumnCount()
058        {
059            return m_arrColumns.length;
060        }
061    
062        public ITableColumn getColumn(int nColumn)
063        {
064            if (nColumn < 0 || nColumn >= m_arrColumns.length)
065            {
066                // error message
067                return null;
068            }
069            
070            return m_arrColumns[nColumn];
071        }
072    
073        public ITableColumn getColumn(String strColumn)
074        {
075            return (ITableColumn) m_mapColumns.get(strColumn);
076        }
077    
078        public Iterator getColumns()
079        {
080            return new ArrayIterator(m_arrColumns);
081        }
082    }