1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.swing.model;
17
18 import javax.swing.table.TableColumn;
19
20 import org.apache.commons.jelly.JellyContext;
21 import org.apache.commons.jelly.expression.Expression;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /***
27 * Represents a column in an ExpressionTable
28 *
29 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
30 * @version $Revision: 155420 $
31 */
32 public class ExpressionTableColumn extends TableColumn {
33
34 /*** The Log to which logging calls will be made. */
35 private static final Log log = LogFactory.getLog( ExpressionTableColumn.class );
36
37 private Expression value;
38 private Class type = Object.class;
39
40 public ExpressionTableColumn() {
41 }
42
43 public String toString() {
44 return super.toString() + "[value:" + value + "]";
45 }
46
47 /***
48 * Evaluates the value of a cell
49 */
50 public Object evaluateValue(ExpressionTableModel model, Object row, int rowIndex, int columnIndex) {
51 if (value == null) {
52 return null;
53 }
54
55 JellyContext context = model.getContext();
56 context.setVariable("rows", model.getRows());
57 context.setVariable("columns", model.getColumnList());
58 context.setVariable("row", row);
59 context.setVariable("rowIndex", new Integer(rowIndex));
60 context.setVariable("columnIndex", new Integer(columnIndex));
61
62
63 try {
64 return value.evaluateRecurse(context);
65 }
66 catch (RuntimeException e) {
67 log.warn( "Caught exception: " + e + " evaluating: " + value, e );
68 throw e;
69 }
70 }
71
72
73
74
75 /***
76 * Returns the column type.
77 * @return Class
78 */
79 public Class getType() {
80 return type;
81 }
82
83 /***
84 * Returns the expression used to extract the value.
85 * @return Expression
86 */
87 public Expression getValue() {
88 return value;
89 }
90
91 /***
92 * Sets the expression used to extract the value.
93 * @param type The type to set
94 */
95 public void setType(Class type) {
96 this.type = type;
97 }
98
99 /***
100 * Sets the value.
101 * @param value The value to set
102 */
103 public void setValue(Expression value) {
104 this.value = value;
105 }
106
107 }