001package jmri.jmrix.bidib.swing; 002 003import java.util.ArrayList; 004import java.util.Iterator; 005import java.util.List; 006import javax.swing.table.AbstractTableModel; 007 008/** 009 * not used so far... 010 * @author munderwd 011 */ 012public abstract class BiDiBTableModel extends AbstractTableModel { 013 014 protected List<List<Object>> rowData; 015 protected List<List<Object>> deletedData; 016 protected String[] columnNames = new String[0]; // this needs content 017 private int _dirtyCol = 0; 018 private int _newCol = 0; 019 private int _deleteCol = 0; 020 protected int _lastDataCol = 0; 021 022 public BiDiBTableModel(int dc, int nc, int delc, int numc) { 023 super(); 024 _dirtyCol = dc; 025 _newCol = nc; 026 _deleteCol = delc; 027 _lastDataCol = numc - 4; 028 rowData = new ArrayList<>(); 029 deletedData = new ArrayList<>(); 030 } 031 032 public int getDeleteColumn() { 033 return 3; 034 } 035 036 public boolean isDirty() { 037 for (int i = 0; i < rowData.size(); i++) { 038 if (isDirtyRow(i)) { 039 return true; 040 } 041 } 042 return false; 043 } 044 045 public boolean isNewRow(int row) { 046 //return((boolean) isNew.get(row)); 047 return (boolean) rowData.get(row).get(_newCol); 048 } 049 050 public void setNewRow(int row, boolean n) { 051 //isNew.set(row, n); 052 rowData.get(row).set(_newCol, n); 053 } 054 055 public boolean isDirtyRow(int row) { 056 //return((boolean)isDirty.get(row)); 057 return (boolean) rowData.get(row).get(_dirtyCol); 058 } 059 060 public void setDirtyRow(int row, boolean d) { 061 //isDirty.set(row, d); 062 rowData.get(row).set(_dirtyCol, d); 063 } 064 065 public boolean isMarkedForDelete(int row) { 066 //return((boolean)markDelete.get(row)); 067 return (boolean) rowData.get(row).get(_deleteCol); 068 } 069 070 public void removeRow(int row) { 071 deletedData.add(rowData.get(row)); 072 rowData.remove(row); 073 fireTableRowsDeleted(row, row); 074 } 075 076 public void markForDelete(int row, boolean mark) { 077 //markDelete.set(row, mark); 078 rowData.get(row).set(_deleteCol, mark); 079 if (mark) { 080 removeRow(row); 081 } 082 } 083 084 public boolean contains(List<Object> v) { 085 Iterator<List<Object>> it = rowData.iterator(); 086 while (it.hasNext()) { 087 List<Object> r = it.next(); 088 if (r.get(0) == v.get(0)) { 089 return true; 090 } 091 } 092 return false; 093 } 094 095 public void insertData(List<Object> v, boolean isnew) { 096 if (!rowData.contains(v)) { 097 v.add(Bundle.getMessage("ColumnDelete")); 098 v.add(isnew); // is new 099 v.add(false); // is dirty (no) 100 v.add(false); // is marked for delete (of course not) 101 rowData.add(v); 102 } 103 fireTableDataChanged(); 104 } 105 106 @Override 107 public void setValueAt(Object value, int row, int col) { 108 rowData.get(row).set(col, value); 109 if (col < _lastDataCol) { 110 // Only set dirty if data changed, not state 111 // Data is in columns 0-2 112 setDirtyRow(row, true); 113 } 114 fireTableCellUpdated(row, col); 115 } 116 117 public List<List<Object>> getRowData() { 118 return rowData; 119 } 120 121 @Override 122 public String getColumnName(int col) { 123 return columnNames[col]; 124 } 125 126 @Override 127 public int getRowCount() { 128 return rowData.size(); 129 } 130 131 @Override 132 public int getColumnCount() { 133 return columnNames.length; 134 } 135 136 @Override 137 public Object getValueAt(int row, int col) { 138 if (row >= 0 && row < rowData.size()) { 139 return rowData.get(row).get(col); 140 } else { 141 return 0; 142 } 143 } 144 145 @Override 146 public boolean isCellEditable(int row, int col) { 147 return true; 148 } 149 150}