001: /*
002: $Id: DefaultTableModel.java 259 2003-11-04 12:00:48Z jstrachan $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046: package groovy.model;
047:
048: import groovy.lang.Closure;
049:
050: import java.util.Collections;
051: import java.util.List;
052:
053: import javax.swing.table.AbstractTableModel;
054: import javax.swing.table.DefaultTableColumnModel;
055: import javax.swing.table.TableColumnModel;
056:
057: import org.codehaus.groovy.runtime.InvokerHelper;
058:
059: /**
060: * A default table model made up of PropertyModels on a Value model.
061: *
062: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
063: * @version $Revision: 259 $
064: */
065: public class DefaultTableModel extends AbstractTableModel {
066:
067: private ValueModel rowModel;
068: private ValueModel rowsModel;
069: private MyTableColumnModel columnModel = new MyTableColumnModel();
070:
071: public DefaultTableModel(ValueModel rowsModel) {
072: this (rowsModel, new ValueHolder());
073: }
074:
075: public DefaultTableModel(ValueModel rowsModel, ValueModel rowModel) {
076: this .rowModel = rowModel;
077: this .rowsModel = rowsModel;
078: }
079:
080: /**
081: * @return the column definitions.
082: */
083: public List getColumnList() {
084: return columnModel.getColumnList();
085: }
086:
087: public TableColumnModel getColumnModel() {
088: return columnModel;
089: }
090:
091: /**
092: * Adds a property model column to the table
093: */
094: public DefaultTableColumn addPropertyColumn(Object headerValue,
095: String property, Class type) {
096: return addColumn(headerValue, new PropertyModel(rowModel,
097: property, type));
098: }
099:
100: /**
101: * Adds a closure based column to the table
102: */
103: public DefaultTableColumn addClosureColumn(Object headerValue,
104: Closure readClosure, Closure writeClosure, Class type) {
105: return addColumn(headerValue, new ClosureModel(rowModel,
106: readClosure, writeClosure, type));
107: }
108:
109: public DefaultTableColumn addColumn(Object headerValue,
110: ValueModel columnValueModel) {
111: DefaultTableColumn answer = new DefaultTableColumn(headerValue,
112: columnValueModel);
113: addColumn(answer);
114: return answer;
115: }
116:
117: /**
118: * Adds a new column definition to the table
119: */
120: public void addColumn(DefaultTableColumn column) {
121: columnModel.addColumn(column);
122: }
123:
124: /**
125: * Removes a column definition from the table
126: */
127: public void removeColumn(DefaultTableColumn column) {
128: columnModel.removeColumn(column);
129: }
130:
131: public int getRowCount() {
132: return getRows().size();
133: }
134:
135: public int getColumnCount() {
136: return columnModel.getColumnCount();
137: }
138:
139: public String getColumnName(int columnIndex) {
140: String answer = null;
141: if (columnIndex < 0
142: || columnIndex >= columnModel.getColumnCount()) {
143: return answer;
144: }
145: Object value = columnModel.getColumn(columnIndex)
146: .getHeaderValue();
147: if (value != null) {
148: return value.toString();
149: }
150: return answer;
151: }
152:
153: public Class getColumnClass(int columnIndex) {
154: return getColumnModel(columnIndex).getType();
155: }
156:
157: public boolean isCellEditable(int rowIndex, int columnIndex) {
158: return getColumnModel(columnIndex).isEditable();
159: }
160:
161: public Object getValueAt(int rowIndex, int columnIndex) {
162: List rows = getRows();
163: Object answer = null;
164: if (rowIndex < 0 || rowIndex >= rows.size()) {
165: return answer;
166: }
167: if (columnIndex < 0
168: || columnIndex >= columnModel.getColumnCount()) {
169: return answer;
170: }
171: Object row = getRows().get(rowIndex);
172: rowModel.setValue(row);
173: DefaultTableColumn column = (DefaultTableColumn) columnModel
174: .getColumn(columnIndex);
175: if (row == null || column == null) {
176: return answer;
177: }
178: return column.getValue(row, rowIndex, columnIndex);
179: }
180:
181: public void setValueAt(Object value, int rowIndex, int columnIndex) {
182: List rows = getRows();
183: if (rowIndex < 0 || rowIndex >= rows.size()) {
184: return;
185: }
186: if (columnIndex < 0
187: || columnIndex >= columnModel.getColumnCount()) {
188: return;
189: }
190: Object row = getRows().get(rowIndex);
191: rowModel.setValue(row);
192: DefaultTableColumn column = (DefaultTableColumn) columnModel
193: .getColumn(columnIndex);
194: if (row == null || column == null) {
195: return;
196: }
197: column.setValue(row, value, rowIndex, columnIndex);
198: }
199:
200: protected ValueModel getColumnModel(int columnIndex) {
201: DefaultTableColumn column = (DefaultTableColumn) columnModel
202: .getColumn(columnIndex);
203: return column.getValueModel();
204: }
205:
206: protected List getRows() {
207: Object value = rowsModel.getValue();
208: if (value == null) {
209: return Collections.EMPTY_LIST;
210: }
211: return InvokerHelper.asList(value);
212: }
213:
214: protected static class MyTableColumnModel extends
215: DefaultTableColumnModel {
216: public List getColumnList() {
217: return tableColumns;
218: }
219: }
220:
221: public ValueModel getRowModel() {
222: return rowModel;
223: }
224:
225: public ValueModel getRowsModel() {
226: return rowsModel;
227: }
228:
229: }
|