001package jmri.jmrit.logixng.tools.swing;
002
003import java.util.*;
004
005import javax.swing.table.AbstractTableModel;
006
007import jmri.jmrit.logixng.SymbolTable;
008import jmri.jmrit.logixng.SymbolTable.Symbol;
009
010/**
011 * Table model for the current symbol table while debugging
012 * @author Daniel Bergqvist Copyright 2020
013 */
014public class DebuggerSymbolTableModel extends AbstractTableModel {
015
016    public static final int COLUMN_NAME = 0;
017    public static final int COLUMN_VALUE = 1;
018
019    private final List<Symbol> _symbols = new ArrayList<>();
020    private SymbolTable _symbolTable = null;
021
022
023    public void update(SymbolTable symbolTable) {
024        _symbolTable = symbolTable;
025        _symbols.clear();
026        for (Symbol s : symbolTable.getSymbols().values()) {
027            _symbols.add(s);
028        }
029        fireTableDataChanged();
030    }
031
032    /** {@inheritDoc} */
033    @Override
034    public int getRowCount() {
035        return _symbols.size();
036    }
037
038    /** {@inheritDoc} */
039    @Override
040    public int getColumnCount() {
041        return 2;
042    }
043
044    /** {@inheritDoc} */
045    @Override
046    public String getColumnName(int col) {
047        switch (col) {
048            case COLUMN_NAME:
049                return Bundle.getMessage("ColumnSymbolName");
050            case COLUMN_VALUE:
051                return Bundle.getMessage("ColumnSymbolValue");
052            default:
053                throw new IllegalArgumentException("Invalid column");
054        }
055    }
056
057    /** {@inheritDoc} */
058    @Override
059    public Class<?> getColumnClass(int col) {
060        switch (col) {
061            case COLUMN_NAME:
062            case COLUMN_VALUE:
063                return String.class;
064            default:
065                throw new IllegalArgumentException("Invalid column");
066        }
067    }
068
069    /** {@inheritDoc} */
070    @Override
071    public boolean isCellEditable(int row, int col) {
072        return true;
073    }
074
075    /** {@inheritDoc} */
076    @Override
077    public void setValueAt(Object value, int rowIndex, int columnIndex) {
078/*
079        Symbol symbol = _symbols.get(rowIndex);
080
081        switch (columnIndex) {
082            case COLUMN_VALUE:
083                _conditionalNG.getStack().setValueAtIndex(symbol.getIndex(), value);
084                break;
085            default:
086                throw new IllegalArgumentException("Invalid column");
087        }
088*/
089    }
090
091    /** {@inheritDoc} */
092    @Override
093    public Object getValueAt(int rowIndex, int columnIndex) {
094        if (rowIndex >= _symbols.size()) throw new IllegalArgumentException("Invalid row");
095
096        switch (columnIndex) {
097            case COLUMN_NAME:
098                return _symbols.get(rowIndex).getName();
099            case COLUMN_VALUE:
100                if (_symbolTable == null) return "";
101                return _symbolTable.getValue(_symbols.get(rowIndex).getName());
102            default:
103                throw new IllegalArgumentException("Invalid column");
104        }
105    }
106
107//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DebuggerSymbolTableModel.class);
108}