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