001package jmri.jmrit.consisttool;
002
003import javax.swing.JButton;
004import javax.swing.JTable;
005import javax.swing.table.AbstractTableModel;
006import javax.swing.table.TableCellEditor;
007import javax.swing.table.TableColumnModel;
008import jmri.Consist;
009import jmri.ConsistManager;
010import jmri.DccLocoAddress;
011import jmri.InstanceManager;
012import jmri.util.table.ButtonEditor;
013import jmri.util.table.ButtonRenderer;
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017/**
018 * Table data model for display of consist information.
019 *
020 * @author Paul Bender Copyright (c) 2004-2005
021 */
022public class ConsistDataModel extends AbstractTableModel {
023
024    private static final int ADDRCOLUMN = 0;    // Locomotive address
025    private static final int ROSTERCOLUMN = 1;  // Roster Entry, this exists
026    private static final int DIRECTIONCOLUMN = 2;  // Relative Direction
027    private static final int DELCOLUMN = 3;     // Remove Button
028    private static final int NUMCOLUMN = 4;
029    // a place holder for a consist and Consist Manager objects.
030    private Consist _consist = null;
031    private ConsistManager consistMan = null;
032
033    // Construct a new instance
034    ConsistDataModel() {
035        consistMan = InstanceManager.getDefault(jmri.ConsistManager.class);
036    }
037
038    void initTable(JTable ConsistTable) {
039        // Install the button handlers for the Delete Buttons
040        TableColumnModel tcm = ConsistTable.getColumnModel();
041        ButtonRenderer buttonRenderer = new ButtonRenderer();
042        tcm.getColumn(DELCOLUMN).setCellRenderer(buttonRenderer);
043        TableCellEditor buttonEditor = new ButtonEditor(new JButton());
044        tcm.getColumn(DELCOLUMN).setCellEditor(buttonEditor);
045    }
046
047    public void setConsist(Consist consist) {
048        log.debug("Setting Consist");
049        _consist = consist;
050        fireTableDataChanged();
051    }
052
053    public void setConsist(DccLocoAddress Address) {
054        log.debug("Setting Consist using address: {}",Address);
055        _consist = consistMan.getConsist(Address);
056        fireTableDataChanged();
057    }
058
059    public Consist getConsist() {
060        return _consist;
061    }
062
063    @Override
064    public int getRowCount() {
065        try {
066            return (_consist.getConsistList().size());
067        } catch (NullPointerException e) {
068            return (0);
069        }
070    }
071
072    @Override
073    public int getColumnCount() {
074        return NUMCOLUMN;
075    }
076
077    @Override
078    public String getColumnName(int col) {
079        switch (col) {
080            case ADDRCOLUMN:
081                return Bundle.getMessage("AddressColumnLabel");
082            case ROSTERCOLUMN:
083                return Bundle.getMessage("RosterColumnLabel");
084            case DIRECTIONCOLUMN:
085                return Bundle.getMessage("DirectionColumnLabel");
086            default:
087                return "";
088        }
089    }
090
091    @Override
092    public Class<?> getColumnClass(int col) {
093        switch (col) {
094            case ROSTERCOLUMN:
095                return (String.class);
096            case DELCOLUMN:
097                return (JButton.class);
098            case DIRECTIONCOLUMN:
099                return (Boolean.class);
100            default:
101                return (String.class);
102        }
103    }
104
105    @Override
106    public boolean isCellEditable(int row, int col) {
107        log.debug("isCellEditable called for row: {} column: {}",row,col);
108        if (col == DELCOLUMN) {
109            return (true);
110        } else if (row != 0 && col == DIRECTIONCOLUMN) {
111            return (true);
112        } else {
113            return (false);
114        }
115    }
116
117    @Override
118    public Object getValueAt(int row, int col) {
119        log.debug("getValueAt called for row: {} column: {}",row,col);
120        if (_consist == null) {
121            log.debug("Consist not defined");
122            return (null);
123        }
124        // some error checking
125        if (row >= _consist.getConsistList().size()) {
126            log.debug("row is greater than consist list size");
127            return null;
128        }
129        switch (col) {
130            case ADDRCOLUMN:
131                return (_consist.getConsistList().get(row).toString());
132            case ROSTERCOLUMN:
133                return _consist.getRosterId(_consist.getConsistList().get(row));
134            case DIRECTIONCOLUMN:
135                return (Boolean.valueOf(_consist.getLocoDirection(_consist.getConsistList().get(row))));
136            case DELCOLUMN:
137                return Bundle.getMessage("ButtonDelete");
138            default:
139                return ("");
140        }
141    }
142
143    @Override
144    public void setValueAt(Object value, int row, int col) {
145        log.debug("setValueAt called for row: {} column: {}",row,col);
146        if (_consist == null) {
147            return;
148        }
149        switch (col) {
150            case DIRECTIONCOLUMN:
151                _consist.add(_consist.getConsistList().get(row), ((Boolean) value).booleanValue());
152                fireTableDataChanged();
153                break;
154            case DELCOLUMN:
155                log.debug("Delete Called for row {}",row);
156                fireTableRowsDeleted(row, row);
157                _consist.remove(_consist.getConsistList().get(row));
158                fireTableDataChanged();
159                break;
160            default:
161                log.error("Unknown Consist Operation");
162        }
163    }
164    private static final Logger log = LoggerFactory.getLogger(ConsistDataModel.class);
165}