33 import javax.swing.border.*;
34 import java.io.Serializable;
35
36 /**
37 * JButton subclass to help out MetalComboBoxUI
38 * <p>
39 * <strong>Warning:</strong>
40 * Serialized objects of this class will not be compatible with
41 * future Swing releases. The current serialization support is
42 * appropriate for short term storage or RMI between applications running
43 * the same version of Swing. As of 1.4, support for long term storage
44 * of all JavaBeans™
45 * has been added to the <code>java.beans</code> package.
46 * Please see {@link java.beans.XMLEncoder}.
47 *
48 * @see MetalComboBoxButton
49 * @author Tom Santos
50 */
51 @SuppressWarnings("serial") // Same-version serialization only
52 public class MetalComboBoxButton extends JButton {
53 protected JComboBox comboBox;
54 protected JList listBox;
55 protected CellRendererPane rendererPane;
56 protected Icon comboIcon;
57 protected boolean iconOnly = false;
58
59 public final JComboBox getComboBox() { return comboBox;}
60 public final void setComboBox( JComboBox cb ) { comboBox = cb;}
61
62 public final Icon getComboIcon() { return comboIcon;}
63 public final void setComboIcon( Icon i ) { comboIcon = i;}
64
65 public final boolean isIconOnly() { return iconOnly;}
66 public final void setIconOnly( boolean isIconOnly ) { iconOnly = isIconOnly;}
67
68 MetalComboBoxButton() {
69 super( "" );
70 DefaultButtonModel model = new DefaultButtonModel() {
71 public void setArmed( boolean armed ) {
72 super.setArmed( isPressed() ? true : armed );
73 }
74 };
75 setModel( model );
76 }
77
78 public MetalComboBoxButton( JComboBox cb, Icon i,
79 CellRendererPane pane, JList list ) {
80 this();
81 comboBox = cb;
82 comboIcon = i;
83 rendererPane = pane;
84 listBox = list;
85 setEnabled( comboBox.isEnabled() );
86 }
87
88 public MetalComboBoxButton( JComboBox cb, Icon i, boolean onlyIcon,
89 CellRendererPane pane, JList list ) {
90 this( cb, i, pane, list );
91 iconOnly = onlyIcon;
92 }
93
94 public boolean isFocusTraversable() {
95 return false;
96 }
97
98 public void setEnabled(boolean enabled) {
99 super.setEnabled(enabled);
100
101 // Set the background and foreground to the combobox colors.
102 if (enabled) {
103 setBackground(comboBox.getBackground());
104 setForeground(comboBox.getForeground());
105 } else {
106 setBackground(UIManager.getColor("ComboBox.disabledBackground"));
107 setForeground(UIManager.getColor("ComboBox.disabledForeground"));
108 }
109 }
151 iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
152 }
153
154 comboIcon.paintIcon( this, g, iconLeft, iconTop );
155
156 // Paint the focus
157 if ( comboBox.hasFocus() && (!MetalLookAndFeel.usingOcean() ||
158 comboBox.isEditable())) {
159 g.setColor( MetalLookAndFeel.getFocusColor() );
160 g.drawRect( left - 1, top - 1, width + 3, height + 1 );
161 }
162 }
163
164 if (MetalLookAndFeel.usingOcean()) {
165 // With Ocean the button only paints the arrow, bail.
166 return;
167 }
168
169 // Let the renderer paint
170 if ( ! iconOnly && comboBox != null ) {
171 ListCellRenderer renderer = comboBox.getRenderer();
172 Component c;
173 boolean renderPressed = getModel().isPressed();
174 c = renderer.getListCellRendererComponent(listBox,
175 comboBox.getSelectedItem(),
176 -1,
177 renderPressed,
178 false);
179 c.setFont(rendererPane.getFont());
180
181 if ( model.isArmed() && model.isPressed() ) {
182 if ( isOpaque() ) {
183 c.setBackground(UIManager.getColor("Button.select"));
184 }
185 c.setForeground(comboBox.getForeground());
186 }
187 else if ( !comboBox.isEnabled() ) {
188 if ( isOpaque() ) {
189 c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
190 }
191 c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
|
33 import javax.swing.border.*;
34 import java.io.Serializable;
35
36 /**
37 * JButton subclass to help out MetalComboBoxUI
38 * <p>
39 * <strong>Warning:</strong>
40 * Serialized objects of this class will not be compatible with
41 * future Swing releases. The current serialization support is
42 * appropriate for short term storage or RMI between applications running
43 * the same version of Swing. As of 1.4, support for long term storage
44 * of all JavaBeans™
45 * has been added to the <code>java.beans</code> package.
46 * Please see {@link java.beans.XMLEncoder}.
47 *
48 * @see MetalComboBoxButton
49 * @author Tom Santos
50 */
51 @SuppressWarnings("serial") // Same-version serialization only
52 public class MetalComboBoxButton extends JButton {
53 protected JComboBox<Object> comboBox;
54 protected JList<Object> listBox;
55 protected CellRendererPane rendererPane;
56 protected Icon comboIcon;
57 protected boolean iconOnly = false;
58
59 public final JComboBox<Object> getComboBox() { return comboBox;}
60 public final void setComboBox( JComboBox<Object> cb ) { comboBox = cb;}
61
62 public final Icon getComboIcon() { return comboIcon;}
63 public final void setComboIcon( Icon i ) { comboIcon = i;}
64
65 public final boolean isIconOnly() { return iconOnly;}
66 public final void setIconOnly( boolean isIconOnly ) { iconOnly = isIconOnly;}
67
68 MetalComboBoxButton() {
69 super( "" );
70 DefaultButtonModel model = new DefaultButtonModel() {
71 public void setArmed( boolean armed ) {
72 super.setArmed( isPressed() ? true : armed );
73 }
74 };
75 setModel( model );
76 }
77
78 public MetalComboBoxButton( JComboBox<Object> cb, Icon i,
79 CellRendererPane pane, JList<Object> list ) {
80 this();
81 comboBox = cb;
82 comboIcon = i;
83 rendererPane = pane;
84 listBox = list;
85 setEnabled( comboBox.isEnabled() );
86 }
87
88 public MetalComboBoxButton( JComboBox<Object> cb, Icon i, boolean onlyIcon,
89 CellRendererPane pane, JList<Object> list ) {
90 this( cb, i, pane, list );
91 iconOnly = onlyIcon;
92 }
93
94 public boolean isFocusTraversable() {
95 return false;
96 }
97
98 public void setEnabled(boolean enabled) {
99 super.setEnabled(enabled);
100
101 // Set the background and foreground to the combobox colors.
102 if (enabled) {
103 setBackground(comboBox.getBackground());
104 setForeground(comboBox.getForeground());
105 } else {
106 setBackground(UIManager.getColor("ComboBox.disabledBackground"));
107 setForeground(UIManager.getColor("ComboBox.disabledForeground"));
108 }
109 }
151 iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
152 }
153
154 comboIcon.paintIcon( this, g, iconLeft, iconTop );
155
156 // Paint the focus
157 if ( comboBox.hasFocus() && (!MetalLookAndFeel.usingOcean() ||
158 comboBox.isEditable())) {
159 g.setColor( MetalLookAndFeel.getFocusColor() );
160 g.drawRect( left - 1, top - 1, width + 3, height + 1 );
161 }
162 }
163
164 if (MetalLookAndFeel.usingOcean()) {
165 // With Ocean the button only paints the arrow, bail.
166 return;
167 }
168
169 // Let the renderer paint
170 if ( ! iconOnly && comboBox != null ) {
171 ListCellRenderer<Object> renderer = comboBox.getRenderer();
172 Component c;
173 boolean renderPressed = getModel().isPressed();
174 c = renderer.getListCellRendererComponent(listBox,
175 comboBox.getSelectedItem(),
176 -1,
177 renderPressed,
178 false);
179 c.setFont(rendererPane.getFont());
180
181 if ( model.isArmed() && model.isPressed() ) {
182 if ( isOpaque() ) {
183 c.setBackground(UIManager.getColor("Button.select"));
184 }
185 c.setForeground(comboBox.getForeground());
186 }
187 else if ( !comboBox.isEnabled() ) {
188 if ( isOpaque() ) {
189 c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
190 }
191 c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
|