001package jmri.jmrit.withrottle;
002
003/**
004 *
005 * WiThrottle
006 *
007 * @author Brett Hoffman Copyright (C) 2009
008 * @author Created by Brett Hoffman on:
009 * @author 11/11/09.
010 */
011import java.util.ArrayList;
012import java.util.ResourceBundle;
013import javax.swing.table.AbstractTableModel;
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017public class WiThrottlesListModel extends AbstractTableModel {
018
019    ArrayList<DeviceServer> deviceList;
020    //DeviceServer[] deviceList;
021
022    static final ResourceBundle rb = ResourceBundle.getBundle("jmri.jmrit.withrottle.WiThrottleBundle");
023
024    WiThrottlesListModel(ArrayList<DeviceServer> deviceList) {
025
026        this.deviceList = deviceList;
027
028    }
029
030    @Override
031    public int getColumnCount() {
032        return 3;
033    }
034
035    @Override
036    public int getRowCount() {
037        return deviceList.size();
038    }
039
040    /**
041     * Added column LabelRosterId since 4.15.4. 
042     */
043    @Override
044    public String getColumnName(int col) {
045        String title;
046        switch (col) {
047            case 0: {
048                title = rb.getString("LabelDeviceName");
049                break;
050            }
051            case 1: {
052                title = rb.getString("LabelAddress");
053                break;
054            }
055            case 2: {
056                title = rb.getString("LabelRosterId");
057                break;
058            }
059            default: {
060                title = "";
061            }
062        }
063        return title;
064    }
065
066    @Override
067    public String getValueAt(int row, int col) {
068        if (deviceList.size() < 1) {
069            return null;
070        }
071        // some error checking
072        if (row >= deviceList.size()) {
073            log.debug("row is greater than device list size");
074            return null;
075        }
076        if (col == 0) {
077            return deviceList.get(row).getName();
078        } else if (col == 1) {
079            return deviceList.get(row).getCurrentAddressString();
080        } else {
081            return deviceList.get(row).getCurrentRosterIdString();
082        }
083    }
084
085    public void updateDeviceList(ArrayList<DeviceServer> deviceList) {
086        this.deviceList = deviceList;
087        this.fireTableDataChanged();
088    }
089
090    private final static Logger log = LoggerFactory.getLogger(WiThrottlesListModel.class);
091}