1. /*
  2. * @(#)WindowsComboBoxUI.java 1.25 00/02/02
  3. *
  4. * Copyright 1997-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 com.sun.java.swing.plaf.windows;
  11. import javax.swing.plaf.basic.*;
  12. import javax.swing.plaf.*;
  13. import javax.swing.border.*;
  14. import javax.swing.*;
  15. import java.awt.event.*;
  16. import java.awt.*;
  17. /**
  18. * Windows combo box.
  19. * <p>
  20. * <strong>Warning:</strong>
  21. * Serialized objects of this class will not be compatible with
  22. * future Swing releases. The current serialization support is appropriate
  23. * for short term storage or RMI between applications running the same
  24. * version of Swing. A future release of Swing will provide support for
  25. * long term persistence.
  26. *
  27. * @version 1.25, 02/02/00
  28. * @author Tom Santos
  29. */
  30. public class WindowsComboBoxUI extends BasicComboBoxUI {
  31. private static final JTextField sizer = new JTextField();
  32. public static ComponentUI createUI(JComponent c) {
  33. return new WindowsComboBoxUI();
  34. }
  35. public void installUI( JComponent c ) {
  36. super.installUI( c );
  37. comboBox.setRequestFocusEnabled( true );
  38. }
  39. public Dimension getMinimumSize( JComponent c ) {
  40. if ( !isMinimumSizeDirty ) {
  41. return new Dimension( cachedMinimumSize );
  42. }
  43. Dimension size = getDisplaySize();
  44. Insets insets = getInsets();
  45. if ( comboBox.getRenderer() instanceof UIResource ) {
  46. sizer.setFont( comboBox.getFont() );
  47. size.height = sizer.getPreferredSize().height;
  48. }
  49. else {
  50. size.height += insets.top + insets.bottom;
  51. }
  52. int buttonSize = size.height - (insets.top + insets.bottom);
  53. size.width += insets.left + insets.right + buttonSize;
  54. cachedMinimumSize.setSize( size.width, size.height );
  55. isMinimumSizeDirty = false;
  56. return size;
  57. }
  58. protected void selectNextPossibleValue() {
  59. super.selectNextPossibleValue();
  60. }
  61. protected void selectPreviousPossibleValue() {
  62. super.selectPreviousPossibleValue();
  63. }
  64. protected void installKeyboardActions() {
  65. ActionMap oldMap = (ActionMap)UIManager.get("ComboBox.actionMap");
  66. super.installKeyboardActions();
  67. if (oldMap == null) {
  68. ActionMap map = (ActionMap)UIManager.get("ComboBox.actionMap");
  69. if (map != null) {
  70. // The actions we install are a little different, override
  71. // them here.
  72. map.put("selectPrevious", new UpAction());
  73. map.put("selectNext", new DownAction());
  74. }
  75. }
  76. }
  77. void windowsSetPopupVisible( boolean visible ) {
  78. setPopupVisible( comboBox, visible );
  79. }
  80. protected ComboPopup createPopup() {
  81. return new WindowsComboPopup( comboBox );
  82. }
  83. /**
  84. * Subclassed to add Windows specific Key Bindings.
  85. */
  86. protected class WindowsComboPopup extends BasicComboPopup {
  87. public WindowsComboPopup( JComboBox cBox ) {
  88. super( cBox );
  89. }
  90. protected KeyListener createKeyListener() {
  91. return new InvocationKeyHandler();
  92. }
  93. protected class InvocationKeyHandler extends BasicComboPopup.InvocationKeyHandler {
  94. public void keyReleased( KeyEvent e ) {
  95. if ( e.getKeyCode() == KeyEvent.VK_F4 ) {
  96. if ( isVisible() ) {
  97. hide();
  98. }
  99. else {
  100. show();
  101. }
  102. }
  103. else if ( e.isAltDown() && e.getKeyCode() != KeyEvent.VK_ALT ) {
  104. if ( e.getKeyCode() == KeyEvent.VK_UP ||
  105. e.getKeyCode() == KeyEvent.VK_DOWN ) {
  106. if ( isVisible() ) {
  107. hide();
  108. }
  109. else {
  110. show();
  111. }
  112. }
  113. }
  114. else if ( comboBox.isEditable() &&
  115. !isVisible() &&
  116. (e.getKeyCode() == KeyEvent.VK_UP ||
  117. e.getKeyCode() == KeyEvent.VK_DOWN) ) {
  118. show();
  119. }
  120. else if ( !comboBox.isEditable() && isVisible() ) {
  121. super.keyReleased( e );
  122. }
  123. }
  124. }
  125. }
  126. static class DownAction extends AbstractAction {
  127. public void actionPerformed(ActionEvent e) {
  128. JComboBox comboBox = (JComboBox)e.getSource();
  129. if ( comboBox.isEnabled() ) {
  130. WindowsComboBoxUI ui = (WindowsComboBoxUI)comboBox.getUI();
  131. if ( !comboBox.isEditable() || (comboBox.isEditable() &&
  132. ui.isPopupVisible(comboBox))) {
  133. ui.selectNextPossibleValue();
  134. }
  135. }
  136. }
  137. }
  138. static class UpAction extends AbstractAction {
  139. public void actionPerformed(ActionEvent e) {
  140. JComboBox comboBox = (JComboBox)e.getSource();
  141. if ( comboBox.isEnabled() ) {
  142. WindowsComboBoxUI ui = (WindowsComboBoxUI)comboBox.getUI();
  143. if ( !comboBox.isEditable() || (comboBox.isEditable() &&
  144. ui.isPopupVisible(comboBox))) {
  145. ui.selectPreviousPossibleValue();
  146. }
  147. }
  148. }
  149. }
  150. }