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