1. /*
  2. * @(#)WindowsComboBoxUI.java 1.44 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.windows;
  8. import javax.swing.plaf.basic.*;
  9. import javax.swing.plaf.*;
  10. import javax.swing.border.*;
  11. import javax.swing.*;
  12. import java.awt.event.*;
  13. import java.awt.*;
  14. /**
  15. * Windows combo box.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is appropriate
  20. * for short term storage or RMI between applications running the same
  21. * version of Swing. A future release of Swing will provide support for
  22. * long term persistence.
  23. *
  24. * @version 1.44, 05/18/04
  25. * @author Tom Santos
  26. */
  27. public class WindowsComboBoxUI extends BasicComboBoxUI {
  28. public static ComponentUI createUI(JComponent c) {
  29. return new WindowsComboBoxUI();
  30. }
  31. public void installUI( JComponent c ) {
  32. super.installUI( c );
  33. comboBox.setRequestFocusEnabled( true );
  34. }
  35. /**
  36. * If necessary paints the currently selected item.
  37. *
  38. * @param g Graphics to paint to
  39. * @param bounds Region to paint current value to
  40. * @param hasFocus whether or not the JComboBox has focus
  41. * @throws NullPointerException if any of the arguments are null.
  42. * @since 1.5
  43. */
  44. public void paintCurrentValue(Graphics g, Rectangle bounds,
  45. boolean hasFocus) {
  46. if (XPStyle.getXP() != null) {
  47. bounds.x += 2;
  48. bounds.y += 2;
  49. bounds.width -= 3;
  50. bounds.height -= 4;
  51. } else {
  52. bounds.x += 1;
  53. bounds.y += 1;
  54. bounds.width -= 2;
  55. bounds.height -= 2;
  56. }
  57. super.paintCurrentValue(g, bounds, hasFocus);
  58. }
  59. public Dimension getPreferredSize( JComponent c ) {
  60. Dimension d = super.getPreferredSize(c);
  61. d.width += 4;
  62. d.height += 2;
  63. if (XPStyle.getXP() != null) {
  64. d.height += 2;
  65. }
  66. return d;
  67. }
  68. /**
  69. * Creates a layout manager for managing the components which make up the
  70. * combo box.
  71. *
  72. * @return an instance of a layout manager
  73. */
  74. protected LayoutManager createLayoutManager() {
  75. return new BasicComboBoxUI.ComboBoxLayoutManager() {
  76. public void layoutContainer(Container parent) {
  77. super.layoutContainer(parent);
  78. if (XPStyle.getXP() != null && arrowButton != null) {
  79. Dimension d = parent.getSize();
  80. Insets insets = getInsets();
  81. int buttonWidth = arrowButton.getPreferredSize().width;
  82. arrowButton.setBounds(WindowsUtils.isLeftToRight((JComboBox)parent)
  83. ? (d.width - insets.right - buttonWidth)
  84. : insets.left,
  85. insets.top,
  86. buttonWidth, d.height - insets.top - insets.bottom);
  87. }
  88. }
  89. };
  90. }
  91. protected void installKeyboardActions() {
  92. super.installKeyboardActions();
  93. }
  94. protected ComboPopup createPopup() {
  95. return super.createPopup();
  96. }
  97. /**
  98. * Creates the default editor that will be used in editable combo boxes.
  99. * A default editor will be used only if an editor has not been
  100. * explicitly set with <code>setEditor</code>.
  101. *
  102. * @return a <code>ComboBoxEditor</code> used for the combo box
  103. * @see javax.swing.JComboBox#setEditor
  104. */
  105. protected ComboBoxEditor createEditor() {
  106. return new WindowsComboBoxEditor();
  107. }
  108. /**
  109. * Creates an button which will be used as the control to show or hide
  110. * the popup portion of the combo box.
  111. *
  112. * @return a button which represents the popup control
  113. */
  114. protected JButton createArrowButton() {
  115. if (XPStyle.getXP() != null) {
  116. return new XPComboBoxButton();
  117. } else {
  118. return super.createArrowButton();
  119. }
  120. }
  121. private static class XPComboBoxButton extends XPStyle.GlyphButton {
  122. public XPComboBoxButton() {
  123. super("combobox.dropdownbutton");
  124. setRequestFocusEnabled(false);
  125. }
  126. public Dimension getPreferredSize() {
  127. return new Dimension(17, 20);
  128. }
  129. }
  130. /**
  131. * Subclassed to add Windows specific Key Bindings.
  132. * This class is now obsolete and doesn't do anything.
  133. * Only included for backwards API compatibility.
  134. * Do not call or override.
  135. *
  136. * @deprecated As of Java 2 platform v1.4.
  137. */
  138. @Deprecated
  139. protected class WindowsComboPopup extends BasicComboPopup {
  140. public WindowsComboPopup( JComboBox cBox ) {
  141. super( cBox );
  142. }
  143. protected KeyListener createKeyListener() {
  144. return new InvocationKeyHandler();
  145. }
  146. protected class InvocationKeyHandler extends BasicComboPopup.InvocationKeyHandler {
  147. protected InvocationKeyHandler() {
  148. WindowsComboPopup.this.super();
  149. }
  150. }
  151. }
  152. /**
  153. * Subclassed to highlight selected item in an editable combo box.
  154. */
  155. public static class WindowsComboBoxEditor
  156. extends BasicComboBoxEditor.UIResource {
  157. public void setItem(Object item) {
  158. super.setItem(item);
  159. if (editor.hasFocus()) {
  160. editor.selectAll();
  161. }
  162. }
  163. }
  164. }