1. /*
  2. * @(#)WindowsComboBoxUI.java 1.36 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 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.36, 01/23/03
  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. * Creates a layout manager for managing the components which make up the
  37. * combo box.
  38. *
  39. * @return an instance of a layout manager
  40. */
  41. protected LayoutManager createLayoutManager() {
  42. return new BasicComboBoxUI.ComboBoxLayoutManager() {
  43. public void layoutContainer(Container parent) {
  44. super.layoutContainer(parent);
  45. if (XPStyle.getXP() != null && arrowButton != null) {
  46. Dimension d = parent.getSize();
  47. Insets insets = getInsets();
  48. int buttonWidth = arrowButton.getPreferredSize().width;
  49. arrowButton.setBounds(d.width - insets.right - buttonWidth, insets.top,
  50. buttonWidth, d.height - insets.top - insets.bottom);
  51. }
  52. }
  53. };
  54. }
  55. protected void installKeyboardActions() {
  56. super.installKeyboardActions();
  57. ActionMap map = SwingUtilities.getUIActionMap(comboBox);
  58. if (map != null) {
  59. map.put("selectPrevious", new UpAction());
  60. map.put("selectNext", new DownAction());
  61. }
  62. }
  63. protected ComboPopup createPopup() {
  64. return new WindowsComboPopup( comboBox );
  65. }
  66. /**
  67. * Creates the default editor that will be used in editable combo boxes.
  68. * A default editor will be used only if an editor has not been
  69. * explicitly set with <code>setEditor</code>.
  70. *
  71. * @return a <code>ComboBoxEditor</code> used for the combo box
  72. * @see javax.swing.JComboBox#setEditor
  73. */
  74. protected ComboBoxEditor createEditor() {
  75. return new WindowsComboBoxEditor();
  76. }
  77. /**
  78. * Creates an button which will be used as the control to show or hide
  79. * the popup portion of the combo box.
  80. *
  81. * @return a button which represents the popup control
  82. */
  83. protected JButton createArrowButton() {
  84. if (XPStyle.getXP() != null) {
  85. return new XPComboBoxButton();
  86. } else {
  87. return super.createArrowButton();
  88. }
  89. }
  90. private static class XPComboBoxButton extends XPStyle.GlyphButton {
  91. public XPComboBoxButton() {
  92. super("combobox.dropdownbutton");
  93. setRequestFocusEnabled(false);
  94. }
  95. public Dimension getPreferredSize() {
  96. return new Dimension(17, 20);
  97. }
  98. }
  99. /**
  100. * Subclassed to add Windows specific Key Bindings.
  101. * This class is now obsolete and doesn't do anything.
  102. * Only included for backwards API compatibility.
  103. * Do not call or override.
  104. *
  105. * @deprecated As of Java 2 platform v1.4.
  106. */
  107. protected class WindowsComboPopup extends BasicComboPopup {
  108. public WindowsComboPopup( JComboBox cBox ) {
  109. super( cBox );
  110. }
  111. protected KeyListener createKeyListener() {
  112. return new InvocationKeyHandler();
  113. }
  114. protected class InvocationKeyHandler extends BasicComboPopup.InvocationKeyHandler {
  115. protected InvocationKeyHandler() {
  116. WindowsComboPopup.this.super();
  117. }
  118. }
  119. }
  120. /**
  121. * Subclassed to highlight selected item in an editable combo box.
  122. */
  123. public static class WindowsComboBoxEditor
  124. extends BasicComboBoxEditor.UIResource {
  125. public void setItem(Object item) {
  126. super.setItem(item);
  127. editor.selectAll();
  128. }
  129. }
  130. static class DownAction extends AbstractAction {
  131. public void actionPerformed(ActionEvent e) {
  132. JComboBox comboBox = (JComboBox)e.getSource();
  133. if ( comboBox.isEnabled() ) {
  134. WindowsComboBoxUI ui = (WindowsComboBoxUI)comboBox.getUI();
  135. if (comboBox.isEditable() && !ui.isPopupVisible(comboBox)) {
  136. ui.setPopupVisible(comboBox, true);
  137. } else {
  138. ui.selectNextPossibleValue();
  139. }
  140. }
  141. }
  142. }
  143. static class UpAction extends AbstractAction {
  144. public void actionPerformed(ActionEvent e) {
  145. JComboBox comboBox = (JComboBox)e.getSource();
  146. if ( comboBox.isEnabled() ) {
  147. WindowsComboBoxUI ui = (WindowsComboBoxUI)comboBox.getUI();
  148. if (comboBox.isEditable() && !ui.isPopupVisible(comboBox)) {
  149. ui.setPopupVisible(comboBox, true);
  150. } else {
  151. ui.selectPreviousPossibleValue();
  152. }
  153. }
  154. }
  155. }
  156. }