001package jmri.jmrit.timetable;
002
003import java.awt.Color;
004import jmri.util.swing.JmriColorChooser;
005/**
006 * Define the content of a Train Type record.
007 *
008 * @author Dave Sand Copyright (C) 2018
009 */
010public class TrainType {
011
012    /**
013     * Create a new train type with default values.
014     * @param layoutId The parent layout id.
015     * @throws IllegalArgumentException TYPE_ADD_FAIL
016     */
017    public TrainType(int layoutId) {
018        if (_dm.getLayout(layoutId) == null) {
019            throw new IllegalArgumentException(TimeTableDataManager.TYPE_ADD_FAIL);
020        }
021        _typeId = _dm.getNextId("TrainType");  // NOI18N
022        _layoutId = layoutId;
023        _dm.addTrainType(_typeId, this);
024    }
025
026    public TrainType(int typeId, int layoutId, String typeName, String typeColor) {
027        _typeId = typeId;
028        _layoutId = layoutId;
029        setTypeName(typeName);
030        setTypeColor(typeColor);
031    }
032
033    TimeTableDataManager _dm = TimeTableDataManager.getDataManager();
034
035    private final int _typeId;
036    private final int _layoutId;
037    private String _typeName = Bundle.getMessage("NewTypeName");  // NOI18N
038    private String _typeColor = "#000000";  // NOI18N
039
040    /**
041     * Make a copy of the train type.
042     * @param layoutId The new layoutId, if zero use the current layout id.
043     * @return a new train type instance.
044     */
045    public TrainType getCopy(int layoutId) {
046        if (layoutId == 0) layoutId = getLayoutId();
047        TrainType copy = new TrainType(layoutId);
048        copy.setTypeName(Bundle.getMessage("DuplicateCopyName", _typeName));
049        copy.setTypeColor(_typeColor);
050        return copy;
051    }
052
053    public int getTypeId() {
054        return _typeId;
055    }
056
057    public int getLayoutId() {
058        return _layoutId;
059    }
060
061    public String getTypeName() {
062        return _typeName;
063    }
064
065    public void setTypeName(String newName) {
066        _typeName = newName;
067    }
068
069    public String getTypeColor() {
070        return _typeColor;
071    }
072
073    public void setTypeColor(String newColor) {
074        _typeColor = newColor;
075        JmriColorChooser.addRecentColor(Color.decode(newColor));
076    }
077
078    @Override
079    public String toString() {
080        return _typeName;
081    }
082
083//     private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TrainType.class);
084}