1. /*
  2. * @(#)MetalComboBoxUI.java 1.42 03/01/23
  3. *
  4. * Copyright 2003 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.*;
  11. import javax.swing.plaf.*;
  12. import javax.swing.border.*;
  13. import javax.swing.plaf.basic.*;
  14. import java.io.Serializable;
  15. import java.beans.*;
  16. /**
  17. * Metal UI for JComboBox
  18. * <p>
  19. * <strong>Warning:</strong>
  20. * Serialized objects of this class will not be compatible with
  21. * future Swing releases. The current serialization support is
  22. * appropriate for short term storage or RMI between applications running
  23. * the same version of Swing. As of 1.4, support for long term storage
  24. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  25. * has been added to the <code>java.beans</code> package.
  26. * Please see {@link java.beans.XMLEncoder}.
  27. *
  28. * @see MetalComboBoxEditor
  29. * @see MetalComboBoxButton
  30. * @version 1.42 01/23/03
  31. * @author Tom Santos
  32. */
  33. public class MetalComboBoxUI extends BasicComboBoxUI {
  34. public static ComponentUI createUI(JComponent c) {
  35. return new MetalComboBoxUI();
  36. }
  37. public void paint(Graphics g, JComponent c) {
  38. }
  39. protected ComboBoxEditor createEditor() {
  40. return new MetalComboBoxEditor.UIResource();
  41. }
  42. protected ComboPopup createPopup() {
  43. return new MetalComboPopup( comboBox );
  44. }
  45. protected JButton createArrowButton() {
  46. JButton button = new MetalComboBoxButton( comboBox,
  47. new MetalComboBoxIcon(),
  48. comboBox.isEditable(),
  49. currentValuePane,
  50. listBox );
  51. button.setMargin( new Insets( 0, 1, 1, 3 ) );
  52. return button;
  53. }
  54. public PropertyChangeListener createPropertyChangeListener() {
  55. return new MetalPropertyChangeListener();
  56. }
  57. /**
  58. * This inner class is marked "public" due to a compiler bug.
  59. * This class should be treated as a "protected" inner class.
  60. * Instantiate it only within subclasses of <FooUI>.
  61. */
  62. public class MetalPropertyChangeListener extends BasicComboBoxUI.PropertyChangeHandler {
  63. public void propertyChange(PropertyChangeEvent e) {
  64. super.propertyChange( e );
  65. String propertyName = e.getPropertyName();
  66. if ( propertyName.equals( "editable" ) ) {
  67. MetalComboBoxButton button = (MetalComboBoxButton)arrowButton;
  68. button.setIconOnly( comboBox.isEditable() );
  69. comboBox.repaint();
  70. } else if ( propertyName.equals( "background" ) ) {
  71. Color color = (Color)e.getNewValue();
  72. arrowButton.setBackground(color);
  73. listBox.setBackground(color);
  74. } else if ( propertyName.equals( "foreground" ) ) {
  75. Color color = (Color)e.getNewValue();
  76. arrowButton.setForeground(color);
  77. listBox.setForeground(color);
  78. }
  79. }
  80. }
  81. /**
  82. * As of Java 2 platform v1.4 this method is no longer used. Do not call or
  83. * override. All the functionality of this method is in the
  84. * MetalPropertyChangeListener.
  85. *
  86. * @deprecated As of Java 2 platform v1.4.
  87. */
  88. protected void editablePropertyChanged( PropertyChangeEvent e ) { }
  89. protected LayoutManager createLayoutManager() {
  90. return new MetalComboBoxLayoutManager();
  91. }
  92. /**
  93. * This inner class is marked "public" due to a compiler bug.
  94. * This class should be treated as a "protected" inner class.
  95. * Instantiate it only within subclasses of <FooUI>.
  96. */
  97. public class MetalComboBoxLayoutManager extends BasicComboBoxUI.ComboBoxLayoutManager {
  98. public void layoutContainer( Container parent ) {
  99. layoutComboBox( parent, this );
  100. }
  101. public void superLayout( Container parent ) {
  102. super.layoutContainer( parent );
  103. }
  104. }
  105. // This is here because of a bug in the compiler.
  106. // When a protected-inner-class-savvy compiler comes out we
  107. // should move this into MetalComboBoxLayoutManager.
  108. public void layoutComboBox( Container parent, MetalComboBoxLayoutManager manager ) {
  109. if ( comboBox.isEditable() ) {
  110. manager.superLayout( parent );
  111. }
  112. else {
  113. if ( arrowButton != null ) {
  114. Insets insets = comboBox.getInsets();
  115. int width = comboBox.getWidth();
  116. int height = comboBox.getHeight();
  117. arrowButton.setBounds( insets.left, insets.top,
  118. width - (insets.left + insets.right),
  119. height - (insets.top + insets.bottom) );
  120. }
  121. }
  122. }
  123. /**
  124. * As of Java 2 platform v1.4 this method is no
  125. * longer used.
  126. *
  127. * @deprecated As of Java 2 platform v1.4.
  128. */
  129. protected void removeListeners() {
  130. if ( propertyChangeListener != null ) {
  131. comboBox.removePropertyChangeListener( propertyChangeListener );
  132. }
  133. }
  134. // These two methods were overloaded and made public. This was probably a
  135. // mistake in the implementation. The functionality that they used to
  136. // provide is no longer necessary and should be removed. However,
  137. // removing them will create an uncompatible API change.
  138. public void configureEditor() {
  139. super.configureEditor();
  140. }
  141. public void unconfigureEditor() {
  142. super.unconfigureEditor();
  143. }
  144. public Dimension getMinimumSize( JComponent c ) {
  145. if ( !isMinimumSizeDirty ) {
  146. return new Dimension( cachedMinimumSize );
  147. }
  148. Dimension size = null;
  149. if ( !comboBox.isEditable() &&
  150. arrowButton != null &&
  151. arrowButton instanceof MetalComboBoxButton ) {
  152. MetalComboBoxButton button = (MetalComboBoxButton)arrowButton;
  153. Insets buttonInsets = button.getInsets();
  154. Insets insets = comboBox.getInsets();
  155. size = getDisplaySize();
  156. size.width += insets.left + insets.right;
  157. size.width += buttonInsets.left + buttonInsets.right;
  158. size.width += buttonInsets.right + button.getComboIcon().getIconWidth();
  159. size.height += insets.top + insets.bottom;
  160. size.height += buttonInsets.top + buttonInsets.bottom;
  161. }
  162. else if ( comboBox.isEditable() &&
  163. arrowButton != null &&
  164. editor != null ) {
  165. size = super.getMinimumSize( c );
  166. Insets margin = arrowButton.getMargin();
  167. size.height += margin.top + margin.bottom;
  168. size.width += margin.left + margin.right;
  169. }
  170. else {
  171. size = super.getMinimumSize( c );
  172. }
  173. cachedMinimumSize.setSize( size.width, size.height );
  174. isMinimumSizeDirty = false;
  175. return new Dimension( cachedMinimumSize );
  176. }
  177. /**
  178. * This inner class is marked "public" due to a compiler bug.
  179. * This class should be treated as a "protected" inner class.
  180. * Instantiate it only within subclasses of <FooUI>.
  181. *
  182. * This class is now obsolete and doesn't do anything and
  183. * is only included for backwards API compatibility. Do not call or
  184. * override.
  185. *
  186. * @deprecated As of Java 2 platform v1.4.
  187. */
  188. public class MetalComboPopup extends BasicComboPopup {
  189. public MetalComboPopup( JComboBox cBox) {
  190. super( cBox );
  191. }
  192. // This method was overloaded and made public. This was probably
  193. // mistake in the implementation. The functionality that they used to
  194. // provide is no longer necessary and should be removed. However,
  195. // removing them will create an uncompatible API change.
  196. public void delegateFocus(MouseEvent e) {
  197. super.delegateFocus(e);
  198. }
  199. }
  200. }