001package jmri.jmrit.operations.rollingstock;
002
003import java.util.*;
004
005import javax.swing.JComboBox;
006
007import jmri.beans.PropertyChangeSupport;
008
009/**
010 * 
011 *
012 * @author Daniel Boudreau Copyright (C) 2021
013 */
014public abstract class RollingStockGroupManager extends PropertyChangeSupport {
015
016    public static final String NONE = "";
017
018    protected Hashtable<String, RollingStockGroup<?>> _groupHashTable = new Hashtable<>();
019
020    public static final String LISTLENGTH_CHANGED_PROPERTY = "GroupListLengthChanged"; // NOI18N
021
022    public RollingStockGroupManager() {
023    }
024
025    /**
026     * Get a comboBox loaded with current group names
027     *
028     * @return comboBox with group names.
029     */
030    public JComboBox<String> getComboBox() {
031        JComboBox<String> box = new JComboBox<>();
032        box.addItem(NONE);
033        for (String name : getNameList()) {
034            box.addItem(name);
035        }
036        return box;
037    }
038
039    /**
040     * Update an existing comboBox with the current kernel names
041     *
042     * @param box comboBox requesting update
043     */
044    public void updateComboBox(JComboBox<String> box) {
045        box.removeAllItems();
046        box.addItem(NONE);
047        for (String name : getNameList()) {
048            box.addItem(name);
049        }
050    }
051
052    /**
053     * Get a list of group names
054     *
055     * @return ordered list of group names
056     */
057    public List<String> getNameList() {
058        List<String> out = new ArrayList<>();
059        Enumeration<String> en = _groupHashTable.keys();
060        while (en.hasMoreElements()) {
061            out.add(en.nextElement());
062        }
063        Collections.sort(out);
064        return out;
065    }
066
067    public int getMaxNameLength() {
068        int maxLength = 0;
069        for (String name : getNameList()) {
070            if (name.length() > maxLength) {
071                maxLength = name.length();
072            }
073        }
074        return maxLength;
075    }
076}