001package jmri.jmrit.beantable.routetable;
002
003import jmri.InstanceManager;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007import java.util.List;
008
009
010/**
011 * Set up table for selecting Sensors and Sensor State.
012 *
013 * Split from {@link jmri.jmrit.beantable.RouteTableAction}
014 *
015 * @author Dave Duchamp Copyright (C) 2004
016 * @author Bob Jacobsen Copyright (C) 2007
017 * @author Simon Reader Copyright (C) 2008
018 * @author Pete Cressman Copyright (C) 2009
019 * @author Egbert Broerse Copyright (C) 2016
020 * @author Paul Bender Copyright (C) 2020
021 */
022class RouteSensorModel extends RouteOutputModel {
023
024    private final AbstractRouteAddEditFrame routeAddFrame;
025
026    RouteSensorModel(AbstractRouteAddEditFrame routeAddFrame) {
027        this.routeAddFrame = routeAddFrame;
028        InstanceManager.sensorManagerInstance().addPropertyChangeListener(this);
029    }
030
031    @Override
032    public int getRowCount() {
033        if (routeAddFrame.isShowAll()) {
034            return routeAddFrame.get_sensorList().size();
035        } else {
036            return routeAddFrame.get_includedSensorList().size();
037        }
038    }
039
040    @Override
041    public Object getValueAt(int r, int c) {
042        List<RouteSensor> sensorList;
043        if (routeAddFrame.isShowAll()) {
044            sensorList = routeAddFrame.get_sensorList();
045        } else {
046            sensorList = routeAddFrame.get_includedSensorList();
047        }
048        // some error checking
049        if (r >= sensorList.size()) {
050            log.debug("row is greater than turnout list size");
051            return null;
052        }
053        switch (c) {
054            case INCLUDE_COLUMN:
055                return sensorList.get(r).isIncluded();
056            case SNAME_COLUMN:  // slot number
057                return sensorList.get(r).getSysName();
058            case UNAME_COLUMN:  //
059                return sensorList.get(r).getUserName();
060            case STATE_COLUMN:  //
061                return sensorList.get(r).getSetToState();
062            default:
063                return null;
064        }
065    }
066
067    @Override
068    public void setValueAt(Object type, int r, int c) {
069        List<RouteSensor> sensorList;
070        if (routeAddFrame.isShowAll()) {
071            sensorList = routeAddFrame.get_sensorList();
072        } else {
073            sensorList = routeAddFrame.get_includedSensorList();
074        }
075        switch (c) {
076            case INCLUDE_COLUMN:
077                sensorList.get(r).setIncluded(((Boolean) type));
078                break;
079            case STATE_COLUMN:
080                sensorList.get(r).setSetToState((String) type);
081                break;
082            default:
083                log.error("RouteSensorModel.setValueAt should not be called on column {}", c);
084        }
085    }
086
087    private static final Logger log = LoggerFactory.getLogger(RouteSensorModel.class);
088
089}