KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > components > TableColumnModelSourceImpl


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 JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
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 public class TableColumnModelSourceImpl implements TableColumnModelSource
38 {
39     /** @since 4.0 */
40     private ExpressionEvaluator _expressionEvaluator;
41
42     /** @since 4.0 */
43
44     public void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator)
45     {
46         _expressionEvaluator = expressionEvaluator;
47     }
48
49     /**
50      * Generate a table column model out of the description string provided. Entries in the
51      * description string are separated by commas. Each column entry is of the format name,
52      * name:expression, or name:displayName:expression. An entry prefixed with ! represents a
53      * non-sortable column. If the whole description string is prefixed with *, it represents
54      * columns to be included in a Form.
55      *
56      * @param strDesc
57      * the description of the column model to be generated
58      * @param objComponent
59      * the component ordering the generation
60      * @param objColumnSettingsContainer
61      * the component containing the column settings
62      * @return a table column model based on the provided parameters
63      */

64     public ITableColumnModel generateTableColumnModel(IAdvancedTableColumnSource objColumnSource,
65             String JavaDoc strDesc, IComponent objComponent, IComponent objColumnSettingsContainer)
66     {
67         if (strDesc == null)
68             return null;
69
70         List JavaDoc arrColumns = new ArrayList JavaDoc();
71
72         boolean bFormColumns = false;
73         while (strDesc.startsWith("*"))
74         {
75             strDesc = strDesc.substring(1);
76             bFormColumns = true;
77         }
78
79         StringTokenizer JavaDoc objTokenizer = new StringTokenizer JavaDoc(strDesc, ",");
80         while (objTokenizer.hasMoreTokens())
81         {
82             String JavaDoc strToken = objTokenizer.nextToken().trim();
83
84             if (strToken.startsWith("="))
85             {
86                 String JavaDoc strColumnExpression = strToken.substring(1);
87
88                 Object JavaDoc objColumn = _expressionEvaluator.read(
89                         objColumnSettingsContainer,
90                         strColumnExpression);
91
92                 if (!(objColumn instanceof ITableColumn))
93                     throw new ApplicationRuntimeException(TableMessages.notAColumn(
94                             objComponent,
95                             strColumnExpression));
96
97                 arrColumns.add(objColumn);
98                 continue;
99             }
100
101             boolean bSortable = true;
102             if (strToken.startsWith("!"))
103             {
104                 strToken = strToken.substring(1);
105                 bSortable = false;
106             }
107
108             StringTokenizer JavaDoc objColumnTokenizer = new StringTokenizer JavaDoc(strToken, ":");
109
110             String JavaDoc strName = "";
111             if (objColumnTokenizer.hasMoreTokens())
112                 strName = objColumnTokenizer.nextToken();
113
114             String JavaDoc strExpression = strName;
115             if (objColumnTokenizer.hasMoreTokens())
116                 strExpression = objColumnTokenizer.nextToken();
117
118             String JavaDoc strDisplayName = strName;
119             if (objColumnTokenizer.hasMoreTokens())
120             {
121                 strDisplayName = strExpression;
122                 strExpression = objColumnTokenizer.nextToken();
123             }
124
125             IAdvancedTableColumn objColumn =
126                 objColumnSource.generateTableColumn(strName, strDisplayName,
127                         bSortable, strExpression);
128             if (bFormColumns)
129                 objColumn.setColumnRendererSource(SimpleTableColumn.FORM_COLUMN_RENDERER_SOURCE);
130             if (objColumnSettingsContainer != null)
131                 objColumn.loadSettings(objColumnSettingsContainer);
132
133             arrColumns.add(objColumn);
134         }
135
136         return new SimpleTableColumnModel(arrColumns);
137     }
138
139 }
Popular Tags