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.components;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    import java.util.StringTokenizer;
020    
021    import org.apache.hivemind.ApplicationRuntimeException;
022    import org.apache.tapestry.IComponent;
023    import org.apache.tapestry.contrib.table.model.IAdvancedTableColumn;
024    import org.apache.tapestry.contrib.table.model.IAdvancedTableColumnSource;
025    import org.apache.tapestry.contrib.table.model.ITableColumn;
026    import org.apache.tapestry.contrib.table.model.ITableColumnModel;
027    import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumn;
028    import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumnModel;
029    import org.apache.tapestry.services.ExpressionEvaluator;
030    
031    /**
032     * A placeholder for a static methods related to the Table component
033     * 
034     * @since 3.0
035     * @author Mindbridge
036     */
037    public class TableColumnModelSourceImpl implements TableColumnModelSource
038    {
039        /** @since 4.0 */
040        private ExpressionEvaluator _expressionEvaluator;
041    
042        /** @since 4.0 */
043    
044        public void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator)
045        {
046            _expressionEvaluator = expressionEvaluator;
047        }
048    
049        /**
050         * Generate a table column model out of the description string provided. Entries in the
051         * description string are separated by commas. Each column entry is of the format name,
052         * name:expression, or name:displayName:expression. An entry prefixed with ! represents a
053         * non-sortable column. If the whole description string is prefixed with *, it represents
054         * columns to be included in a Form.
055         * 
056         * @param strDesc
057         *            the description of the column model to be generated
058         * @param objComponent
059         *            the component ordering the generation
060         * @param objColumnSettingsContainer
061         *            the component containing the column settings
062         * @return a table column model based on the provided parameters
063         */
064        public ITableColumnModel generateTableColumnModel(IAdvancedTableColumnSource objColumnSource,
065                    String strDesc, IComponent objComponent, IComponent objColumnSettingsContainer)
066        {
067            if (strDesc == null)
068                return null;
069    
070            List arrColumns = new ArrayList();
071    
072            strDesc = strDesc.trim();
073            boolean bFormColumns = false;
074            while (strDesc.startsWith("*"))
075            {
076                strDesc = strDesc.substring(1);
077                bFormColumns = true;
078            }
079    
080            StringTokenizer objTokenizer = new StringTokenizer(strDesc, ",");
081            while (objTokenizer.hasMoreTokens())
082            {
083                String strToken = objTokenizer.nextToken().trim();
084    
085                if (strToken.startsWith("="))
086                {
087                    String strColumnExpression = strToken.substring(1);
088    
089                    Object objColumn = _expressionEvaluator.read(
090                            objColumnSettingsContainer,
091                            strColumnExpression);
092    
093                    if (!(objColumn instanceof ITableColumn))
094                        throw new ApplicationRuntimeException(TableMessages.notAColumn(
095                                objComponent,
096                                strColumnExpression));
097    
098                    arrColumns.add(objColumn);
099                    continue;
100                }
101    
102                boolean bSortable = true;
103                if (strToken.startsWith("!"))
104                {
105                    strToken = strToken.substring(1);
106                    bSortable = false;
107                }
108    
109                StringTokenizer objColumnTokenizer = new StringTokenizer(strToken, ":");
110    
111                String strName = "";
112                if (objColumnTokenizer.hasMoreTokens())
113                    strName = objColumnTokenizer.nextToken();
114    
115                String strExpression = strName;
116                if (objColumnTokenizer.hasMoreTokens())
117                    strExpression = objColumnTokenizer.nextToken();
118    
119                String strDisplayName = strName;
120                if (objColumnTokenizer.hasMoreTokens())
121                {
122                    strDisplayName = strExpression;
123                    strExpression = objColumnTokenizer.nextToken();
124                }
125    
126                IAdvancedTableColumn objColumn = 
127                    objColumnSource.generateTableColumn(strName, strDisplayName,
128                                    bSortable, strExpression);
129                if (bFormColumns)
130                    objColumn.setColumnRendererSource(SimpleTableColumn.FORM_COLUMN_RENDERER_SOURCE);
131                if (objColumnSettingsContainer != null)
132                    objColumn.loadSettings(objColumnSettingsContainer);
133    
134                arrColumns.add(objColumn);
135            }
136    
137            return new SimpleTableColumnModel(arrColumns);
138        }
139    
140    }