001package jmri.jmrit.operations.rollingstock.cars;
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 Kernels.
016 *
017 * @author Daniel Boudreau Copyright (C) 2021
018 */
019public class KernelManager extends RollingStockGroupManager implements InstanceManagerAutoDefault {
020
021    public KernelManager() {
022    }
023
024    /**
025     * Create a new Kernel
026     *
027     * @param name string name for this Kernel
028     *
029     * @return Kernel
030     */
031    public Kernel newKernel(String name) {
032        Kernel kernel = getKernelByName(name);
033        if (kernel == null && !name.equals(NONE)) {
034            kernel = new Kernel(name);
035            int oldSize = _groupHashTable.size();
036            _groupHashTable.put(name, kernel);
037            setDirtyAndFirePropertyChange(LISTLENGTH_CHANGED_PROPERTY, oldSize, _groupHashTable
038                    .size());
039        }
040        return kernel;
041    }
042
043    /**
044     * Delete a Kernel by name
045     *
046     * @param name string name for the Kernel
047     *
048     */
049    public void deleteKernel(String name) {
050        Kernel kernel = getKernelByName(name);
051        if (kernel != null) {
052            kernel.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 Kernel by name
062     *
063     * @param name string name for the Kernel
064     *
065     * @return named Kernel
066     */
067    public Kernel getKernelByName(String name) {
068        return (Kernel) _groupHashTable.get(name);
069    }
070
071    public void replaceKernelName(String oldName, String newName) {
072        Kernel oldKernel = getKernelByName(oldName);
073        if (oldKernel != null) {
074            Kernel newKernel = newKernel(newName);
075            // keep the lead car
076            Car leadCar = oldKernel.getLead();
077            if (leadCar != null) {
078                leadCar.setKernel(newKernel);
079            }
080            for (Car car : oldKernel.getCars()) {
081                car.setKernel(newKernel);
082            }
083        }
084    }
085
086 
087    public void load(Element root) {
088        // new format using elements starting version 3.3.1
089        if (root.getChild(Xml.NEW_KERNELS) != null) {
090            List<Element> eKernels = root.getChild(Xml.NEW_KERNELS).getChildren(Xml.KERNEL);
091            log.debug("Kernel manager sees {} kernels", eKernels.size());
092            Attribute a;
093            for (Element eKernel : eKernels) {
094                if ((a = eKernel.getAttribute(Xml.NAME)) != null) {
095                    newKernel(a.getValue());
096                }
097            }
098        } // old format
099        else if (root.getChild(Xml.KERNELS) != null) {
100            String names = root.getChildText(Xml.KERNELS);
101            if (!names.isEmpty()) {
102                String[] kernelNames = names.split("%%"); // NOI18N
103                log.debug("kernels: {}", names);
104                for (String name : kernelNames) {
105                    newKernel(name);
106                }
107            }
108        }
109    }
110
111    /**
112     * Create an XML element to represent this Entry. This member has to remain
113     * synchronized with the detailed DTD in operations-cars.dtd.
114     *
115     * @param root The common Element for operations-cars.dtd.
116     */
117    public void store(Element root) {
118        List<String> names = getNameList();
119        Element kernels = new Element(Xml.NEW_KERNELS);
120        for (String name : names) {
121            Element kernel = new Element(Xml.KERNEL);
122            kernel.setAttribute(new Attribute(Xml.NAME, name));
123            kernels.addContent(kernel);
124        }
125        root.addContent(kernels);
126    }
127
128    protected void setDirtyAndFirePropertyChange(String p, Object old, Object n) {
129        // Set dirty
130        InstanceManager.getDefault(CarManagerXml.class).setDirty(true);
131        super.firePropertyChange(p, old, n);
132    }
133
134    private final static Logger log = LoggerFactory.getLogger(KernelManager.class);
135}