|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DefaultTableModel.java | 0% | 0% | 0% | 0% |
|
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;
|
|
51 |
import java.util.List;
|
|
52 |
|
|
53 |
import javax.swing.table.AbstractTableModel;
|
|
54 |
import javax.swing.table.DefaultTableColumnModel;
|
|
55 |
import javax.swing.table.TableColumnModel;
|
|
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 { |
|
66 |
|
|
67 |
private ValueModel rowModel;
|
|
68 |
private ValueModel rowsModel;
|
|
69 |
private MyTableColumnModel columnModel = new MyTableColumnModel(); |
|
70 |
|
|
71 | 0 |
public DefaultTableModel(ValueModel rowsModel) {
|
72 | 0 |
this(rowsModel, new ValueHolder()); |
73 |
} |
|
74 |
|
|
75 | 0 |
public DefaultTableModel(ValueModel rowsModel, ValueModel rowModel) {
|
76 | 0 |
this.rowModel = rowModel;
|
77 | 0 |
this.rowsModel = rowsModel;
|
78 |
} |
|
79 |
|
|
80 |
/**
|
|
81 |
* @return the column definitions.
|
|
82 |
*/
|
|
83 | 0 |
public List getColumnList() {
|
84 | 0 |
return columnModel.getColumnList();
|
85 |
} |
|
86 |
|
|
87 | 0 |
public TableColumnModel getColumnModel() {
|
88 | 0 |
return columnModel;
|
89 |
} |
|
90 |
|
|
91 |
/**
|
|
92 |
* Adds a property model column to the table
|
|
93 |
*/
|
|
94 | 0 |
public DefaultTableColumn addPropertyColumn(Object headerValue, String property, Class type) {
|
95 | 0 |
return addColumn(headerValue, new PropertyModel(rowModel, property, type)); |
96 |
} |
|
97 |
|
|
98 |
/**
|
|
99 |
* Adds a closure based column to the table
|
|
100 |
*/
|
|
101 | 0 |
public DefaultTableColumn addClosureColumn(Object headerValue, Closure readClosure, Closure writeClosure, Class type) {
|
102 | 0 |
return addColumn(headerValue, new ClosureModel(rowModel, readClosure, writeClosure, type)); |
103 |
} |
|
104 |
|
|
105 | 0 |
public DefaultTableColumn addColumn(Object headerValue, ValueModel columnValueModel) {
|
106 | 0 |
DefaultTableColumn answer = new DefaultTableColumn(headerValue, columnValueModel);
|
107 | 0 |
addColumn(answer); |
108 | 0 |
return answer;
|
109 |
} |
|
110 |
|
|
111 |
/**
|
|
112 |
* Adds a new column definition to the table
|
|
113 |
*/
|
|
114 | 0 |
public void addColumn(DefaultTableColumn column) { |
115 | 0 |
columnModel.addColumn(column); |
116 |
} |
|
117 |
|
|
118 |
/**
|
|
119 |
* Removes a column definition from the table
|
|
120 |
*/
|
|
121 | 0 |
public void removeColumn(DefaultTableColumn column) { |
122 | 0 |
columnModel.removeColumn(column); |
123 |
} |
|
124 |
|
|
125 | 0 |
public int getRowCount() { |
126 | 0 |
return getRows().size();
|
127 |
} |
|
128 |
|
|
129 | 0 |
public int getColumnCount() { |
130 | 0 |
return columnModel.getColumnCount();
|
131 |
} |
|
132 |
|
|
133 | 0 |
public String getColumnName(int columnIndex) { |
134 | 0 |
String answer = null;
|
135 | 0 |
if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
|
136 | 0 |
return answer;
|
137 |
} |
|
138 | 0 |
Object value = columnModel.getColumn(columnIndex).getHeaderValue(); |
139 | 0 |
if (value != null) { |
140 | 0 |
return value.toString();
|
141 |
} |
|
142 | 0 |
return answer;
|
143 |
} |
|
144 |
|
|
145 | 0 |
public Class getColumnClass(int columnIndex) { |
146 | 0 |
return getColumnModel(columnIndex).getType();
|
147 |
} |
|
148 |
|
|
149 | 0 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
150 | 0 |
return getColumnModel(columnIndex).isEditable();
|
151 |
} |
|
152 |
|
|
153 | 0 |
public Object getValueAt(int rowIndex, int columnIndex) { |
154 | 0 |
List rows = getRows(); |
155 | 0 |
Object answer = null;
|
156 | 0 |
if (rowIndex < 0 || rowIndex >= rows.size()) {
|
157 | 0 |
return answer;
|
158 |
} |
|
159 | 0 |
if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
|
160 | 0 |
return answer;
|
161 |
} |
|
162 | 0 |
Object row = getRows().get(rowIndex); |
163 | 0 |
rowModel.setValue(row); |
164 | 0 |
DefaultTableColumn column = (DefaultTableColumn) columnModel.getColumn(columnIndex); |
165 | 0 |
if (row == null || column == null) { |
166 | 0 |
return answer;
|
167 |
} |
|
168 | 0 |
return column.getValue(row, rowIndex, columnIndex);
|
169 |
} |
|
170 |
|
|
171 | 0 |
public void setValueAt(Object value, int rowIndex, int columnIndex) { |
172 | 0 |
List rows = getRows(); |
173 | 0 |
if (rowIndex < 0 || rowIndex >= rows.size()) {
|
174 | 0 |
return;
|
175 |
} |
|
176 | 0 |
if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
|
177 | 0 |
return;
|
178 |
} |
|
179 | 0 |
Object row = getRows().get(rowIndex); |
180 | 0 |
rowModel.setValue(row); |
181 | 0 |
DefaultTableColumn column = (DefaultTableColumn) columnModel.getColumn(columnIndex); |
182 | 0 |
if (row == null || column == null) { |
183 | 0 |
return;
|
184 |
} |
|
185 | 0 |
column.setValue(row, value, rowIndex, columnIndex); |
186 |
} |
|
187 |
|
|
188 | 0 |
protected ValueModel getColumnModel(int columnIndex) { |
189 | 0 |
DefaultTableColumn column = (DefaultTableColumn) columnModel.getColumn(columnIndex); |
190 | 0 |
return column.getValueModel();
|
191 |
} |
|
192 |
|
|
193 | 0 |
protected List getRows() {
|
194 | 0 |
Object value = rowsModel.getValue(); |
195 | 0 |
if (value == null) { |
196 | 0 |
return Collections.EMPTY_LIST;
|
197 |
} |
|
198 | 0 |
return InvokerHelper.asList(value);
|
199 |
} |
|
200 |
|
|
201 |
protected static class MyTableColumnModel extends DefaultTableColumnModel { |
|
202 | 0 |
public List getColumnList() {
|
203 | 0 |
return tableColumns;
|
204 |
} |
|
205 |
} |
|
206 |
|
|
207 | 0 |
public ValueModel getRowModel() {
|
208 | 0 |
return rowModel;
|
209 |
} |
|
210 |
|
|
211 | 0 |
public ValueModel getRowsModel() {
|
212 | 0 |
return rowsModel;
|
213 |
} |
|
214 |
|
|
215 |
} |
|
216 |
|
|