Coverage Report - org.apache.tapestry.contrib.table.components.TableColumnModelSourceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
TableColumnModelSourceImpl
0%
0/43
0%
0/22
7.5
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.contrib.table.components;
 16  
 
 17  
 import java.util.ArrayList;
 18  
 import java.util.List;
 19  
 import java.util.StringTokenizer;
 20  
 
 21  
 import org.apache.hivemind.ApplicationRuntimeException;
 22  
 import org.apache.tapestry.IComponent;
 23  
 import org.apache.tapestry.contrib.table.model.IAdvancedTableColumn;
 24  
 import org.apache.tapestry.contrib.table.model.IAdvancedTableColumnSource;
 25  
 import org.apache.tapestry.contrib.table.model.ITableColumn;
 26  
 import org.apache.tapestry.contrib.table.model.ITableColumnModel;
 27  
 import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumn;
 28  
 import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumnModel;
 29  
 import org.apache.tapestry.services.ExpressionEvaluator;
 30  
 
 31  
 /**
 32  
  * A placeholder for a static methods related to the Table component.
 33  
  * 
 34  
  * @since 3.0
 35  
  * @author Mindbridge
 36  
  */
 37  0
 public class TableColumnModelSourceImpl implements TableColumnModelSource
 38  
 {
 39  
 
 40  
     /** @since 4.0 */
 41  
     private ExpressionEvaluator _expressionEvaluator;
 42  
 
 43  
     /** @since 4.0 */
 44  
 
 45  
     public void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator)
 46  
     {
 47  0
         _expressionEvaluator = expressionEvaluator;
 48  0
     }
 49  
 
 50  
     /**
 51  
      * Generate a table column model out of the description string provided.
 52  
      * Entries in the description string are separated by commas. Each column
 53  
      * entry is of the format name, name:expression, or
 54  
      * name:displayName:expression. An entry prefixed with ! represents a
 55  
      * non-sortable column. If the whole description string is prefixed with *,
 56  
      * it represents columns to be included in a Form.
 57  
      * 
 58  
      * @param strDesc
 59  
      *            the description of the column model to be generated
 60  
      * @param objComponent
 61  
      *            the component ordering the generation
 62  
      * @param objColumnSettingsContainer
 63  
      *            the component containing the column settings
 64  
      * @return a table column model based on the provided parameters
 65  
      */
 66  
     public ITableColumnModel generateTableColumnModel(
 67  
             IAdvancedTableColumnSource objColumnSource, String strDesc,
 68  
             IComponent objComponent, IComponent objColumnSettingsContainer)
 69  
     {
 70  0
         if (strDesc == null) return null;
 71  
 
 72  0
         List arrColumns = new ArrayList();
 73  
 
 74  0
         String desc = strDesc.trim();
 75  0
         boolean bFormColumns = false;
 76  0
         while(desc.startsWith("*"))
 77  
         {
 78  0
             desc = desc.substring(1);
 79  0
             bFormColumns = true;
 80  
         }
 81  
 
 82  0
         StringTokenizer objTokenizer = new StringTokenizer(desc, ",");
 83  0
         while(objTokenizer.hasMoreTokens())
 84  
         {
 85  0
             String strToken = objTokenizer.nextToken().trim();
 86  
 
 87  0
             if (strToken.startsWith("="))
 88  
             {
 89  0
                 String strColumnExpression = strToken.substring(1);
 90  
 
 91  0
                 Object objColumn = _expressionEvaluator.read(
 92  
                         objColumnSettingsContainer, strColumnExpression);
 93  
 
 94  0
                 if (!(objColumn instanceof ITableColumn))
 95  0
                     throw new ApplicationRuntimeException(TableMessages
 96  
                             .notAColumn(objComponent, strColumnExpression));
 97  
 
 98  0
                 arrColumns.add(objColumn);
 99  0
                 continue;
 100  
             }
 101  
 
 102  0
             boolean bSortable = true;
 103  0
             if (strToken.startsWith("!"))
 104  
             {
 105  0
                 strToken = strToken.substring(1);
 106  0
                 bSortable = false;
 107  
             }
 108  
 
 109  0
             StringTokenizer objColumnTokenizer = new StringTokenizer(strToken,
 110  
                     ":");
 111  
 
 112  0
             String strName = "";
 113  0
             if (objColumnTokenizer.hasMoreTokens())
 114  0
                 strName = objColumnTokenizer.nextToken();
 115  
 
 116  0
             String strExpression = strName;
 117  0
             if (objColumnTokenizer.hasMoreTokens())
 118  0
                 strExpression = objColumnTokenizer.nextToken();
 119  
 
 120  0
             String strDisplayName = strName;
 121  0
             if (objColumnTokenizer.hasMoreTokens())
 122  
             {
 123  0
                 strDisplayName = strExpression;
 124  0
                 strExpression = objColumnTokenizer.nextToken();
 125  
             }
 126  
 
 127  0
             IAdvancedTableColumn objColumn = objColumnSource
 128  
                     .generateTableColumn(strName, strDisplayName, bSortable,
 129  
                             strExpression);
 130  0
             if (bFormColumns)
 131  0
                 objColumn
 132  
                         .setColumnRendererSource(SimpleTableColumn.FORM_COLUMN_RENDERER_SOURCE);
 133  0
             if (objColumnSettingsContainer != null)
 134  0
                 objColumn.loadSettings(objColumnSettingsContainer);
 135  
 
 136  0
             arrColumns.add(objColumn);
 137  0
         }
 138  
 
 139  0
         return new SimpleTableColumnModel(arrColumns);
 140  
     }
 141  
 
 142  
 }