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.Icon;
008import javax.swing.ImageIcon;
009import javax.swing.JButton;
010import javax.swing.JColorChooser;
011import javax.swing.JLabel;
012import javax.swing.colorchooser.AbstractColorChooserPanel;
013
014/**
015 * Abstract Color Chooser extension that presents a swatch sample of the color
016 * and a button to set the color. Clicking the button results in a 
017 * JColorChooser launching.
018 *
019 * @author Paul Bender Copyright (C) 2017
020 * @since 4.9.6
021 */
022public class ButtonSwatchColorChooserPanel extends AbstractColorChooserPanel {
023
024    private JLabel swatch = null;
025    private JButton setButton = null;
026    private static final int ICON_DIMENSION = 20;
027
028    @Override
029    public void updateChooser(){
030        Color color = getColorFromModel();
031        // update the Swatch to have the right color showing.
032        BufferedImage image = new BufferedImage(ICON_DIMENSION, ICON_DIMENSION,
033                BufferedImage.TYPE_INT_RGB);
034
035        Graphics g = image.getGraphics();
036        // fill it with its representative color
037        g.setColor(color);
038        g.fillRect(0, 0, ICON_DIMENSION, ICON_DIMENSION);
039        // draw a black border around it
040        g.setColor(Color.black);
041        g.drawRect(0, 0, ICON_DIMENSION - 1, ICON_DIMENSION - 1);
042
043        ImageIcon icon = new ImageIcon(image); 
044        g.dispose();
045
046        swatch.setIcon(icon);
047    }
048
049    @Override
050    protected void buildChooser(){
051        BufferedImage image = new BufferedImage(ICON_DIMENSION, ICON_DIMENSION,
052                BufferedImage.TYPE_INT_RGB);
053
054        Graphics g = image.getGraphics();
055        // set completely transparent
056        g.setColor(getColorFromModel());
057        g.fillRect(0, 0, ICON_DIMENSION, ICON_DIMENSION);
058
059        ImageIcon icon = new ImageIcon(image); 
060        g.dispose();
061
062        swatch = new JLabel(icon);
063        add(swatch);
064    
065        setButton = new JButton(Bundle.getMessage("SetColor"));
066        setButton.addActionListener((ActionEvent e) -> {
067            Color desiredColor = JColorChooser.showDialog(this,
068                                 Bundle.getMessage("SetColor"),
069                                 getColorFromModel());
070            if (desiredColor!=null) {
071                getColorSelectionModel().setSelectedColor(desiredColor);
072            }
073        });
074        add(setButton);
075    }
076
077    @Override
078    public String getDisplayName() {
079         return Bundle.getMessage("ButtonSwatchColorChooserName");
080    }
081
082    @Override
083    public Icon getSmallDisplayIcon(){
084       return null;
085    }
086
087    @Override
088    public Icon getLargeDisplayIcon(){
089       return null;
090    }
091}