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