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.ButtonGroup; 008import javax.swing.BoxLayout; 009import javax.swing.JRadioButton; 010 011/** 012 * Abstract Color Chooser extension that presents the JMRI preset colors in 013 * as a Radio Button list with proper internationalization. 014 * 015 * @author Paul Bender Copyright (C) 2017 016 * @since 4.9.6 017 */ 018public class ButtonGroupColorChooserPanel extends AbstractColorChooserPanel { 019 020 private String[] colorText = {"Black", "DarkGray", "Gray", 021 "LightGray", "White", "Red", "Pink", "Orange", 022 "Yellow", "Green", "Blue", "Magenta", "Cyan"}; // NOI18N 023 private Color[] colorCode = {Color.black, Color.darkGray, Color.gray, 024 Color.lightGray, Color.white, Color.red, Color.pink, Color.orange, 025 Color.yellow, Color.green, Color.blue, Color.magenta, Color.cyan}; 026 private int numColors = 13; //number of entries in the above arrays 027 private ButtonGroup colorButtonGroup = null; 028 029 @Override 030 public void updateChooser(){ 031 Color color = getColorFromModel(); 032 // update the combo box to have the right color showing. 033 for(int i = 0;i< numColors;i++){ 034 if(color.equals(colorCode[i])){ 035 String buttonLabel = Bundle.getMessage(colorText[i]); 036 var e = colorButtonGroup.getElements(); 037 while(e.hasMoreElements()) { 038 JRadioButton button = (JRadioButton)e.nextElement(); 039 if(buttonLabel.equals(button.getText())){ 040 button.setSelected(true); 041 return; 042 } 043 } 044 } 045 } 046 } 047 048 @Override 049 protected void buildChooser(){ 050 setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS)); 051 // build the combo box. 052 colorButtonGroup = new ButtonGroup(); 053 for (int i = 0; i < numColors; i++) { 054 addButton(colorButtonGroup,colorCode[i],Bundle.getMessage(colorText[i])); 055 } 056 } 057 058 private void addButton(ButtonGroup bg, Color color, String buttonLabel ) { 059 JRadioButton button = new JRadioButton(buttonLabel); 060 button.addActionListener( (ActionEvent e) -> { 061 getColorSelectionModel().setSelectedColor(color); 062 }); 063 bg.add(button); 064 this.add(button); // add the button to the panel. 065 } 066 067 @Override 068 public String getDisplayName() { 069 return Bundle.getMessage("ButtonGroupColorChooserName"); 070 } 071 072 @Override 073 public Icon getSmallDisplayIcon(){ 074 return null; 075 } 076 077 @Override 078 public Icon getLargeDisplayIcon(){ 079 return null; 080 } 081}