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.HashMap;
019    import java.util.Iterator;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.tapestry.contrib.table.model.ITableColumn;
024    import org.apache.tapestry.contrib.table.model.ITableColumnModel;
025    import org.apache.tapestry.contrib.table.model.common.ArrayIterator;
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    
045            m_mapColumns = new HashMap();
046            for (int i = 0; i < m_arrColumns.length; i++)
047                m_mapColumns.put(m_arrColumns[i].getColumnName(), m_arrColumns[i]);
048        }
049    
050        public SimpleTableColumnModel(List arrColumns)
051        {
052            this((ITableColumn[]) arrColumns.toArray(new ITableColumn[arrColumns.size()]));
053        }
054    
055        public int getColumnCount()
056        {
057            return m_arrColumns.length;
058        }
059    
060        public ITableColumn getColumn(int nColumn)
061        {
062            if (nColumn < 0 || nColumn >= m_arrColumns.length)
063            {
064                // error message
065                return null;
066            }
067            return m_arrColumns[nColumn];
068        }
069    
070        public ITableColumn getColumn(String strColumn)
071        {
072            return (ITableColumn) m_mapColumns.get(strColumn);
073        }
074    
075        public Iterator getColumns()
076        {
077            return new ArrayIterator(m_arrColumns);
078        }
079    
080    }