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    
040        /** @since 4.0 */
041        private ExpressionEvaluator _expressionEvaluator;
042    
043        /** @since 4.0 */
044    
045        public void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator)
046        {
047            _expressionEvaluator = expressionEvaluator;
048        }
049    
050        /**
051         * Generate a table column model out of the description string provided.
052         * Entries in the description string are separated by commas. Each column
053         * entry is of the format name, name:expression, or
054         * name:displayName:expression. An entry prefixed with ! represents a
055         * non-sortable column. If the whole description string is prefixed with *,
056         * it represents columns to be included in a Form.
057         * 
058         * @param strDesc
059         *            the description of the column model to be generated
060         * @param objComponent
061         *            the component ordering the generation
062         * @param objColumnSettingsContainer
063         *            the component containing the column settings
064         * @return a table column model based on the provided parameters
065         */
066        public ITableColumnModel generateTableColumnModel(
067                IAdvancedTableColumnSource objColumnSource, String strDesc,
068                IComponent objComponent, IComponent objColumnSettingsContainer)
069        {
070            if (strDesc == null) return null;
071    
072            List arrColumns = new ArrayList();
073    
074            String desc = strDesc.trim();
075            boolean bFormColumns = false;
076            while(desc.startsWith("*"))
077            {
078                desc = desc.substring(1);
079                bFormColumns = true;
080            }
081    
082            StringTokenizer objTokenizer = new StringTokenizer(desc, ",");
083            while(objTokenizer.hasMoreTokens())
084            {
085                String strToken = objTokenizer.nextToken().trim();
086    
087                if (strToken.startsWith("="))
088                {
089                    String strColumnExpression = strToken.substring(1);
090    
091                    Object objColumn = _expressionEvaluator.read(
092                            objColumnSettingsContainer, strColumnExpression);
093    
094                    if (!(objColumn instanceof ITableColumn))
095                        throw new ApplicationRuntimeException(TableMessages
096                                .notAColumn(objComponent, 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    
112                String strName = "";
113                if (objColumnTokenizer.hasMoreTokens())
114                    strName = objColumnTokenizer.nextToken();
115    
116                String strExpression = strName;
117                if (objColumnTokenizer.hasMoreTokens())
118                    strExpression = objColumnTokenizer.nextToken();
119    
120                String strDisplayName = strName;
121                if (objColumnTokenizer.hasMoreTokens())
122                {
123                    strDisplayName = strExpression;
124                    strExpression = objColumnTokenizer.nextToken();
125                }
126    
127                IAdvancedTableColumn objColumn = objColumnSource
128                        .generateTableColumn(strName, strDisplayName, bSortable,
129                                strExpression);
130                if (bFormColumns)
131                    objColumn
132                            .setColumnRendererSource(SimpleTableColumn.FORM_COLUMN_RENDERER_SOURCE);
133                if (objColumnSettingsContainer != null)
134                    objColumn.loadSettings(objColumnSettingsContainer);
135    
136                arrColumns.add(objColumn);
137            }
138    
139            return new SimpleTableColumnModel(arrColumns);
140        }
141    
142    }