KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > groovy > model > DefaultTableModel


1 /*
2  $Id: DefaultTableModel.java,v 1.2 2003/11/04 12:00:48 jstrachan Exp $
3
4  Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5
6  Redistribution and use of this software and associated documentation
7  ("Software"), with or without modification, are permitted provided
8  that the following conditions are met:
9
10  1. Redistributions of source code must retain copyright
11     statements and notices. Redistributions must also contain a
12     copy of this document.
13
14  2. Redistributions in binary form must reproduce the
15     above copyright notice, this list of conditions and the
16     following disclaimer in the documentation and/or other
17     materials provided with the distribution.
18
19  3. The name "groovy" must not be used to endorse or promote
20     products derived from this Software without prior written
21     permission of The Codehaus. For written permission,
22     please contact info@codehaus.org.
23
24  4. Products derived from this Software may not be called "groovy"
25     nor may "groovy" appear in their names without prior written
26     permission of The Codehaus. "groovy" is a registered
27     trademark of The Codehaus.
28
29  5. Due credit should be given to The Codehaus -
30     http://groovy.codehaus.org/
31
32  THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33  ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
36  THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43  OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  */

46 package groovy.model;
47
48 import groovy.lang.Closure;
49
50 import java.util.Collections JavaDoc;
51 import java.util.List JavaDoc;
52
53 import javax.swing.table.AbstractTableModel JavaDoc;
54 import javax.swing.table.DefaultTableColumnModel JavaDoc;
55 import javax.swing.table.TableColumnModel JavaDoc;
56
57 import org.codehaus.groovy.runtime.InvokerHelper;
58
59 /**
60  * A default table model made up of PropertyModels on a Value model.
61  *
62  * @author <a HREF="mailto:james@coredevelopers.net">James Strachan</a>
63  * @version $Revision: 1.2 $
64  */

65 public class DefaultTableModel extends AbstractTableModel JavaDoc {
66
67     private ValueModel rowModel;
68     private ValueModel rowsModel;
69     private MyTableColumnModel columnModel = new MyTableColumnModel();
70
71     public DefaultTableModel(ValueModel rowsModel) {
72         this(rowsModel, new ValueHolder());
73     }
74     
75     public DefaultTableModel(ValueModel rowsModel, ValueModel rowModel) {
76         this.rowModel = rowModel;
77         this.rowsModel = rowsModel;
78     }
79     
80     /**
81      * @return the column definitions.
82      */

83     public List JavaDoc getColumnList() {
84         return columnModel.getColumnList();
85     }
86
87     public TableColumnModel JavaDoc getColumnModel() {
88         return columnModel;
89     }
90     
91     /**
92      * Adds a property model column to the table
93      */

94     public DefaultTableColumn addPropertyColumn(Object JavaDoc headerValue, String JavaDoc property, Class JavaDoc type) {
95         return addColumn(headerValue, new PropertyModel(rowModel, property, type));
96     }
97     
98     /**
99      * Adds a closure based column to the table
100      */

101     public DefaultTableColumn addClosureColumn(Object JavaDoc headerValue, Closure readClosure, Closure writeClosure, Class JavaDoc type) {
102         return addColumn(headerValue, new ClosureModel(rowModel, readClosure, writeClosure, type));
103     }
104     
105     public DefaultTableColumn addColumn(Object JavaDoc headerValue, ValueModel columnValueModel) {
106         DefaultTableColumn answer = new DefaultTableColumn(headerValue, columnValueModel);
107         addColumn(answer);
108         return answer;
109     }
110     
111     /**
112      * Adds a new column definition to the table
113      */

114     public void addColumn(DefaultTableColumn column) {
115         columnModel.addColumn(column);
116     }
117     
118     /**
119      * Removes a column definition from the table
120      */

121     public void removeColumn(DefaultTableColumn column) {
122         columnModel.removeColumn(column);
123     }
124     
125     public int getRowCount() {
126         return getRows().size();
127     }
128
129     public int getColumnCount() {
130         return columnModel.getColumnCount();
131     }
132     
133     public String JavaDoc getColumnName(int columnIndex) {
134         String JavaDoc answer = null;
135         if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
136             return answer;
137         }
138         Object JavaDoc value = columnModel.getColumn(columnIndex).getHeaderValue();
139         if (value != null) {
140             return value.toString();
141         }
142         return answer;
143     }
144
145     public Class JavaDoc getColumnClass(int columnIndex) {
146         return getColumnModel(columnIndex).getType();
147     }
148
149     public boolean isCellEditable(int rowIndex, int columnIndex) {
150         return getColumnModel(columnIndex).isEditable();
151     }
152
153     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
154         List JavaDoc rows = getRows();
155         Object JavaDoc answer = null;
156         if (rowIndex < 0 || rowIndex >= rows.size()) {
157             return answer;
158         }
159         if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
160             return answer;
161         }
162         Object JavaDoc row = getRows().get(rowIndex);
163         rowModel.setValue(row);
164         DefaultTableColumn column = (DefaultTableColumn) columnModel.getColumn(columnIndex);
165         if (row == null || column == null) {
166             return answer;
167         }
168         return column.getValue(row, rowIndex, columnIndex);
169     }
170
171     public void setValueAt(Object JavaDoc value, int rowIndex, int columnIndex) {
172         List JavaDoc rows = getRows();
173         if (rowIndex < 0 || rowIndex >= rows.size()) {
174             return;
175         }
176         if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
177             return;
178         }
179         Object JavaDoc row = getRows().get(rowIndex);
180         rowModel.setValue(row);
181         DefaultTableColumn column = (DefaultTableColumn) columnModel.getColumn(columnIndex);
182         if (row == null || column == null) {
183             return;
184         }
185         column.setValue(row, value, rowIndex, columnIndex);
186     }
187
188     protected ValueModel getColumnModel(int columnIndex) {
189         DefaultTableColumn column = (DefaultTableColumn) columnModel.getColumn(columnIndex);
190         return column.getValueModel();
191     }
192
193     protected List JavaDoc getRows() {
194         Object JavaDoc value = rowsModel.getValue();
195         if (value == null) {
196             return Collections.EMPTY_LIST;
197         }
198         return InvokerHelper.asList(value);
199     }
200
201     protected static class MyTableColumnModel extends DefaultTableColumnModel JavaDoc {
202         public List JavaDoc getColumnList() {
203             return tableColumns;
204         }
205     }
206     
207     public ValueModel getRowModel() {
208         return rowModel;
209     }
210
211     public ValueModel getRowsModel() {
212         return rowsModel;
213     }
214
215 }
216
Popular Tags