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