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