001package jmri.util.swing;
002
003import java.awt.Color;
004import java.awt.event.ActionEvent;
005import javax.swing.colorchooser.AbstractColorChooserPanel;
006import javax.swing.Icon;
007import javax.swing.JComboBox;
008
009/**
010 * Abstract Color Chooser extension that presents the JMRI preset colors in
011 * a Combo Box with proper internationalization.
012 *
013 * @author Paul Bender Copyright (C) 2017
014 * @since 4.9.6
015 */
016public class ComboBoxColorChooserPanel extends AbstractColorChooserPanel {
017
018    private String[] colorText = {"Black", "DarkGray", "Gray",
019       "LightGray", "White", "Red", "Pink", "Orange",
020       "Yellow", "Green", "Blue", "Magenta", "Cyan"};    // NOI18N
021    private Color[] colorCode = {Color.black, Color.darkGray, Color.gray,
022       Color.lightGray, Color.white, Color.red, Color.pink, Color.orange,
023       Color.yellow, Color.green, Color.blue, Color.magenta, Color.cyan};
024    private int numColors = 13; //number of entries in the above arrays
025    private JComboBox<String> colorCombo = null;
026
027    @Override
028    public void updateChooser(){
029        Color color = getColorFromModel();
030        // update the combo box to have the right color showing.
031        for(int i = 0;i< numColors;i++){
032            if(color.equals(colorCode[i])){
033               colorCombo.setSelectedIndex(i);
034               return;
035            }
036        }
037    }
038
039    @Override
040    protected void buildChooser(){
041        // remove everything from the chooser panel
042        removeAll();
043        // build the combo box.
044        colorCombo = new JComboBox<String>();
045        for (int i = 0; i < numColors; i++) {
046            colorCombo.addItem(Bundle.getMessage(colorText[i]));   
047        }
048        colorCombo.setMaximumRowCount(numColors);
049        colorCombo.addActionListener( (ActionEvent e) -> {
050            int index = colorCombo.getSelectedIndex();
051            getColorSelectionModel().setSelectedColor(colorCode[index]);
052        });
053        add(colorCombo);
054    }
055
056    @Override
057    public String getDisplayName() {
058         return Bundle.getMessage("ComboBoxColorChooserName");
059    }
060
061    @Override
062    public Icon getSmallDisplayIcon(){
063       return null;
064    }
065
066    @Override
067    public Icon getLargeDisplayIcon(){
068       return null;
069    }
070}