KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > colors > ColorComboBoxRenderer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.options.colors;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.SystemColor JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import javax.swing.ComboBoxEditor JavaDoc;
29 import javax.swing.JComboBox JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.JList JavaDoc;
32 import javax.swing.ListCellRenderer JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34
35
36 /**
37  * Renderer and editor for color JComboBox.
38  *
39  * @author Jan Jancura
40  */

41 class ColorComboBoxRenderer extends JComponent JavaDoc implements
42 ListCellRenderer JavaDoc, ComboBoxEditor JavaDoc {
43
44     private int SIZE = 9;
45     private ColorValue value;
46     private JComboBox JavaDoc comboBox;
47
48     ColorComboBoxRenderer (JComboBox JavaDoc comboBox) {
49         this.comboBox = comboBox;
50         setPreferredSize (new Dimension JavaDoc (
51             50,
52             comboBox.getFontMetrics (comboBox.getFont ()).
53                 getHeight () + 2
54         ));
55         setOpaque (true);
56         setFocusable (true);
57     }
58
59     public void paint (Graphics JavaDoc g) {
60         Color JavaDoc oldColor = g.getColor ();
61         Dimension JavaDoc 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 JavaDoc getListCellRendererComponent (
95         JList JavaDoc list,
96         Object JavaDoc 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 JavaDoc 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 JavaDoc anObject) {
124         Object JavaDoc oldValue = this.value;
125         this.value = (ColorValue) anObject;
126         firePropertyChange(ColorComboBox.PROP_COLOR, oldValue, anObject);
127     }
128
129     public Object JavaDoc getItem () {
130         return value;
131     }
132     
133     public void selectAll() {}
134     public void addActionListener (ActionListener JavaDoc l) {}
135     public void removeActionListener (ActionListener JavaDoc l) {}
136 }
137
Popular Tags