1. /*
  2. * @(#)MetalComboBoxButton.java 1.23 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.metal;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.plaf.basic.*;
  11. import javax.swing.*;
  12. import javax.swing.plaf.*;
  13. import javax.swing.border.*;
  14. import java.io.Serializable;
  15. /**
  16. * JButton subclass to help out MetalComboBoxUI
  17. * <p>
  18. * <strong>Warning:</strong>
  19. * Serialized objects of this class will not be compatible with
  20. * future Swing releases. The current serialization support is appropriate
  21. * for short term storage or RMI between applications running the same
  22. * version of Swing. A future release of Swing will provide support for
  23. * long term persistence.
  24. *
  25. * @see MetalComboBoxButton
  26. * @version 1.19 05/25/98
  27. * @author Tom Santos
  28. */
  29. public class MetalComboBoxButton extends JButton {
  30. protected JComboBox comboBox;
  31. protected JList listBox;
  32. protected CellRendererPane rendererPane;
  33. protected Icon comboIcon;
  34. protected boolean iconOnly = false;
  35. public final JComboBox getComboBox() { return comboBox;}
  36. public final void setComboBox( JComboBox cb ) { comboBox = cb;}
  37. public final Icon getComboIcon() { return comboIcon;}
  38. public final void setComboIcon( Icon i ) { comboIcon = i;}
  39. public final boolean isIconOnly() { return iconOnly;}
  40. public final void setIconOnly( boolean isIconOnly ) { iconOnly = isIconOnly;}
  41. MetalComboBoxButton() {
  42. super( "" );
  43. DefaultButtonModel model = new DefaultButtonModel() {
  44. public void setArmed( boolean armed ) {
  45. super.setArmed( isPressed() ? true : armed );
  46. }
  47. };
  48. setModel( model );
  49. }
  50. public MetalComboBoxButton( JComboBox cb, Icon i,
  51. CellRendererPane pane, JList list ) {
  52. this();
  53. comboBox = cb;
  54. comboIcon = i;
  55. rendererPane = pane;
  56. listBox = list;
  57. setEnabled( comboBox.isEnabled() );
  58. setRequestFocusEnabled( comboBox.isEnabled() );
  59. }
  60. public MetalComboBoxButton( JComboBox cb, Icon i, boolean onlyIcon,
  61. CellRendererPane pane, JList list ) {
  62. this( cb, i, pane, list );
  63. iconOnly = onlyIcon;
  64. }
  65. public boolean isFocusTraversable() {
  66. return (!comboBox.isEditable()) && comboBox.isEnabled();
  67. }
  68. public void paintComponent( Graphics g ) {
  69. // Paint the button as usual
  70. super.paintComponent( g );
  71. Insets insets = getInsets();
  72. int width = getWidth() - (insets.left + insets.right);
  73. int height = getHeight() - (insets.top + insets.bottom);
  74. if ( height <= 0 || width <= 0 ) {
  75. return;
  76. }
  77. int left = insets.left;
  78. int top = insets.top;
  79. int right = left + (width - 1);
  80. int bottom = top + (height - 1);
  81. int iconWidth = 0;
  82. int iconLeft = right;
  83. // Paint the icon
  84. if ( comboIcon != null ) {
  85. iconWidth = comboIcon.getIconWidth();
  86. int iconHeight = comboIcon.getIconHeight();
  87. int iconTop = 0;
  88. if ( iconOnly ) {
  89. iconLeft = (getWidth() / 2) - (iconWidth / 2);
  90. iconTop = (getHeight() / 2) - (iconHeight / 2);
  91. }
  92. else {
  93. iconLeft = (left + (width - 1)) - iconWidth;
  94. iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
  95. }
  96. comboIcon.paintIcon( this, g, iconLeft, iconTop );
  97. // Paint the focus
  98. if ( hasFocus() ) {
  99. g.setColor( MetalLookAndFeel.getFocusColor() );
  100. g.drawRect( left - 1, top - 1, width + 3, height + 1 );
  101. }
  102. }
  103. // Let the renderer paint
  104. if ( ! iconOnly && comboBox != null ) {
  105. ListCellRenderer renderer = comboBox.getRenderer();
  106. Component c;
  107. boolean renderPressed = getModel().isPressed();
  108. c = renderer.getListCellRendererComponent(listBox,
  109. comboBox.getSelectedItem(),
  110. -1,
  111. renderPressed,
  112. false);
  113. c.setFont(rendererPane.getFont());
  114. if ( model.isArmed() && model.isPressed() ) {
  115. if ( isOpaque() ) {
  116. c.setBackground(UIManager.getColor("Button.select"));
  117. }
  118. c.setForeground(comboBox.getForeground());
  119. }
  120. else if ( !comboBox.isEnabled() ) {
  121. if ( isOpaque() ) {
  122. c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
  123. }
  124. c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
  125. }
  126. else {
  127. c.setForeground(comboBox.getForeground());
  128. c.setBackground(comboBox.getBackground());
  129. }
  130. int cWidth = width - (insets.right + iconWidth);
  131. rendererPane.paintComponent( g, c, this, left, top, cWidth, height );
  132. }
  133. }
  134. }