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.ognl;
016    
017    import org.apache.tapestry.contrib.table.model.ITableColumn;
018    import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumnModel;
019    import org.apache.tapestry.services.ExpressionEvaluator;
020    
021    /**
022     * @author mindbridge
023     */
024    public class ExpressionTableColumnModel extends SimpleTableColumnModel
025    {
026            private static final long serialVersionUID = 1L;
027            
028        /**
029         * Constructs a table column model containting OGNL expression columns. <br>
030         * The data for the columns is provided in the form of a string array, where the info of each
031         * column is stored in two consecutive fields in the array, hence its size must be even. The
032         * expected info is the following:
033         * <ul>
034         * <li>Column Name
035         * <li>OGNL expression
036         * </ul>
037         * 
038         * @param arrColumnInfo
039         *            The information to construct the columns from
040         * @param bSorted
041         *            Whether all columns are sorted or not
042         */
043        public ExpressionTableColumnModel(String[] arrColumnInfo, boolean bSorted,
044                ExpressionEvaluator expressionEvaluator)
045        {
046            this(convertToDetailedArray(arrColumnInfo, bSorted), expressionEvaluator);
047        }
048    
049        /**
050         * Constructs a table column model containting OGNL expression columns. <br>
051         * The data for the columns is provided in the form of a string array, where the info of each
052         * column is stored in four consecutive fields in the array, hence its size must be divisible by
053         * 4.<br>
054         * The expected info is the following:
055         * <ul>
056         * <li>Column Name
057         * <li>Display Name
058         * <li>OGNL expression
059         * <li>Sorting of the column. This is either a Boolean, or a String representation of a
060         * boolean.
061         * </ul>
062         * 
063         * @param arrColumnInfo
064         */
065        public ExpressionTableColumnModel(Object[] arrColumnInfo,
066                ExpressionEvaluator expressionEvaluator)
067        {
068            super(convertToColumns(arrColumnInfo, expressionEvaluator));
069        }
070    
071        /**
072         * Method convertToDetailedArray.
073         * 
074         * @param arrColumnInfo
075         * @param bSorted
076         * @return Object[]
077         */
078        protected static Object[] convertToDetailedArray(String[] arrColumnInfo, boolean bSorted)
079        {
080            int nColumns = arrColumnInfo.length / 2;
081            int nSize = nColumns * 4;
082            Object[] arrDetailedInfo = new Object[nSize];
083    
084            for (int i = 0; i < nColumns; i++)
085            {
086                int nInputBaseIndex = 2 * i;
087                String strColumnName = arrColumnInfo[nInputBaseIndex];
088                String strExpression = arrColumnInfo[nInputBaseIndex + 1];
089    
090                int nOutputBaseIndex = 4 * i;
091                arrDetailedInfo[nOutputBaseIndex] = strColumnName;
092                arrDetailedInfo[nOutputBaseIndex + 1] = strColumnName;
093                arrDetailedInfo[nOutputBaseIndex + 2] = strExpression;
094                arrDetailedInfo[nOutputBaseIndex + 3] = bSorted ? Boolean.TRUE : Boolean.FALSE;
095            }
096    
097            return arrDetailedInfo;
098        }
099    
100        /**
101         * Method convertToColumns.
102         * 
103         * @param arrDetailedInfo
104         * @return ITableColumn[]
105         */
106        protected static ITableColumn[] convertToColumns(Object[] arrDetailedInfo,
107                ExpressionEvaluator expressionEvaluator)
108        {
109            int nColumns = arrDetailedInfo.length / 4;
110            ITableColumn[] arrColumns = new ITableColumn[nColumns];
111    
112            for (int i = 0; i < nColumns; i++)
113            {
114                Object objTempValue;
115                int nBaseIndex = 4 * i;
116    
117                String strColumnName = "";
118                objTempValue = arrDetailedInfo[nBaseIndex];
119                if (objTempValue != null)
120                    strColumnName = objTempValue.toString();
121    
122                String strDisplayName = "";
123                objTempValue = arrDetailedInfo[nBaseIndex + 1];
124                if (objTempValue != null)
125                    strDisplayName = objTempValue.toString();
126    
127                String strExpression = "";
128                objTempValue = arrDetailedInfo[nBaseIndex + 2];
129                if (objTempValue != null)
130                    strExpression = objTempValue.toString();
131    
132                boolean bSorted = false;
133                objTempValue = arrDetailedInfo[nBaseIndex + 3];
134                if (objTempValue != null)
135                {
136                    if (objTempValue instanceof Boolean)
137                        bSorted = ((Boolean) objTempValue).booleanValue();
138                    else
139                        bSorted = Boolean.valueOf(objTempValue.toString()).booleanValue();
140                }
141    
142                arrColumns[i] = new ExpressionTableColumn(strColumnName, strDisplayName, strExpression,
143                        bSorted, expressionEvaluator);
144            }
145    
146            return arrColumns;
147        }
148    }