KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > model > ognl > ExpressionTableColumnModel


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.model.ognl;
16
17 import org.apache.tapestry.contrib.table.model.ITableColumn;
18 import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumnModel;
19 import org.apache.tapestry.services.ExpressionEvaluator;
20
21 /**
22  * @author mindbridge
23  */

24 public class ExpressionTableColumnModel extends SimpleTableColumnModel
25 {
26     private static final long serialVersionUID = 1L;
27     
28     /**
29      * Constructs a table column model containting OGNL expression columns. <br>
30      * The data for the columns is provided in the form of a string array, where the info of each
31      * column is stored in two consecutive fields in the array, hence its size must be even. The
32      * expected info is the following:
33      * <ul>
34      * <li>Column Name
35      * <li>OGNL expression
36      * </ul>
37      *
38      * @param arrColumnInfo
39      * The information to construct the columns from
40      * @param bSorted
41      * Whether all columns are sorted or not
42      */

43     public ExpressionTableColumnModel(String JavaDoc[] arrColumnInfo, boolean bSorted,
44             ExpressionEvaluator expressionEvaluator)
45     {
46         this(convertToDetailedArray(arrColumnInfo, bSorted), expressionEvaluator);
47     }
48
49     /**
50      * Constructs a table column model containting OGNL expression columns. <br>
51      * The data for the columns is provided in the form of a string array, where the info of each
52      * column is stored in four consecutive fields in the array, hence its size must be divisible by
53      * 4.<br>
54      * The expected info is the following:
55      * <ul>
56      * <li>Column Name
57      * <li>Display Name
58      * <li>OGNL expression
59      * <li>Sorting of the column. This is either a Boolean, or a String representation of a
60      * boolean.
61      * </ul>
62      *
63      * @param arrColumnInfo
64      */

65     public ExpressionTableColumnModel(Object JavaDoc[] arrColumnInfo,
66             ExpressionEvaluator expressionEvaluator)
67     {
68         super(convertToColumns(arrColumnInfo, expressionEvaluator));
69     }
70
71     /**
72      * Method convertToDetailedArray.
73      *
74      * @param arrColumnInfo
75      * @param bSorted
76      * @return Object[]
77      */

78     protected static Object JavaDoc[] convertToDetailedArray(String JavaDoc[] arrColumnInfo, boolean bSorted)
79     {
80         int nColumns = arrColumnInfo.length / 2;
81         int nSize = nColumns * 4;
82         Object JavaDoc[] arrDetailedInfo = new Object JavaDoc[nSize];
83
84         for (int i = 0; i < nColumns; i++)
85         {
86             int nInputBaseIndex = 2 * i;
87             String JavaDoc strColumnName = arrColumnInfo[nInputBaseIndex];
88             String JavaDoc strExpression = arrColumnInfo[nInputBaseIndex + 1];
89
90             int nOutputBaseIndex = 4 * i;
91             arrDetailedInfo[nOutputBaseIndex] = strColumnName;
92             arrDetailedInfo[nOutputBaseIndex + 1] = strColumnName;
93             arrDetailedInfo[nOutputBaseIndex + 2] = strExpression;
94             arrDetailedInfo[nOutputBaseIndex + 3] = bSorted ? Boolean.TRUE : Boolean.FALSE;
95         }
96
97         return arrDetailedInfo;
98     }
99
100     /**
101      * Method convertToColumns.
102      *
103      * @param arrDetailedInfo
104      * @return ITableColumn[]
105      */

106     protected static ITableColumn[] convertToColumns(Object JavaDoc[] arrDetailedInfo,
107             ExpressionEvaluator expressionEvaluator)
108     {
109         int nColumns = arrDetailedInfo.length / 4;
110         ITableColumn[] arrColumns = new ITableColumn[nColumns];
111
112         for (int i = 0; i < nColumns; i++)
113         {
114             Object JavaDoc objTempValue;
115             int nBaseIndex = 4 * i;
116
117             String JavaDoc strColumnName = "";
118             objTempValue = arrDetailedInfo[nBaseIndex];
119             if (objTempValue != null)
120                 strColumnName = objTempValue.toString();
121
122             String JavaDoc strDisplayName = "";
123             objTempValue = arrDetailedInfo[nBaseIndex + 1];
124             if (objTempValue != null)
125                 strDisplayName = objTempValue.toString();
126
127             String JavaDoc strExpression = "";
128             objTempValue = arrDetailedInfo[nBaseIndex + 2];
129             if (objTempValue != null)
130                 strExpression = objTempValue.toString();
131
132             boolean bSorted = false;
133             objTempValue = arrDetailedInfo[nBaseIndex + 3];
134             if (objTempValue != null)
135             {
136                 if (objTempValue instanceof Boolean JavaDoc)
137                     bSorted = ((Boolean JavaDoc) objTempValue).booleanValue();
138                 else
139                     bSorted = Boolean.valueOf(objTempValue.toString()).booleanValue();
140             }
141
142             arrColumns[i] = new ExpressionTableColumn(strColumnName, strDisplayName, strExpression,
143                     bSorted, expressionEvaluator);
144         }
145
146         return arrColumns;
147     }
148 }
Popular Tags