001package jmri.jmrit.sensorgroup;
002
003import javax.swing.table.AbstractTableModel;
004import jmri.Manager;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Abstract base for simple bean tables for insertion in other GUI elements
010 *
011 * @author Bob Jacobsen Copyright (C) 2007
012 */
013public abstract class BeanTableModel extends AbstractTableModel {
014
015    @Override
016    public Class<?> getColumnClass(int c) {
017        if (c == INCLUDE_COLUMN) {
018            return Boolean.class;
019        } else {
020            return String.class;
021        }
022    }
023
024    public abstract Manager<?> getManager();
025
026    @Override
027    public int getColumnCount() {
028        return INCLUDE_COLUMN + 1;
029    }
030
031    @Override
032    public int getRowCount() {
033        return getManager().getNamedBeanSet().size();
034    }
035
036    @Override
037    public boolean isCellEditable(int r, int c) {
038        return (c == INCLUDE_COLUMN);
039    }
040
041    public static final int SNAME_COLUMN = 0;
042    public static final int UNAME_COLUMN = 1;
043    public static final int INCLUDE_COLUMN = 2;
044
045    @Override
046    public String getColumnName(int c) {
047        switch (c) {
048            case SNAME_COLUMN:
049                return Bundle.getMessage("ColumnSystemName");
050            case UNAME_COLUMN:
051                return Bundle.getMessage("ColumnUserName");
052            case INCLUDE_COLUMN:
053                return Bundle.getMessage("ColumnIncluded");
054            default:
055                return "";
056        }
057    }
058
059    /**
060     * User name column must be handled by subclass.
061     * {@inheritDoc}
062     */
063    @Override
064    public Object getValueAt(int r, int c) {
065        switch (c) {
066            case SNAME_COLUMN:  // system name
067                int i=0;
068                for (jmri.NamedBean bean : getManager().getNamedBeanSet()) {
069                    if ( i==r ){
070                        return bean.getSystemName();
071                    }
072                    i++;
073                }
074                log.error("No system name for row {}",r);
075                return "";
076            default:
077                log.warn("getValueAt should not have been asked about c={}", c);
078                return null;
079        }
080    }
081    private final static Logger log = LoggerFactory.getLogger(BeanTableModel.class);
082}