1 package net.sf.jga.swing; 33 34 import java.util.Iterator ; 35 import java.util.List ; 36 import javax.swing.table.AbstractTableModel ; 37 import javax.swing.table.DefaultTableColumnModel ; 38 import javax.swing.table.TableCellEditor ; 39 import javax.swing.table.TableCellRenderer ; 40 import javax.swing.table.TableColumnModel ; 41 import net.sf.jga.fn.BinaryFunctor; 42 import net.sf.jga.fn.EvaluationException; 43 import net.sf.jga.fn.UnaryFunctor; 44 import net.sf.jga.fn.property.ConstructUnary; 45 import net.sf.jga.fn.property.GetProperty; 46 import net.sf.jga.fn.property.SetProperty; 47 import net.sf.jga.fn.string.DefaultFormat; 48 import net.sf.jga.fn.string.FormatValue; 49 import net.sf.jga.fn.string.ParseFormat; 50 import net.sf.jga.fn.adaptor.Identity; 51 52 104 105 116 118 public class GenericTableModel<T> extends AbstractTableModel { 119 120 static final long serialVersionUID = 8996104516840561845L; 121 122 private Class <T> _rowtype; 123 private List <T> _values; 124 125 private DefaultTableColumnModel _columnModel = new DefaultTableColumnModel (); 126 127 131 public GenericTableModel (Class <T> rowtype, List <T> values) { 132 _rowtype = rowtype; 133 _values = values; 134 } 135 136 140 public GenericTableModel (Class <T> rowtype, List <T> values, 141 List <GenericTableColumn<T,?>> columns) 142 { 143 _rowtype = rowtype; 144 _values = values; 145 Iterator <GenericTableColumn<T,?>> iter = columns.iterator(); 146 while(iter.hasNext()) 147 _columnModel.addColumn(iter.next()); 148 } 149 150 154 155 public TableColumnModel getColumnModel() { 156 return _columnModel; 157 } 158 159 162 public T getRowValue(int row) { 163 return _values.get(row); 164 } 165 166 private boolean _nameUsedInHeader = true; 169 170 175 public void setNameUsedInHeader(boolean b) { _nameUsedInHeader = b; } 176 177 178 182 public boolean isNameUsedInHeader() { return _nameUsedInHeader; } 183 184 189 190 public <C> GenericTableColumn<T,C> addColumn(Class <C> coltype, String name){ 191 GetProperty<T,C> getter = new GetProperty<T,C>(_rowtype, name); 192 return addColumnImpl(name, new GenericTableColumn<T,C>(coltype, getter)); 193 } 194 195 201 202 public <C> GenericTableColumn<T,C> addColumn(Class <C> coltype, String name, 203 boolean editable) 204 { 205 GetProperty<T,C> getter = new GetProperty<T,C>(_rowtype, name); 206 SetProperty<T,C> setter = editable ? new SetProperty<T,C>(_rowtype, name, coltype) : null; 207 return addColumnImpl(name, new GenericTableColumn<T,C>(coltype, getter, setter)); 208 } 209 210 216 public <C> GenericTableColumn<T,C> addColumn(Class <C> coltype, String name, 217 UnaryFunctor<C,String > formatter) 218 { 219 GetProperty<T,C> getter = new GetProperty<T,C>(_rowtype, name); 220 return addColumnImpl(name, new GenericTableColumn<T,C>(coltype,getter,null,formatter,null)); 221 } 222 223 224 231 public <C> GenericTableColumn<T,C> addColumn(Class <C> coltype, String name, 232 UnaryFunctor<C,String > formatter, 233 UnaryFunctor<String ,C> parser) 234 { 235 GetProperty<T,C> getter = new GetProperty<T,C>(_rowtype, name); 236 SetProperty<T,C> setter = (parser != null) ? 237 new SetProperty<T,C>(_rowtype, name, coltype) : null; 238 239 return addColumnImpl(name, 240 new GenericTableColumn<T,C>(coltype,getter,setter,formatter,parser)); 241 } 242 243 244 247 private <C> GenericTableColumn<T,C> addColumnImpl(String name, GenericTableColumn<T,C> column) { 248 if (_nameUsedInHeader) 249 column.setHeaderValue(name); 250 251 addColumn(column); 252 return column; 253 } 254 255 260 261 public void addColumn(GenericTableColumn<T,?> col) { 262 col.setModelIndex(getColumnCount()); 263 _columnModel.addColumn(col); 264 } 265 266 269 270 public GenericTableColumn<T,?> getGenericColumn(int col) { 271 return (GenericTableColumn<T,?>) _columnModel.getColumn(col); 274 } 275 276 280 public int getRowCount() { 281 return _values.size(); 282 } 283 284 public int getColumnCount() { 285 return _columnModel.getColumnCount(); 286 } 287 288 public Class getColumnClass(int col) { 289 return getGenericColumn(col).getColumnClass(); 290 } 291 292 public String getColumnName(int col) { 293 GenericTableColumn<T,?> column = getGenericColumn(col); 294 Object name = column.getHeaderValue(); 295 return (name != null) ? name.toString() : super.getColumnName(col); 296 } 297 298 public boolean isCellEditable(int row, int col) { 299 return getGenericColumn(col).isEditable(); 300 } 301 302 public Object getValueAt(int row, int col) { 303 T rowvalue = getRowValue(row); 304 GenericTableColumn<T,?> column = getGenericColumn(col); 305 try { 306 return column.getValueAt(rowvalue); 307 } 308 catch (EvaluationException x) { 309 x.printStackTrace(); 310 return "### ERROR ###"; 311 } 312 } 313 314 public void setValueAt(Object value, int row, int col) { 315 T rowvalue = getRowValue(row); 316 GenericTableColumn<T,?> column = getGenericColumn(col); 317 assert column.getColumnClass().isInstance(value); 318 319 column.setValueAt(rowvalue, value); 320 fireTableCellUpdated(row,col); 321 } 322 } 323 | Popular Tags |