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 java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.swing.table.AbstractTableModel;
22  import javax.swing.table.DefaultTableColumnModel;
23  import javax.swing.table.TableColumnModel;
24  
25  import org.apache.commons.jelly.JellyContext;
26  
27  /***
28   * A Swing TableModel that uses a List of rows with pluggable Expressions
29   * to evaluate the value of the cells
30   *
31   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
32   * @version $Revision: 155420 $
33   */
34  public class ExpressionTableModel extends AbstractTableModel {
35  
36      private JellyContext context;
37      private List rows = new ArrayList();
38      private MyTableColumnModel columnModel = new MyTableColumnModel();
39  
40      public ExpressionTableModel() {
41      }
42  
43      /***
44       * Returns the column definitions.
45       * @return List
46       */
47      public List getColumnList() {
48          return columnModel.getColumnList();
49      }
50  
51      /***
52       * @return the TableColumnModel
53       */
54      public TableColumnModel getColumnModel() {
55          return columnModel;
56      }
57  
58      /***
59       * Adds a new column definition to the table
60       */
61      public void addColumn(ExpressionTableColumn column) {
62          columnModel.addColumn(column);
63      }
64  
65      /***
66       * Removes a column definition from the table
67       */
68      public void removeColumn(ExpressionTableColumn column) {
69          columnModel.removeColumn(column);
70      }
71  
72  
73      // TableModel interface
74      //-------------------------------------------------------------------------
75      public int getRowCount() {
76          return rows.size();
77      }
78  
79      public int getColumnCount() {
80          return columnModel.getColumnCount();
81      }
82  
83      public String getColumnName(int columnIndex) {
84          String answer = null;
85          if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
86              return answer;
87          }
88          Object value = columnModel.getColumn(columnIndex).getHeaderValue();
89          if (value != null) {
90              return value.toString();
91          }
92          return answer;
93      }
94  
95      public Object getValueAt(int rowIndex, int columnIndex) {
96          Object answer = null;
97          if (rowIndex < 0 || rowIndex >= rows.size()) {
98              return answer;
99          }
100         if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
101             return answer;
102         }
103         Object row = rows.get(rowIndex);;
104         ExpressionTableColumn column = (ExpressionTableColumn) columnModel.getColumn(columnIndex);
105         if (row == null || column == null) {
106             return answer;
107         }
108         return column.evaluateValue(this, row, rowIndex, columnIndex);
109     }
110 
111 
112     // Properties
113     //-------------------------------------------------------------------------
114 
115 
116     /***
117      * Returns the list of rows.
118      * @return List
119      */
120     public List getRows() {
121         return rows;
122     }
123 
124     /***
125      * Sets the list of rows.
126      * @param rows The rows to set
127      */
128     public void setRows(List rows) {
129         this.rows = rows;
130     }
131 
132     /***
133      * Returns the context.
134      * @return JellyContext
135      */
136     public JellyContext getContext() {
137         return context;
138     }
139 
140     /***
141      * Sets the context.
142      * @param context The context to set
143      */
144     public void setContext(JellyContext context) {
145         this.context = context;
146     }
147 
148     // Implementation methods
149     //-------------------------------------------------------------------------
150     protected static class MyTableColumnModel extends DefaultTableColumnModel {
151         public List getColumnList() {
152             return tableColumns;
153         }
154     };
155 
156 
157 }