1 19 20 package org.apache.cayenne.dataview; 21 22 import javax.swing.table.AbstractTableModel ; 23 24 import org.apache.cayenne.DataObject; 25 26 31 public class DOTableModel extends AbstractTableModel implements DataObjectChangeListener { 32 33 private ObjEntityView view; 34 private int[] columnMap; 35 private String [] columnNames; 36 private boolean[] editableColumns; 37 private Class [] columnClasses; 38 private int columnCount = 0; 39 private DataObjectList dataObjects = new DataObjectList(1); 40 41 public DOTableModel() { 42 updateModel(); 43 } 44 45 public ObjEntityView getView() { 46 return view; 47 } 48 49 public void setView(ObjEntityView view) { 50 this.view = view; 51 updateModel(); 52 } 53 54 public void updateModel() { 55 if (view != null) { 56 int fieldCount = view.getFieldCount(); 57 columnMap = new int[fieldCount]; 58 columnNames = new String [fieldCount]; 59 editableColumns = new boolean[fieldCount]; 60 columnClasses = new Class [fieldCount]; 61 columnCount = 0; 62 for (int i = 0; i < fieldCount; i++) { 63 ObjEntityViewField field = view.getField(i); 64 if (!field.isVisible()) 65 continue; 66 columnMap[columnCount] = i; 67 columnNames[columnCount] = field.getCaption(); 68 editableColumns[columnCount] = field.isEditable(); 69 columnClasses[columnCount] = field.getJavaClass(); 70 if (columnClasses[columnCount] == null) 71 columnClasses[columnCount] = String .class; 72 columnCount++; 73 } 74 } 75 else { 76 columnMap = new int[0]; 77 columnNames = new String [0]; 78 editableColumns = new boolean[0]; 79 columnClasses = new Class [0]; 80 columnCount = 0; 81 dataObjects = new DataObjectList(1); 82 } 83 fireTableStructureChanged(); 84 } 85 86 public int getColumnCount() { 87 return columnCount; 88 } 89 90 public boolean isCellEditable(int rowIndex, int columnIndex) { 91 return editableColumns[columnIndex]; 92 } 93 94 public String getColumnName(int column) { 95 return columnNames[column]; 96 } 97 98 public Class getColumnClass(int columnIndex) { 99 return columnClasses[columnIndex]; 100 } 101 102 public Object getValueAt(int rowIndex, int columnIndex) { 103 ObjEntityViewField field = getField(columnIndex); 104 DataObject obj = getDataObject(rowIndex); 105 Object value = field.getValue(obj); 106 return value; 107 } 108 109 public int getRowCount() { 110 return dataObjects.size(); 111 } 112 113 public void setValueAt(Object value, int rowIndex, int columnIndex) { 114 ObjEntityViewField field = getField(columnIndex); 115 DataObject obj = getDataObject(rowIndex); 116 field.setValue(obj, value); 117 } 118 119 public DataObject getDataObject(int rowIndex) { 120 return dataObjects.getDataObject(rowIndex); 121 } 122 123 public ObjEntityViewField getField(int columnIndex) { 124 return view.getField(columnMap[columnIndex]); 125 } 126 127 public DataObjectList getDataObjects() { 128 return dataObjects; 129 } 130 131 public void setDataObjects(DataObjectList dataObjects) { 132 this.dataObjects.removeDataObjectChangeListener(this); 133 this.dataObjects = dataObjects; 134 this.dataObjects.addDataObjectChangeListener(this); 135 fireTableDataChanged(); 136 } 137 138 public void dataChanged(DataObjectChangeEvent event) { 139 if (event.isMultiObjectChange()) { 140 fireTableDataChanged(); 141 return; 142 } 143 int affectedRow = event.getAffectedDataObjectIndex(); 144 switch (event.getId()) { 145 case DataObjectChangeEvent.DATAOBJECT_ADDED: 146 fireTableRowsInserted(affectedRow, affectedRow); 147 break; 148 case DataObjectChangeEvent.DATAOBJECT_REMOVED: 149 fireTableRowsDeleted(affectedRow, affectedRow); 150 break; 151 case DataObjectChangeEvent.DATAOBJECT_CHANGED: 152 fireTableRowsUpdated(affectedRow, affectedRow); 153 break; 154 default: 155 fireTableDataChanged(); 156 } 157 } 158 } 159 | Popular Tags |