001package jmri.util.swing;
002
003import java.awt.Color;
004import java.awt.Graphics;
005import java.awt.GridBagConstraints;
006import java.awt.GridBagLayout;
007import java.awt.event.ActionEvent;
008import java.awt.image.BufferedImage;
009import java.util.ArrayList;
010import javax.swing.BorderFactory;
011import javax.swing.BoxLayout;
012import javax.swing.Icon;
013import javax.swing.ImageIcon;
014import javax.swing.JButton;
015import javax.swing.JPanel;
016import javax.swing.colorchooser.AbstractColorChooserPanel;
017
018/**
019 * Create a custom color chooser panel.
020 * The panel contains two button grids.
021 * The left grid contains the 13 standard Java colors.
022 * The right grid contains recently used colors.
023 *
024 * @author Dave Sand Copyright (C) 2018
025 * @since 4.13.1
026 */
027public class JmriColorChooserPanel extends AbstractColorChooserPanel {
028
029    private Color[] colors = {Color.black, Color.darkGray, Color.gray,
030       Color.lightGray, Color.white, Color.red, Color.pink, Color.orange,
031       Color.yellow, Color.green, Color.blue, Color.magenta, Color.cyan,
032       jmri.util.ColorUtil.BROWN};
033    private int numColors = 14; //number of entries in the above array
034    private JPanel recentPanel = new JPanel(new GridBagLayout());
035
036    @Override
037    public void updateChooser(){
038        GridBagConstraints c = new GridBagConstraints();
039        c.anchor = java.awt.GridBagConstraints.WEST;
040        recentPanel.removeAll();
041
042        ArrayList<Color> colors = JmriColorChooser.getRecentColors();
043        int cols = Math.max(3, (int) Math.ceil((double)colors.size() / 7));
044        int idx = 0;
045        for (Color recent : colors) {
046            c.gridx = idx % cols;
047            c.gridy = idx / cols;
048            recentPanel.add(createColorButton(recent, false), c);
049            idx++;
050        }
051    }
052
053    @Override
054    protected void buildChooser(){
055        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
056
057        JPanel stdColors = new JPanel(new GridBagLayout());
058        stdColors.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("StandardColorLabel")));  // NOI18N
059        GridBagConstraints c = new GridBagConstraints();
060        c.anchor = java.awt.GridBagConstraints.WEST;
061        for (int i = 0; i < numColors; i++) {
062            c.gridx = i % 2;
063            c.gridy = i / 2;
064            stdColors.add(createColorButton(colors[i], true), c);
065        }
066        add(stdColors);
067        stdColors.setVisible(true);
068
069        recentPanel.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("RecentColorLabel")));  // NOI18N
070        ArrayList<Color> colors = JmriColorChooser.getRecentColors();
071        int cols = Math.max(3, (int) Math.ceil((double)colors.size() / 7));
072        int idx = 0;
073        for (Color recent : colors) {
074            c.gridx = idx % cols;
075            c.gridy = idx / cols;
076            recentPanel.add(createColorButton(recent, false), c);
077            idx++;
078        }
079        add(recentPanel);
080        recentPanel.setVisible(true);
081    }
082
083    /**
084     * Create a button that contains a color swatch and
085     * the translated color name.  Use the RGB values if a name is not
086     * available.
087     * @param color The color object
088     * @param stdcolor If true, the color name is used otherwise the RGB values.
089     * @return a button item with the listener.
090     */
091    JButton createColorButton(Color color, boolean stdcolor) {
092        BufferedImage image = new BufferedImage(40, 15,
093                BufferedImage.TYPE_INT_ARGB);
094        Graphics g = image.getGraphics();
095        // fill it with its representative color
096        g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()));
097        g.fillRect(0, 0, 40, 15);
098        // draw a black border around it
099        g.setColor(Color.black);
100        g.drawRect(0, 0, 40 - 1, 15 - 1);
101        ImageIcon icon = new ImageIcon(image);
102
103        String colorName = "";
104        if (stdcolor) {
105            colorName = jmri.util.ColorUtil.colorToLocalizedName(color);
106        }
107        String colorTip = String.format("r=%d, g=%d, b=%d, a=%d",
108                color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
109
110        JButton colorButton = new JButton(colorName, icon);
111        colorButton.setToolTipText(colorTip);
112        colorButton.addActionListener((ActionEvent e) -> {
113            getColorSelectionModel().setSelectedColor(color);
114        });
115        return colorButton;
116    }
117
118    @Override
119    public String getDisplayName() {
120        return "JMRI";  // NOI18N
121    }
122
123    @Override
124    public Icon getSmallDisplayIcon(){
125       return null;
126    }
127
128    @Override
129    public Icon getLargeDisplayIcon(){
130       return null;
131    }
132}