001package jmri.jmrit.sensorgroup;
002
003import java.beans.PropertyChangeListener;
004
005import jmri.*;
006
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Model for a simple Sensor JTable.
012 * <p>
013 * Note that this has a very expensive way of handling
014 * changes to the contents of the SensorManager: it recreates
015 * an entire local array instead of using the Manager's accessors.
016 *
017 * @author Bob Jacobsen Copyright (C) 2007
018 * @author Pete Cressman Copyright (C) 2009
019 *
020 */
021public class SensorTableModel extends BeanTableModel implements PropertyChangeListener {
022
023    // Rework this to use the ordered index from the manager
024    
025    String[] _sysNameList;
026    Boolean[] _includedSensors;
027
028    public SensorTableModel() {
029        init();
030        getManager().addPropertyChangeListener(this);
031    }
032
033    private void init() {
034        _sysNameList = new String[getManager().getObjectCount()];
035        _includedSensors = new Boolean[getManager().getObjectCount()];
036        int i = 0;
037        for (Sensor s : getManager().getNamedBeanSet()) {
038            _sysNameList[i] = s.getSystemName();
039            _includedSensors[i] = Boolean.FALSE;
040            i++;
041        }
042    }
043
044    public void dispose() {
045        getManager().removePropertyChangeListener(this);
046    }
047
048    @Override
049    public SensorManager getManager() {
050        return InstanceManager.sensorManagerInstance();
051    }
052
053    @Override
054    public int getRowCount() {
055        return _sysNameList.length;
056    }
057
058    @Override
059    public Object getValueAt(int r, int c) {
060        if (r >= _sysNameList.length) {
061            return null;
062        }
063        switch (c) {
064            case INCLUDE_COLUMN:  // expect to be overriden
065                return _includedSensors[r];
066            case SNAME_COLUMN:
067                return _sysNameList[r];
068            case UNAME_COLUMN:
069                return InstanceManager.sensorManagerInstance().provideSensor(_sysNameList[r]).getUserName();
070            default:
071                return super.getValueAt(r, c);
072        }
073    }
074
075    @Override
076    public void setValueAt(Object type, int r, int c) {
077        if (r > _sysNameList.length) {
078            return;
079        }
080        switch (c) {
081            case INCLUDE_COLUMN:
082                _includedSensors[r] = (Boolean) type;
083                return;
084            default:
085                log.warn("default hit in setValueAt");
086        }
087    }
088
089    @Override
090    public void propertyChange(java.beans.PropertyChangeEvent e) {
091        if (e.getPropertyName().equals("length")) {
092            // a new NamedBean is available in the manager
093            init();
094            fireTableDataChanged();
095        }
096    }
097
098    private final static Logger log = LoggerFactory.getLogger(SensorTableModel.class);
099
100}