001package jmri.jmrit.operations.rollingstock.cars;
002
003import java.util.Comparator;
004
005import org.jdom2.Element;
006
007import jmri.InstanceManager;
008import jmri.InstanceManagerAutoDefault;
009import jmri.jmrit.operations.rollingstock.RollingStockAttribute;
010import jmri.jmrit.operations.setup.Control;
011
012/**
013 * Represents the lengths that cars can have.
014 *
015 * @author Daniel Boudreau Copyright (C) 2008, 2014
016 */
017public class CarLengths extends RollingStockAttribute implements InstanceManagerAutoDefault {
018
019    private static final String LENGTHS = Bundle.getMessage("carLengths");
020    public static final String CARLENGTHS_CHANGED_PROPERTY = "CarLengths"; // NOI18N
021    public static final String CARLENGTHS_NAME_CHANGED_PROPERTY = "CarLengthsName"; // NOI18N
022
023    protected static final int CAR_MIN_NAME_LENGTH = Control.max_len_string_length_name;
024
025    public CarLengths() {
026    }
027
028    @Override
029    protected String getDefaultNames() {
030        return LENGTHS;
031    }
032
033    // override, need to perform a number sort
034    @Override
035    public void setNames(String[] lengths) {
036        setValues(lengths);
037    }
038
039    @Override
040    public void addName(String length) {
041        super.addName(length);
042        setDirtyAndFirePropertyChange(CARLENGTHS_CHANGED_PROPERTY, null, length);
043    }
044
045    @Override
046    public void deleteName(String length) {
047        super.deleteName(length);
048        setDirtyAndFirePropertyChange(CARLENGTHS_CHANGED_PROPERTY, length, null);
049    }
050
051    public void replaceName(String oldName, String newName) {
052        super.addName(newName);
053        setDirtyAndFirePropertyChange(CARLENGTHS_NAME_CHANGED_PROPERTY, oldName, newName);
054        // need to keep old name so location manager can replace properly
055        super.deleteName(oldName);
056    }
057
058    @Override
059    public void sort() {
060        list.sort(Comparator.comparingInt(Integer::parseInt));
061    }
062
063    @Override
064    protected int getMinNameLength() {
065        return CAR_MIN_NAME_LENGTH;
066    }
067
068    /**
069     * Create an XML element to represent this Entry. This member has to remain
070     * synchronized with the detailed DTD in operations-cars.dtd.
071     * @param root The common Element for operations-cars.dtd.
072     *
073     */
074    public void store(Element root) {
075        store(root, Xml.LENGTHS, Xml.LENGTH);
076    }
077
078    public void load(Element root) {
079        load(root, Xml.LENGTHS, Xml.LENGTH, Xml.CAR_LENGTHS);
080    }
081
082    protected void setDirtyAndFirePropertyChange(String p, Object old, Object n) {
083        // Set dirty
084        InstanceManager.getDefault(CarManagerXml.class).setDirty(true);
085        super.firePropertyChange(p, old, n);
086    }
087
088//    private final static Logger log = LoggerFactory.getLogger(CarLengths.class);
089
090}