KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalComboBoxButton


1 /*
2  * @(#)MetalComboBoxButton.java 1.38 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.metal;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.plaf.basic.*;
13 import javax.swing.*;
14 import javax.swing.plaf.*;
15 import javax.swing.border.*;
16 import java.io.Serializable JavaDoc;
17
18 /**
19  * JButton subclass to help out MetalComboBoxUI
20  * <p>
21  * <strong>Warning:</strong>
22  * Serialized objects of this class will not be compatible with
23  * future Swing releases. The current serialization support is
24  * appropriate for short term storage or RMI between applications running
25  * the same version of Swing. As of 1.4, support for long term storage
26  * of all JavaBeans<sup><font size="-2">TM</font></sup>
27  * has been added to the <code>java.beans</code> package.
28  * Please see {@link java.beans.XMLEncoder}.
29  *
30  * @see MetalComboBoxButton
31  * @version 1.38 12/19/03
32  * @author Tom Santos
33  */

34 public class MetalComboBoxButton extends JButton {
35     protected JComboBox comboBox;
36     protected JList listBox;
37     protected CellRendererPane rendererPane;
38     protected Icon comboIcon;
39     protected boolean iconOnly = false;
40
41     public final JComboBox getComboBox() { return comboBox;}
42     public final void setComboBox( JComboBox cb ) { comboBox = cb;}
43
44     public final Icon getComboIcon() { return comboIcon;}
45     public final void setComboIcon( Icon i ) { comboIcon = i;}
46
47     public final boolean isIconOnly() { return iconOnly;}
48     public final void setIconOnly( boolean isIconOnly ) { iconOnly = isIconOnly;}
49
50     MetalComboBoxButton() {
51         super( "" );
52         DefaultButtonModel model = new DefaultButtonModel() {
53             public void setArmed( boolean armed ) {
54                 super.setArmed( isPressed() ? true : armed );
55             }
56         };
57         setModel( model );
58     }
59
60     public MetalComboBoxButton( JComboBox cb, Icon i,
61                                 CellRendererPane pane, JList list ) {
62         this();
63         comboBox = cb;
64         comboIcon = i;
65         rendererPane = pane;
66         listBox = list;
67         setEnabled( comboBox.isEnabled() );
68     }
69
70     public MetalComboBoxButton( JComboBox cb, Icon i, boolean onlyIcon,
71                                 CellRendererPane pane, JList list ) {
72         this( cb, i, pane, list );
73         iconOnly = onlyIcon;
74     }
75
76     public boolean isFocusTraversable() {
77     return false;
78     }
79
80     public void setEnabled(boolean enabled) {
81     super.setEnabled(enabled);
82
83     // Set the background and foreground to the combobox colors.
84
if (enabled) {
85         setBackground(comboBox.getBackground());
86         setForeground(comboBox.getForeground());
87     } else {
88         setBackground(UIManager.getColor("ComboBox.disabledBackground"));
89         setForeground(UIManager.getColor("ComboBox.disabledForeground"));
90     }
91     }
92
93     public void paintComponent( Graphics g ) {
94         boolean leftToRight = MetalUtils.isLeftToRight(comboBox);
95
96         // Paint the button as usual
97
super.paintComponent( g );
98
99         Insets insets = getInsets();
100
101         int width = getWidth() - (insets.left + insets.right);
102         int height = getHeight() - (insets.top + insets.bottom);
103
104         if ( height <= 0 || width <= 0 ) {
105             return;
106         }
107
108         int left = insets.left;
109         int top = insets.top;
110         int right = left + (width - 1);
111         int bottom = top + (height - 1);
112
113         int iconWidth = 0;
114         int iconLeft = (leftToRight) ? right : left;
115
116         // Paint the icon
117
if ( comboIcon != null ) {
118             iconWidth = comboIcon.getIconWidth();
119             int iconHeight = comboIcon.getIconHeight();
120             int iconTop = 0;
121
122             if ( iconOnly ) {
123                 iconLeft = (getWidth() / 2) - (iconWidth / 2);
124                 iconTop = (getHeight() / 2) - (iconHeight / 2);
125             }
126             else {
127             if (leftToRight) {
128             iconLeft = (left + (width - 1)) - iconWidth;
129         }
130         else {
131             iconLeft = left;
132         }
133                 iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
134             }
135
136             comboIcon.paintIcon( this, g, iconLeft, iconTop );
137
138             // Paint the focus
139
if ( comboBox.hasFocus() && (!MetalLookAndFeel.usingOcean() ||
140                                          comboBox.isEditable())) {
141                 g.setColor( MetalLookAndFeel.getFocusColor() );
142                 g.drawRect( left - 1, top - 1, width + 3, height + 1 );
143             }
144         }
145
146         if (MetalLookAndFeel.usingOcean()) {
147             // With Ocean the button only paints the arrow, bail.
148
return;
149         }
150
151         // Let the renderer paint
152
if ( ! iconOnly && comboBox != null ) {
153             ListCellRenderer renderer = comboBox.getRenderer();
154             Component c;
155             boolean renderPressed = getModel().isPressed();
156             c = renderer.getListCellRendererComponent(listBox,
157                                                       comboBox.getSelectedItem(),
158                                                       -1,
159                                                       renderPressed,
160                                                       false);
161             c.setFont(rendererPane.getFont());
162
163             if ( model.isArmed() && model.isPressed() ) {
164                 if ( isOpaque() ) {
165                     c.setBackground(UIManager.getColor("Button.select"));
166                 }
167                 c.setForeground(comboBox.getForeground());
168             }
169             else if ( !comboBox.isEnabled() ) {
170                 if ( isOpaque() ) {
171                     c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
172                 }
173                 c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
174             }
175             else {
176                 c.setForeground(comboBox.getForeground());
177                 c.setBackground(comboBox.getBackground());
178             }
179
180
181             int cWidth = width - (insets.right + iconWidth);
182             
183             // Fix for 4238829: should lay out the JPanel.
184
boolean shouldValidate = false;
185             if (c instanceof JPanel) {
186                 shouldValidate = true;
187             }
188             
189         if (leftToRight) {
190             rendererPane.paintComponent( g, c, this,
191                          left, top, cWidth, height, shouldValidate );
192         }
193         else {
194             rendererPane.paintComponent( g, c, this,
195                          left + iconWidth, top, cWidth, height, shouldValidate );
196         }
197         }
198     }
199 }
200
Popular Tags