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 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 ConsistTableRosterEntryColumnCellRenderer rosterEntryRenderer = new ConsistTableRosterEntryColumnCellRenderer(); 046 tcm.getColumn(ROSTERCOLUMN).setCellRenderer(rosterEntryRenderer); 047 } 048 049 public void setConsist(Consist consist) { 050 log.debug("Setting Consist"); 051 _consist = consist; 052 fireTableDataChanged(); 053 } 054 055 public void setConsist(DccLocoAddress Address) { 056 log.debug("Setting Consist using address: {}",Address); 057 _consist = consistMan.getConsist(Address); 058 fireTableDataChanged(); 059 } 060 061 public Consist getConsist() { 062 return _consist; 063 } 064 065 @Override 066 public int getRowCount() { 067 try { 068 return (_consist.getConsistList().size()); 069 } catch (NullPointerException e) { 070 return (0); 071 } 072 } 073 074 @Override 075 public int getColumnCount() { 076 return NUMCOLUMN; 077 } 078 079 @Override 080 public String getColumnName(int col) { 081 switch (col) { 082 case ADDRCOLUMN: 083 return Bundle.getMessage("AddressColumnLabel"); 084 case ROSTERCOLUMN: 085 return Bundle.getMessage("RosterColumnLabel"); 086 case DIRECTIONCOLUMN: 087 return Bundle.getMessage("DirectionColumnLabel"); 088 default: 089 return ""; 090 } 091 } 092 093 @Override 094 public Class<?> getColumnClass(int col) { 095 switch (col) { 096 case ROSTERCOLUMN: 097 return (String.class); 098 case DELCOLUMN: 099 return (JButton.class); 100 case DIRECTIONCOLUMN: 101 return (Boolean.class); 102 default: 103 return (String.class); 104 } 105 } 106 107 @Override 108 public boolean isCellEditable(int row, int col) { 109 log.debug("isCellEditable called for row: {} column: {}",row,col); 110 if (col == DELCOLUMN) { 111 return (true); 112 } else if (row != 0 && col == DIRECTIONCOLUMN) { 113 return (true); 114 } else { 115 return (false); 116 } 117 } 118 119 @Override 120 public Object getValueAt(int row, int col) { 121 log.debug("getValueAt called for row: {} column: {}",row,col); 122 if (_consist == null) { 123 log.debug("Consist not defined"); 124 return (null); 125 } 126 // some error checking 127 if (row >= _consist.getConsistList().size()) { 128 log.debug("row is greater than consist list size"); 129 return null; 130 } 131 switch (col) { 132 case ADDRCOLUMN: 133 return (_consist.getConsistList().get(row).toString()); 134 case ROSTERCOLUMN: 135 return _consist.getRosterId(_consist.getConsistList().get(row)); 136 case DIRECTIONCOLUMN: 137 return (Boolean.valueOf(_consist.getLocoDirection(_consist.getConsistList().get(row)))); 138 case DELCOLUMN: 139 return Bundle.getMessage("ButtonDelete"); 140 default: 141 return (""); 142 } 143 } 144 145 @Override 146 public void setValueAt(Object value, int row, int col) { 147 log.debug("setValueAt called for row: {} column: {}",row,col); 148 if (_consist == null) { 149 return; 150 } 151 switch (col) { 152 case DIRECTIONCOLUMN: 153 _consist.add(_consist.getConsistList().get(row), ((Boolean) value).booleanValue()); 154 fireTableDataChanged(); 155 consistMan.notifyConsistListChanged(); 156 break; 157 case DELCOLUMN: 158 log.debug("Delete Called for row {}",row); 159 fireTableRowsDeleted(row, row); 160 _consist.remove(_consist.getConsistList().get(row)); 161 fireTableDataChanged(); 162 consistMan.notifyConsistListChanged(); 163 break; 164 default: 165 log.error("Unknown Consist Operation"); 166 } 167 } 168 private static final Logger log = LoggerFactory.getLogger(ConsistDataModel.class); 169}