1 19 20 package org.netbeans.modules.options.colors; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.Graphics ; 26 import java.awt.SystemColor ; 27 import java.awt.event.ActionListener ; 28 import javax.swing.ComboBoxEditor ; 29 import javax.swing.JComboBox ; 30 import javax.swing.JComponent ; 31 import javax.swing.JList ; 32 import javax.swing.ListCellRenderer ; 33 import javax.swing.SwingUtilities ; 34 35 36 41 class ColorComboBoxRenderer extends JComponent implements 42 ListCellRenderer , ComboBoxEditor { 43 44 private int SIZE = 9; 45 private ColorValue value; 46 private JComboBox comboBox; 47 48 ColorComboBoxRenderer (JComboBox comboBox) { 49 this.comboBox = comboBox; 50 setPreferredSize (new Dimension ( 51 50, 52 comboBox.getFontMetrics (comboBox.getFont ()). 53 getHeight () + 2 54 )); 55 setOpaque (true); 56 setFocusable (true); 57 } 58 59 public void paint (Graphics g) { 60 Color oldColor = g.getColor (); 61 Dimension size = getSize (); 62 if (isFocusOwner ()) 63 g.setColor (SystemColor.textHighlight); 64 else 65 g.setColor (getBackground ()); 66 g.fillRect (0, 0, size.width, size.height); 67 int i = (size.height - SIZE) / 2; 68 if (value.color != null) { 69 g.setColor (Color.black); 70 g.drawRect (i, i, SIZE, SIZE); 71 g.setColor (value.color); 72 g.fillRect (i + 1, i + 1, SIZE - 1, SIZE - 1); 73 } 74 if (value.text != null) { 75 if (isFocusOwner ()) 76 g.setColor (SystemColor.textHighlightText); 77 else 78 g.setColor (getForeground ()); 79 if (value.color != null) 80 g.drawString (value.text, i + SIZE + 5, i + SIZE); 81 else 82 g.drawString (value.text, 5, i + SIZE); 83 } 84 g.setColor (oldColor); 85 } 86 87 public void setEnabled (boolean enabled) { 88 setBackground (enabled ? 89 SystemColor.text : SystemColor.control 90 ); 91 super.setEnabled (enabled); 92 } 93 94 public Component getListCellRendererComponent ( 95 JList list, 96 Object value, 97 int index, 98 boolean isSelected, 99 boolean cellHasFocus 100 ) { 101 this.value = (ColorValue) value; 102 setEnabled (list.isEnabled ()); 103 setBackground (isSelected ? 104 SystemColor.textHighlight : SystemColor.text 105 ); 106 setForeground (isSelected ? 107 SystemColor.textHighlightText : SystemColor.textText 108 ); 109 return this; 110 } 111 112 public Component getEditorComponent () { 113 setEnabled (comboBox.isEnabled ()); 114 setBackground (comboBox.isFocusOwner () ? 115 SystemColor.textHighlight : SystemColor.text 116 ); 117 setForeground (comboBox.isFocusOwner () ? 118 SystemColor.textHighlightText : SystemColor.textText 119 ); 120 return this; 121 } 122 123 public void setItem (Object anObject) { 124 Object oldValue = this.value; 125 this.value = (ColorValue) anObject; 126 firePropertyChange(ColorComboBox.PROP_COLOR, oldValue, anObject); 127 } 128 129 public Object getItem () { 130 return value; 131 } 132 133 public void selectAll() {} 134 public void addActionListener (ActionListener l) {} 135 public void removeActionListener (ActionListener l) {} 136 } 137 | Popular Tags |