001package jmri.util.swing;
002
003import com.alexandriasoftware.swing.JSplitButton;
004import java.awt.Color;
005import java.awt.Graphics;
006import java.awt.event.ActionEvent;
007import java.awt.image.BufferedImage;
008import javax.swing.Icon;
009import javax.swing.ImageIcon;
010import javax.swing.JColorChooser;
011import javax.swing.colorchooser.AbstractColorChooserPanel;
012
013/**
014 * Abstract Color Chooser extension that presents a split button
015 * to set the color. Clicking the button results in a
016 * JColorChooser launching.
017 *
018 * @author Paul Bender Copyright (C) 2018
019 * @since 4.1.1
020 */
021public class SplitButtonColorChooserPanel extends AbstractColorChooserPanel {
022
023    //private JLabel swatch = null;
024    private JSplitButton setButton = null;
025    private static final int ICON_DIMENSION = 20;
026
027    @Override
028    public void updateChooser(){
029        Color color = getColorFromModel();
030        // update the Swatch to have the right color showing.
031        BufferedImage image = new BufferedImage(ICON_DIMENSION, ICON_DIMENSION,
032                BufferedImage.TYPE_INT_RGB);
033
034        Graphics g = image.getGraphics();
035        // fill it with its representative color
036        g.setColor(color);
037        g.fillRect(0, 0, ICON_DIMENSION, ICON_DIMENSION);
038        // draw a black border around it
039        g.setColor(Color.black);
040        g.drawRect(0, 0, ICON_DIMENSION - 1, ICON_DIMENSION - 1);
041
042        //setButton.setImage(image);
043        ImageIcon icon = new ImageIcon(image);
044        setButton.setIcon(icon);
045        setButton.setPopupMenu(new ColorListPopupMenu(getColorSelectionModel()));
046        g.dispose();
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        setButton = new JSplitButton(Bundle.getMessage("SetColor") + "      ",icon);  // NOI18N
063        setButton.addButtonClickedActionListener((ActionEvent e) -> {
064            Color desiredColor = JColorChooser.showDialog(setButton.getParent(),
065                    Bundle.getMessage("SetColor"), // NOI18N
066                    getColorFromModel());
067            if (desiredColor != null) {
068                getColorSelectionModel().setSelectedColor(desiredColor);
069            }
070        });
071        setButton.addSplitButtonClickedActionListener((ActionEvent e) -> {
072            //Color desiredColor = JColorChooser.showDialog(this,
073            //                     Bundle.getMessage("SetColor"),
074            //                     getColorFromModel());
075            //if (desiredColor!=null) {
076            //    getColorSelectionModel().setSelectedColor(desiredColor);
077        });
078
079        //setButton.setImage(image);
080        setButton.setPopupMenu(new ColorListPopupMenu(getColorSelectionModel()));
081        add(setButton);
082    }
083
084    @Override
085    public String getDisplayName() {
086         return Bundle.getMessage("SplitButtonColorChooserName");  // NOI18N
087    }
088
089    @Override
090    public Icon getSmallDisplayIcon(){
091       return null;
092    }
093
094    @Override
095    public Icon getLargeDisplayIcon(){
096       return null;
097    }
098}