001package jmri.jmrit.operations.rollingstock.engines;
002
003import java.util.List;
004
005import org.jdom2.Attribute;
006import org.jdom2.Element;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010import jmri.InstanceManager;
011import jmri.InstanceManagerAutoDefault;
012import jmri.jmrit.operations.rollingstock.RollingStockGroupManager;
013
014/**
015 * Manages Consists.
016 *
017 * @author Daniel Boudreau Copyright (C) 2021
018 */
019public class ConsistManager extends RollingStockGroupManager implements InstanceManagerAutoDefault {
020
021    public ConsistManager() {
022    }
023
024    /**
025     * Create a new Consist
026     *
027     * @param name string name for this Consist
028     *
029     * @return Consist
030     */
031    public Consist newConsist(String name) {
032        Consist consist = getConsistByName(name);
033        if (consist == null && !name.equals(NONE)) {
034            consist = new Consist(name);
035            int oldSize = _groupHashTable.size();
036            _groupHashTable.put(name, consist);
037            setDirtyAndFirePropertyChange(LISTLENGTH_CHANGED_PROPERTY, oldSize, _groupHashTable
038                    .size());
039        }
040        return consist;
041    }
042
043    /**
044     * Delete a Consist by name
045     *
046     * @param name string name for the Consist
047     *
048     */
049    public void deleteConsist(String name) {
050        Consist consist = getConsistByName(name);
051        if (consist != null) {
052            consist.dispose();
053            Integer oldSize = Integer.valueOf(_groupHashTable.size());
054            _groupHashTable.remove(name);
055            setDirtyAndFirePropertyChange(LISTLENGTH_CHANGED_PROPERTY, oldSize, Integer.valueOf(_groupHashTable
056                    .size()));
057        }
058    }
059
060    /**
061     * Get a Consist by name
062     *
063     * @param name string name for the Consist
064     *
065     * @return named Consist
066     */
067    public Consist getConsistByName(String name) {
068        return (Consist) _groupHashTable.get(name);
069    }
070
071    public void replaceConsistName(String oldName, String newName) {
072        Consist oldConsist = getConsistByName(oldName);
073        if (oldConsist != null) {
074            Consist newConsist = newConsist(newName);
075            // keep the lead engine
076            Engine leadEngine = oldConsist.getLead();
077            if (leadEngine != null) {
078                leadEngine.setConsist(newConsist);
079            }
080            for (Engine engine : oldConsist.getEngines()) {
081                engine.setConsist(newConsist);
082            }
083        }
084    }
085 
086    public void load(Element root) {
087        // new format using elements starting version 3.3.1
088        if (root.getChild(Xml.NEW_CONSISTS) != null) {
089            List<Element> eConsists = root.getChild(Xml.NEW_CONSISTS).getChildren(Xml.CONSIST);
090            log.debug("Consist manager sees {} consists", eConsists.size());
091            Attribute a;
092            for (Element eConsist : eConsists) {
093                if ((a = eConsist.getAttribute(Xml.NAME)) != null) {
094                    newConsist(a.getValue());
095                }
096            }
097        } // old format
098        else if (root.getChild(Xml.CONSISTS) != null) {
099            String names = root.getChildText(Xml.CONSISTS);
100            if (!names.isEmpty()) {
101                String[] consistNames = names.split("%%"); // NOI18N
102                log.debug("consists: {}", names);
103                for (String name : consistNames) {
104                    newConsist(name);
105                }
106            }
107        }
108    }
109
110    /**
111     * Create an XML element to represent this Entry. This member has to remain
112     * synchronized with the detailed DTD in operations-engines.dtd.
113     *
114     * @param root The common Element for operations-engines.dtd.
115     */
116    public void store(Element root) {
117        List<String> names = getNameList();
118        Element consists = new Element(Xml.NEW_CONSISTS);
119        for (String name : names) {
120            Element consist = new Element(Xml.CONSIST);
121            consist.setAttribute(new Attribute(Xml.NAME, name));
122            consists.addContent(consist);
123        }
124        root.addContent(consists);
125    }
126
127    protected void setDirtyAndFirePropertyChange(String p, Object old, Object n) {
128        // Set dirty
129        InstanceManager.getDefault(EngineManagerXml.class).setDirty(true);
130        super.firePropertyChange(p, old, n);
131    }
132
133    private final static Logger log = LoggerFactory.getLogger(ConsistManager.class);
134}