1 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 65 public class DefaultTableModel extends AbstractTableModel { 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 83 public List getColumnList() { 84 return columnModel.getColumnList(); 85 } 86 87 public TableColumnModel getColumnModel() { 88 return columnModel; 89 } 90 91 94 public DefaultTableColumn addPropertyColumn(Object headerValue, String property, Class type) { 95 return addColumn(headerValue, new PropertyModel(rowModel, property, type)); 96 } 97 98 101 public DefaultTableColumn addClosureColumn(Object headerValue, Closure readClosure, Closure writeClosure, Class type) { 102 return addColumn(headerValue, new ClosureModel(rowModel, readClosure, writeClosure, type)); 103 } 104 105 public DefaultTableColumn addColumn(Object headerValue, ValueModel columnValueModel) { 106 DefaultTableColumn answer = new DefaultTableColumn(headerValue, columnValueModel); 107 addColumn(answer); 108 return answer; 109 } 110 111 114 public void addColumn(DefaultTableColumn column) { 115 columnModel.addColumn(column); 116 } 117 118 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 getColumnName(int columnIndex) { 134 String answer = null; 135 if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) { 136 return answer; 137 } 138 Object value = columnModel.getColumn(columnIndex).getHeaderValue(); 139 if (value != null) { 140 return value.toString(); 141 } 142 return answer; 143 } 144 145 public Class 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 getValueAt(int rowIndex, int columnIndex) { 154 List rows = getRows(); 155 Object 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 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 value, int rowIndex, int columnIndex) { 172 List rows = getRows(); 173 if (rowIndex < 0 || rowIndex >= rows.size()) { 174 return; 175 } 176 if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) { 177 return; 178 } 179 Object 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 getRows() { 194 Object 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 { 202 public List 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 |