1 56 package org.objectstyle.cayenne.dataview; 57 58 import javax.swing.table.AbstractTableModel ; 59 60 import org.objectstyle.cayenne.DataObject; 61 62 67 public class DOTableModel extends AbstractTableModel implements DataObjectChangeListener { 68 69 private ObjEntityView view; 70 private int[] columnMap; 71 private String [] columnNames; 72 private boolean[] editableColumns; 73 private Class [] columnClasses; 74 private int columnCount = 0; 75 private DataObjectList dataObjects = new DataObjectList(1); 76 77 public DOTableModel() { 78 updateModel(); 79 } 80 81 public ObjEntityView getView() { 82 return view; 83 } 84 85 public void setView(ObjEntityView view) { 86 this.view = view; 87 updateModel(); 88 } 89 90 public void updateModel() { 91 if (view != null) { 92 int fieldCount = view.getFieldCount(); 93 columnMap = new int[fieldCount]; 94 columnNames = new String [fieldCount]; 95 editableColumns = new boolean[fieldCount]; 96 columnClasses = new Class [fieldCount]; 97 columnCount = 0; 98 for (int i = 0; i < fieldCount; i++) { 99 ObjEntityViewField field = view.getField(i); 100 if (!field.isVisible()) 101 continue; 102 columnMap[columnCount] = i; 103 columnNames[columnCount] = field.getCaption(); 104 editableColumns[columnCount] = field.isEditable(); 105 columnClasses[columnCount] = field.getJavaClass(); 106 if (columnClasses[columnCount] == null) 107 columnClasses[columnCount] = String .class; 108 columnCount++; 109 } 110 } 111 else { 112 columnMap = new int[0]; 113 columnNames = new String [0]; 114 editableColumns = new boolean[0]; 115 columnClasses = new Class [0]; 116 columnCount = 0; 117 dataObjects = new DataObjectList(1); 118 } 119 fireTableStructureChanged(); 120 } 121 122 public int getColumnCount() { 123 return columnCount; 124 } 125 126 public boolean isCellEditable(int rowIndex, int columnIndex) { 127 return editableColumns[columnIndex]; 128 } 129 130 public String getColumnName(int column) { 131 return columnNames[column]; 132 } 133 134 public Class getColumnClass(int columnIndex) { 135 return columnClasses[columnIndex]; 136 } 137 138 public Object getValueAt(int rowIndex, int columnIndex) { 139 ObjEntityViewField field = getField(columnIndex); 140 DataObject obj = getDataObject(rowIndex); 141 Object value = field.getValue(obj); 142 return value; 143 } 144 145 public int getRowCount() { 146 return dataObjects.size(); 147 } 148 149 public void setValueAt(Object value, int rowIndex, int columnIndex) { 150 ObjEntityViewField field = getField(columnIndex); 151 DataObject obj = getDataObject(rowIndex); 152 field.setValue(obj, value); 153 } 154 155 public DataObject getDataObject(int rowIndex) { 156 return dataObjects.getDataObject(rowIndex); 157 } 158 159 public ObjEntityViewField getField(int columnIndex) { 160 return view.getField(columnMap[columnIndex]); 161 } 162 163 public DataObjectList getDataObjects() { 164 return dataObjects; 165 } 166 167 public void setDataObjects(DataObjectList dataObjects) { 168 this.dataObjects.removeDataObjectChangeListener(this); 169 this.dataObjects = dataObjects; 170 this.dataObjects.addDataObjectChangeListener(this); 171 fireTableDataChanged(); 172 } 173 174 public void dataChanged(DataObjectChangeEvent event) { 175 if (event.isMultiObjectChange()) { 176 fireTableDataChanged(); 177 return; 178 } 179 int affectedRow = event.getAffectedDataObjectIndex(); 180 switch (event.getId()) { 181 case DataObjectChangeEvent.DATAOBJECT_ADDED: 182 fireTableRowsInserted(affectedRow, affectedRow); 183 break; 184 case DataObjectChangeEvent.DATAOBJECT_REMOVED: 185 fireTableRowsDeleted(affectedRow, affectedRow); 186 break; 187 case DataObjectChangeEvent.DATAOBJECT_CHANGED: 188 fireTableRowsUpdated(affectedRow, affectedRow); 189 break; 190 default: 191 fireTableDataChanged(); 192 } 193 } 194 } | Popular Tags |