1 19 20 package org.netbeans.modules.options.colors; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dialog ; 25 import java.awt.Dimension ; 26 import java.awt.Font ; 27 import java.awt.FontMetrics ; 28 import java.awt.Graphics ; 29 import java.awt.SystemColor ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 import javax.swing.ComboBoxEditor ; 35 import javax.swing.DefaultComboBoxModel ; 36 import javax.swing.DefaultListCellRenderer ; 37 import javax.swing.JColorChooser ; 38 import javax.swing.JComboBox ; 39 import javax.swing.JComponent ; 40 import javax.swing.JLabel ; 41 import javax.swing.JList ; 42 import javax.swing.JPanel ; 43 import javax.swing.JTextField ; 44 import javax.swing.ListCellRenderer ; 45 import javax.swing.SwingUtilities ; 46 import javax.swing.UIManager ; 47 import org.openide.util.NbBundle; 48 49 50 54 public class ColorComboBox { 55 56 public static final String PROP_COLOR = "color"; 58 private static Object [] content = new Object [] { 59 new ColorValue (Color.BLACK), 60 new ColorValue (Color.BLUE), 61 new ColorValue (Color.CYAN), 62 new ColorValue (Color.DARK_GRAY), 63 new ColorValue (Color.GRAY), 64 new ColorValue (Color.GREEN), 65 new ColorValue (Color.LIGHT_GRAY), 66 new ColorValue (Color.MAGENTA), 67 new ColorValue (Color.ORANGE), 68 new ColorValue (Color.PINK), 69 new ColorValue (Color.RED), 70 new ColorValue (Color.WHITE), 71 new ColorValue (Color.YELLOW), 72 ColorValue.CUSTOM_COLOR, 73 new ColorValue (loc ("CTL_None_Color"), null) }; 75 76 77 78 static void init (final JComboBox combo) { 79 combo.setModel (new DefaultComboBoxModel (content)); 80 combo.setRenderer (new ColorComboBoxRenderer (combo)); 81 combo.setEditable (true); 82 combo.setEditor (new ColorComboBoxRenderer (combo)); 83 combo.setSelectedItem (new ColorValue (null, null)); 84 combo.addActionListener (new ComboBoxListener (combo)); 85 } 86 87 static void setInheritedColor (JComboBox combo, Color color) { 88 Object [] ncontent = new Object [content.length]; 89 System.arraycopy (content, 0, ncontent, 0, content.length); 90 if (color != null) 91 ncontent [content.length - 1] = new ColorValue ( 92 loc ("CTL_Inherited_Color"), color ); 94 else 95 ncontent [content.length - 1] = new ColorValue ( 96 loc ("CTL_None_Color"), null ); 98 combo.setModel (new DefaultComboBoxModel (ncontent)); 99 } 100 101 static void setColor (JComboBox combo, Color color) { 102 if (color == null) { 103 combo.setSelectedIndex (content.length - 1); 104 } else { 105 combo.setSelectedItem (new ColorValue (color)); 106 } 107 } 108 109 static Color getColor (JComboBox combo) { 110 if (combo.getSelectedIndex () == (content.length - 1)) return null; 111 return ((ColorValue) combo.getSelectedItem ()).color; 112 } 113 114 private static String loc (String key) { 115 return NbBundle.getMessage (ColorComboBox.class, key); 116 } 117 118 private static class ComboBoxListener implements ActionListener { 120 121 private JComboBox combo; 122 private Object lastSelection; 123 124 ComboBoxListener(JComboBox combo) { 125 this.combo = combo; 126 lastSelection = combo.getSelectedItem(); 127 } 128 129 public void actionPerformed(ActionEvent ev) { 130 if (combo.getSelectedItem() == ColorValue.CUSTOM_COLOR) { 131 Color c = JColorChooser.showDialog( 132 SwingUtilities.getAncestorOfClass 133 (Dialog .class, combo), 134 loc("SelectColor"), 135 null 136 ); 137 if (c != null) { 138 setColor (combo, c); 139 } else if (lastSelection != null) { 140 combo.setSelectedItem(lastSelection); 141 } 142 } 143 lastSelection = combo.getSelectedItem(); 144 } 145 146 } 148 } 149 | Popular Tags |