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            int oldSize = _groupHashTable.size();
054            _groupHashTable.remove(name);
055            setDirtyAndFirePropertyChange(LISTLENGTH_CHANGED_PROPERTY, oldSize, _groupHashTable.size());
056        }
057    }
058
059    /**
060     * Get a Consist by name
061     *
062     * @param name string name for the Consist
063     *
064     * @return named Consist
065     */
066    public Consist getConsistByName(String name) {
067        return (Consist) _groupHashTable.get(name);
068    }
069
070    public void replaceConsistName(String oldName, String newName) {
071        Consist oldConsist = getConsistByName(oldName);
072        if (oldConsist != null) {
073            Consist newConsist = newConsist(newName);
074            // keep the lead engine
075            Engine leadEngine = oldConsist.getLead();
076            if (leadEngine != null) {
077                leadEngine.setConsist(newConsist);
078            }
079            for (Engine engine : oldConsist.getEngines()) {
080                engine.setConsist(newConsist);
081            }
082        }
083    }
084 
085    public void load(Element root) {
086        // new format using elements starting version 3.3.1
087        if (root.getChild(Xml.NEW_CONSISTS) != null) {
088            List<Element> eConsists = root.getChild(Xml.NEW_CONSISTS).getChildren(Xml.CONSIST);
089            log.debug("Consist manager sees {} consists", eConsists.size());
090            Attribute a;
091            for (Element eConsist : eConsists) {
092                if ((a = eConsist.getAttribute(Xml.NAME)) != null) {
093                    newConsist(a.getValue());
094                }
095            }
096        } // old format
097        else if (root.getChild(Xml.CONSISTS) != null) {
098            String names = root.getChildText(Xml.CONSISTS);
099            if (!names.isEmpty()) {
100                String[] consistNames = names.split("%%"); // NOI18N
101                log.debug("consists: {}", names);
102                for (String name : consistNames) {
103                    newConsist(name);
104                }
105            }
106        }
107    }
108
109    /**
110     * Create an XML element to represent this Entry. This member has to remain
111     * synchronized with the detailed DTD in operations-engines.dtd.
112     *
113     * @param root The common Element for operations-engines.dtd.
114     */
115    public void store(Element root) {
116        List<String> names = getNameList();
117        Element consists = new Element(Xml.NEW_CONSISTS);
118        for (String name : names) {
119            Element consist = new Element(Xml.CONSIST);
120            consist.setAttribute(new Attribute(Xml.NAME, name));
121            consists.addContent(consist);
122        }
123        root.addContent(consists);
124    }
125
126    protected void setDirtyAndFirePropertyChange(String p, Object old, Object n) {
127        // Set dirty
128        InstanceManager.getDefault(EngineManagerXml.class).setDirty(true);
129        super.firePropertyChange(p, old, n);
130    }
131
132    private static final Logger log = LoggerFactory.getLogger(ConsistManager.class);
133}