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