001package jmri.jmrit.timetable;
002
003/**
004 * Define the content of a Train record.
005 *
006 * @author Dave Sand Copyright (C) 2018
007 */
008public class Train {
009
010    /**
011     * Create a new train with default values.
012     * @param scheduleId The parent schedule id.
013     * @throws IllegalArgumentException TRAIN_ADD_FAIL
014     */
015    public Train(int scheduleId) {
016        if (_dm.getSchedule(scheduleId) == null) {
017            throw new IllegalArgumentException(TimeTableDataManager.TRAIN_ADD_FAIL);
018        }
019        _trainId = _dm.getNextId("Train");  // NOI18N
020        _scheduleId = scheduleId;
021        _dm.addTrain(_trainId, this);
022    }
023
024    public Train(int trainId, int scheduleId, int typeId, String trainName, String trainDesc,
025                int defaultSpeed, int startTime, int throttle, int routeDuration, String trainNotes) {
026        _trainId = trainId;
027        _scheduleId = scheduleId;
028        setTypeId(typeId);
029        setTrainName(trainName);
030        setTrainDesc(trainDesc);
031        setDefaultSpeed(defaultSpeed);
032        setStartTime(startTime);
033        setThrottle(throttle);
034        setRouteDuration(routeDuration);
035        setTrainNotes(trainNotes);
036    }
037
038    TimeTableDataManager _dm = TimeTableDataManager.getDataManager();
039
040    private final int _trainId;
041    private final int _scheduleId;
042    private int _typeId = 0;
043    private String _trainName = Bundle.getMessage("NewTrainName");  // NOI18N
044    private String _trainDesc = Bundle.getMessage("NewTrainDesc");  // NOI18N
045    private int _defaultSpeed = 1;
046    private int _startTime = 0;
047    private int _throttle = 0;
048    private int _routeDuration = 0;
049    private String _trainNotes = "";
050
051    /**
052     * Make a copy of the train.
053     * @param schedId The new schedule id, if zero use the current schedule id.
054     * @param typeId The new train type id.  If zero use the current train type id.
055     * @return a new Train instance.
056     */
057    public Train getCopy(int schedId, int typeId) {
058        if (schedId == 0) schedId = getScheduleId();
059        if (typeId == 0) typeId = getTypeId();
060
061        Train copy = new Train(schedId);
062        copy.setTypeId(typeId);
063        copy.setTrainName(Bundle.getMessage("DuplicateCopyName", _trainName));
064        copy.setTrainDesc(Bundle.getMessage("DuplicateCopyName", _trainDesc));
065        copy.setDefaultSpeed(_defaultSpeed);
066        copy.setStartTime(_startTime);
067        copy.setThrottle(_throttle);
068        copy.setRouteDuration(_routeDuration);
069        copy.setTrainNotes(_trainNotes);
070        return copy;
071    }
072
073    public int getTrainId() {
074        return _trainId;
075    }
076
077    public int getScheduleId() {
078        return _scheduleId;
079    }
080
081    public int getTypeId() {
082        return _typeId;
083    }
084
085    public void setTypeId(int newType) {
086        _typeId = newType;
087    }
088
089    public String getTrainName() {
090        return _trainName;
091    }
092
093    public void setTrainName(String newName) {
094        _trainName = newName;
095    }
096
097    public String getTrainDesc() {
098        return _trainDesc;
099    }
100
101    public void setTrainDesc(String newDesc) {
102        _trainDesc = newDesc;
103    }
104
105    public int getDefaultSpeed() {
106        return _defaultSpeed;
107    }
108
109    public void setDefaultSpeed(int newSpeed) {
110        if (newSpeed < 0) {
111            throw new IllegalArgumentException(TimeTableDataManager.DEFAULT_SPEED_LT_0);
112        }
113        int oldSpeed = _defaultSpeed;
114        _defaultSpeed = newSpeed;
115
116        try {
117            _dm.calculateTrain(_trainId, false);
118            _dm.calculateTrain(_trainId, true);
119        } catch (IllegalArgumentException ex) {
120            _defaultSpeed = oldSpeed;  // Roll back default speed change
121            throw ex;
122        }
123    }
124
125    public int getStartTime() {
126        return _startTime;
127    }
128
129    public void setStartTime(int newStartTime) {
130        Schedule schedule = _dm.getSchedule(_scheduleId);
131        if (!_dm.validateTime(schedule.getStartHour(), schedule.getDuration(), newStartTime)) {
132            throw new IllegalArgumentException(String.format("%s~%d~%d",  // NOI18N
133                    TimeTableDataManager.START_TIME_RANGE, schedule.getStartHour(), schedule.getStartHour() + schedule.getDuration()));
134        }
135        int oldStartTime = _startTime;
136        _startTime = newStartTime;
137
138        try {
139            _dm.calculateTrain(_trainId, false);
140            _dm.calculateTrain(_trainId, true);
141        } catch (IllegalArgumentException ex) {
142            _startTime = oldStartTime;  // Roll back start time change
143            throw ex;
144        }
145    }
146
147    public int getThrottle() {
148        return _throttle;
149    }
150
151    public void setThrottle(int newThrottle) {
152        Layout layout = _dm.getLayout(_dm.getSchedule(_scheduleId).getLayoutId());
153        if (newThrottle < 0 || newThrottle > layout.getThrottles()) {
154            throw new IllegalArgumentException(TimeTableDataManager.THROTTLE_RANGE);
155        }
156
157        _throttle = newThrottle;
158    }
159
160    public int getRouteDuration() {
161        return _routeDuration;
162    }
163
164    public void setRouteDuration(int newRouteDuration) {
165        _routeDuration = newRouteDuration;
166    }
167
168    public String getTrainNotes() {
169        return _trainNotes;
170    }
171
172    public void setTrainNotes(String newNotes) {
173        _trainNotes = newNotes;
174    }
175
176    @Override
177    public String toString() {
178        return _trainName;
179    }
180
181//     private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Train.class);
182}