001package jmri.jmrit.display;
002
003import javax.swing.DefaultComboBoxModel;
004import javax.swing.JComboBox;
005
006/**
007 * An icon to display and input a Memory value in a TextField.
008 * <p>
009 * Handles the case of either a String or an Integer in the Memory, preserving
010 * what it finds.
011 *
012 * @author Pete Cressman    Copyright (c) 2012
013 * @author Daniel Bergqvist Copyright (C) 2022
014 * @since 5.3.1
015 */
016public abstract class MemoryOrGVComboIcon extends PositionableJPanel {
017
018    public MemoryOrGVComboIcon(Editor editor) {
019        super(editor);
020    }
021
022    @Override
023    public abstract JComboBox<String> getTextComponent();
024
025    protected abstract ComboModel getComboModel();
026
027    protected abstract void update();
028
029
030    protected class ComboModel extends DefaultComboBoxModel<String> {
031
032        ComboModel() {
033            super();
034        }
035
036        ComboModel(String[] l) {
037            super(l);
038        }
039
040        @Override
041        public void addElement(String obj) {
042            if (getIndexOf(obj) >= 0) {
043                return;
044            }
045            super.addElement(obj);
046            update();
047        }
048
049        @Override
050        public void insertElementAt(String obj, int idx) {
051            if (getIndexOf(obj) >= 0) {
052                return;
053            }
054            super.insertElementAt(obj, idx);
055            update();
056        }
057    }
058
059}