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