001package jmri.jmrix.can.cbus.node;
002
003import java.util.ArrayList;
004
005import jmri.jmrix.can.CanSystemConnectionMemo;
006
007/**
008 * Table data model for display of Cbus Nodes imported from MERG FCU
009 *
010 * @author Steve Young (c) 2019
011 * @see CbusNodeFromBackup
012 * 
013 */
014public class CbusNodeFromFcuTableDataModel extends CbusBasicNodeTableOperations {
015
016    // column order needs to match list in column tooltips
017    static public final int FCU_NODE_NUMBER_COLUMN = 0; 
018    static public final int FCU_NODE_TYPE_NAME_COLUMN = 1; 
019    static public final int FCU_NODE_USER_NAME_COLUMN = 2;
020    static public final int FCU_NODE_EVENTS_COLUMN = 3;
021    static public final int NODE_NV_TOTAL_COLUMN = 4;
022    static public final int FCU_NODE_TOTAL_BYTES_COLUMN = 5;
023    static public final int FCU_MAX_COLUMN = 6;
024
025    public CbusNodeFromFcuTableDataModel(CanSystemConnectionMemo memo, int row, int column) {
026        super (memo, row, column);
027        _mainArray = new ArrayList<>();
028    }
029
030    // order needs to match column list top of dtabledatamodel
031    public static final String[] FCUTABLETIPS = {
032        null,
033        null,
034        null,
035        null,
036        null,
037        null,
038        null,
039        null,
040        null,
041        null
042
043    }; // Length = number of items in array should (at least) match number of columns
044
045    /**
046     * {@inheritDoc}
047     */
048    @Override
049    public int getRowCount() {
050        return _mainArray.size();
051    }
052
053    /**
054     * Reset the Table Data.
055     */
056    public void resetData() {
057        _mainArray.clear();
058        fireTableDataChanged();
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public int getColumnCount() {
066        return FCU_MAX_COLUMN;
067    }
068
069    /**
070     * Returns String of column name from column int
071     * used in table header
072     * {@inheritDoc}
073     */
074    @Override
075    public String getColumnName(int col) { // not in any order
076        switch (col) {
077            case FCU_NODE_NUMBER_COLUMN:
078                return Bundle.getMessage("NodeNumberCol");
079            case FCU_NODE_USER_NAME_COLUMN:
080                return Bundle.getMessage("UserName");
081            case FCU_NODE_TYPE_NAME_COLUMN:
082                return Bundle.getMessage("ColumnType");
083            case FCU_NODE_EVENTS_COLUMN:
084                return Bundle.getMessage("CbusEvents");
085            case NODE_NV_TOTAL_COLUMN:
086                return Bundle.getMessage("ColumnNVs");
087            case FCU_NODE_TOTAL_BYTES_COLUMN:
088                return Bundle.getMessage("TotalBytes");
089            default:
090                return "unknown " + col; // NOI18N
091        }
092    } 
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public Class<?> getColumnClass(int col) {
099        if (_mainArray.isEmpty()) {
100            return Object.class;
101        }
102        return getValueAt(0, col).getClass();
103    }
104
105    /**
106     * Always False as backup Node.
107     * {@inheritDoc}
108     */
109    @Override
110    public boolean isCellEditable(int row, int col) {
111        return false;
112    }
113
114    /**
115     * {@inheritDoc}
116     */
117    @Override
118    public Object getValueAt(int row, int col) {
119        switch (col) {
120            case FCU_NODE_NUMBER_COLUMN:
121                return _mainArray.get(row).getNodeNumber();
122            case FCU_NODE_USER_NAME_COLUMN:
123                return _mainArray.get(row).getUserName();
124            case FCU_NODE_TYPE_NAME_COLUMN:
125                return _mainArray.get(row).getNodeStats().getNodeTypeName();
126            case FCU_NODE_EVENTS_COLUMN:
127                return _mainArray.get(row).getNodeEventManager().getTotalNodeEvents();
128            case NODE_NV_TOTAL_COLUMN:
129                return _mainArray.get(row).getNodeNvManager().getTotalNVs();
130            case FCU_NODE_TOTAL_BYTES_COLUMN:
131                return _mainArray.get(row).getNodeStats().totalNodeBytes();
132            default:
133                return null;
134        }
135    }
136
137    /**
138     * Ignored as data from file.
139     * {@inheritDoc}
140     */
141    @Override
142    public void setValueAt(Object value, int row, int col) {
143    }
144
145    /**
146     * Returns a new or existing Backup node by node number
147     * @param nodenum Node Number
148     */
149    @Override
150    public CbusNodeFromBackup provideNodeByNodeNum( int nodenum ) {
151        if ( nodenum < 1 || nodenum > 65535 ) {
152            throw new IllegalArgumentException("Node number should be between 1 and 65535");
153        }
154        for (int i = 0; i < getRowCount(); i++) {
155            if ( _mainArray.get(i).getNodeNumber() == nodenum ) {
156                return (CbusNodeFromBackup) _mainArray.get(i);
157            }
158        }
159        CbusNodeFromBackup cs = new CbusNodeFromBackup(_memo, nodenum);
160        // cs.startParamsLookup();
161        addNode(cs);
162        return cs;        
163    }
164    
165    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CbusNodeFromFcuTableDataModel.class);
166}