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