001package jmri.util.swing;
002
003import java.awt.Color;
004import java.awt.Graphics;
005import java.awt.event.ActionEvent;
006import java.awt.image.BufferedImage;
007import javax.swing.colorchooser.ColorSelectionModel;
008import javax.swing.ImageIcon;
009import javax.swing.JLabel;
010import javax.swing.JPopupMenu;
011import javax.swing.JMenuItem;
012
013/**
014 * Popup menu for displaying recently selected colors along with standard
015 * java colors.
016 *
017 * @author Paul Bender Copyright (C) 2018
018 * @since 4.13.1
019 */
020public class ColorListPopupMenu extends JPopupMenu {
021
022    private static final int ICON_DIMENSION = 20;
023
024    // Standard Colors
025    private Color[] colorCode = {Color.black, Color.darkGray, Color.gray,
026       Color.lightGray, Color.white, Color.red, Color.pink, Color.orange,
027       Color.yellow, Color.green, Color.blue, Color.magenta, Color.cyan,
028       jmri.util.ColorUtil.BROWN};
029    private int numColors = 14; //number of entries in the above arrays
030    private ColorSelectionModel model;
031
032    public ColorListPopupMenu(ColorSelectionModel m){
033        super();
034        model = m;
035        addRecentColors();
036        addSeparator();
037        addStandardColors();
038    }
039
040    private void addRecentColors(){
041        // build the menu.
042        add(new JLabel(Bundle.getMessage("RecentColorLabel")));
043        for (Color color : JmriColorChooser.getRecentColors()) {
044            add(createMenuItem(color, false));
045        }
046    }
047
048    private void addStandardColors(){
049        // build the menu.
050        add(new JLabel(Bundle.getMessage("StandardColorLabel")));
051        for (int i = 0; i < numColors; i++) {
052            add(createMenuItem(colorCode[i], true));
053        }
054    }
055
056    private JMenuItem createMenuItem(Color swatchColor, boolean isStdColor){
057        // update the Swatch to have the right color showing.
058        BufferedImage image = new BufferedImage(ICON_DIMENSION, ICON_DIMENSION,
059             BufferedImage.TYPE_INT_ARGB);
060
061        Graphics g = image.getGraphics();
062        // fill it with its representative color
063        g.setColor(new Color(swatchColor.getRed(), swatchColor.getGreen(), swatchColor.getBlue(), swatchColor.getAlpha()));
064        g.fillRect(0, 0, ICON_DIMENSION, ICON_DIMENSION);
065        // draw a black border around it
066        g.setColor(Color.black);
067        g.drawRect(0, 0, ICON_DIMENSION - 1, ICON_DIMENSION - 1);
068
069        ImageIcon icon = new ImageIcon(image);
070
071        g.dispose();
072
073        String colorName = "";
074        String colorTip = String.format("r=%d, g=%d, b=%d, a=%d",
075                swatchColor.getRed(), swatchColor.getGreen(), swatchColor.getBlue(), swatchColor.getAlpha());
076        if (isStdColor) {
077            colorName = jmri.util.ColorUtil.colorToLocalizedName(swatchColor);
078        } else {
079            colorName = colorTip;
080        }
081
082        JMenuItem colorMenuItem = new JMenuItem(colorName,icon);
083        if (isStdColor) colorMenuItem.setToolTipText(colorTip);
084        colorMenuItem.addActionListener((ActionEvent e) -> {
085           model.setSelectedColor(swatchColor);
086        });
087        return colorMenuItem;
088    }
089
090}