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    
037        private static final long serialVersionUID = 1L;
038    
039        private ITableColumn[] m_arrColumns;
040        private Map m_mapColumns;
041    
042        public SimpleTableColumnModel(ITableColumn[] arrColumns)
043        {
044            m_arrColumns = arrColumns;
045    
046            m_mapColumns = new HashMap();
047            for(int i = 0; i < m_arrColumns.length; i++)
048                m_mapColumns.put(m_arrColumns[i].getColumnName(), m_arrColumns[i]);
049        }
050    
051        public SimpleTableColumnModel(List arrColumns)
052        {
053            this((ITableColumn[]) arrColumns.toArray(new ITableColumn[arrColumns
054                    .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            return m_arrColumns[nColumn];
070        }
071    
072        public ITableColumn getColumn(String strColumn)
073        {
074            return (ITableColumn) m_mapColumns.get(strColumn);
075        }
076    
077        public Iterator getColumns()
078        {
079            return new ArrayIterator(m_arrColumns);
080        }
081    
082    }