001package jmri.jmrit.timetable;
002
003/**
004 * Define the content of a Station record.
005 *
006 * @author Dave Sand Copyright (C) 2018
007 */
008public class Station {
009
010    /**
011     * Create a new station with default values.
012     * @param segmentId The parent segment id.
013     * @throws IllegalArgumentException STATION_ADD_FAIL
014     */
015    public Station(int segmentId) {
016        if (_dm.getSegment(segmentId) == null) {
017            throw new IllegalArgumentException(TimeTableDataManager.STATION_ADD_FAIL);
018        }
019        _stationId = _dm.getNextId("Station");  // NOI18N
020        _segmentId = segmentId;
021        _dm.addStation(_stationId, this);
022    }
023
024    public Station(int stationId, int segmentId, String stationName, double distance, boolean doubleTrack, int sidings, int staging) {
025        _stationId = stationId;
026        _segmentId = segmentId;
027        setStationName(stationName);
028        setDistance(distance);
029        setDoubleTrack(doubleTrack);
030        setSidings(sidings);
031        setStaging(staging);
032    }
033
034    TimeTableDataManager _dm = TimeTableDataManager.getDataManager();
035
036    private final int _stationId;
037    private final int _segmentId;
038    private String _stationName = Bundle.getMessage("NewStationName");  // NOI18N
039    private double _distance = 1.0;
040    private boolean _doubleTrack = false;
041    private int _sidings = 0;
042    private int _staging = 0;
043
044    /**
045     * Make a copy of the station.
046     * @param segmentId The new segmentId, if zero use the current segment id.
047     * @return a new station instance.
048     */
049    public Station getCopy(int segmentId) {
050        if (segmentId == 0) segmentId = getSegmentId();
051        Station copy = new Station(segmentId);
052        copy.setStationName(Bundle.getMessage("DuplicateCopyName", _stationName));
053        copy.setDistance(_distance);
054        copy.setDoubleTrack(_doubleTrack);
055        copy.setSidings(_sidings);
056        copy.setStaging(_staging);
057        return copy;
058    }
059
060    public int getStationId() {
061        return _stationId;
062    }
063
064    public int getSegmentId() {
065        return _segmentId;
066    }
067
068    public String getStationName() {
069        return _stationName;
070    }
071
072    public void setStationName(String newName) {
073        _stationName = newName;
074    }
075
076    public double getDistance() {
077        return _distance;
078    }
079
080    /**
081     * Set a new distance.
082     * @param newDistance The value to be used.
083     * @throws IllegalArgumentException (DISTANCE_LT_0) if the value is less than 0.0.
084     */
085    public void setDistance(double newDistance) {
086        if (newDistance < 0) {
087            throw new IllegalArgumentException(TimeTableDataManager.DISTANCE_LT_0);
088        }
089        double oldDistance = _distance;
090        _distance = newDistance;
091
092        try {
093            int layoutId = _dm.getSegment(getSegmentId()).getLayoutId();
094            _dm.calculateLayoutTrains(layoutId, false);
095            _dm.calculateLayoutTrains(layoutId, true);
096        } catch (IllegalArgumentException ex) {
097            _distance = oldDistance;  // Roll back distance change
098            throw ex;
099        }
100    }
101
102    public boolean getDoubleTrack() {
103        return _doubleTrack;
104    }
105
106    public void setDoubleTrack(boolean newDoubleTrack) {
107        _doubleTrack = newDoubleTrack;
108    }
109
110    public int getSidings() {
111        return _sidings;
112    }
113
114    /**
115     * Set a new siding count.
116     * @param newSidings The value to be used.
117     * @throws IllegalArgumentException (SIDINGS_LT_0) if the value is less than 0.
118     */
119    public void setSidings(int newSidings) {
120        if (newSidings < 0) {
121            throw new IllegalArgumentException(TimeTableDataManager.SIDINGS_LT_0);
122        }
123        _sidings = newSidings;
124    }
125
126    public int getStaging() {
127        return _staging;
128    }
129
130    /**
131     * Set a new staging track count.
132     * @param newStaging The value to be used.
133     * @throws IllegalArgumentException (STAGING_LT_0, STAGING_IN_USE) if the value is
134     * less than 0 or a staging track is referenced by a train stop.
135     */
136    public void setStaging(int newStaging) {
137        if (newStaging < 0) {
138            throw new IllegalArgumentException(TimeTableDataManager.STAGING_LT_0);
139        }
140        for (Stop stop : _dm.getStops(0, getStationId(), false)) {
141            if (stop.getStagingTrack() > newStaging) {
142                throw new IllegalArgumentException(TimeTableDataManager.STAGING_IN_USE);
143            }
144        }
145        _staging = newStaging;
146    }
147
148    @Override
149    public String toString() {
150        return _stationName;
151    }
152
153//     private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Station.class);
154}