View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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          // lets put the values in the context
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          // now lets invoke the expression
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      // Properties
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 }