1. /*
  2. * @(#)WindowsTextFieldUI.java 1.13 01/11/29
  3. *
  4. * Copyright 2002 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 java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.plaf.*;
  11. import javax.swing.plaf.basic.BasicTextFieldUI;
  12. import javax.swing.text.*;
  13. import javax.swing.*;
  14. import javax.swing.plaf.UIResource;
  15. /**
  16. * Provides the Windows look and feel for a text field. This
  17. * is basically the following customizations to the default
  18. * look-and-feel.
  19. * <ul>
  20. * <li>The border is beveled (using the standard control color).
  21. * <li>The background is white by default.
  22. * <li>The highlight color is a dark color, blue by default.
  23. * <li>The foreground color is high contrast in the selected
  24. * area, white by default. The unselected foreground is black.
  25. * <li>The cursor blinks at about 1/2 second intervals.
  26. * <li>The entire value is selected when focus is gained.
  27. * <li>Shift-left-arrow and shift-right-arrow extend selection
  28. * <li>Cntrl-left-arrow and cntrl-right-arrow act like home and
  29. * end respectively.
  30. * </ul>
  31. * <p>
  32. * <strong>Warning:</strong>
  33. * Serialized objects of this class will not be compatible with
  34. * future Swing releases. The current serialization support is appropriate
  35. * for short term storage or RMI between applications running the same
  36. * version of Swing. A future release of Swing will provide support for
  37. * long term persistence.
  38. *
  39. * @author Timothy Prinzing
  40. * @version 1.13 11/29/01
  41. */
  42. public class WindowsTextFieldUI extends BasicTextFieldUI
  43. {
  44. /**
  45. * Creates a UI for a JTextField.
  46. *
  47. * @param c the text field
  48. * @return the UI
  49. */
  50. public static ComponentUI createUI(JComponent c) {
  51. return new WindowsTextFieldUI();
  52. }
  53. /**
  54. * Creates the caret for a field.
  55. *
  56. * @return the caret
  57. */
  58. protected Caret createCaret() {
  59. return new WindowsFieldCaret();
  60. }
  61. /**
  62. * WindowsFieldCaret has different scrolling behavior than
  63. * DefaultCaret, selects the field when focus enters it, and
  64. * deselects the field when focus leaves.
  65. */
  66. static class WindowsFieldCaret extends DefaultCaret implements UIResource {
  67. /** Set to true in focusLost if the FocusEvent is temporary. */
  68. transient boolean temporaryLoss;
  69. /** Set if the focus is lost temporarily, is used to properly
  70. * restore after a temporary loss. */
  71. transient int tempDot, tempMark;
  72. public WindowsFieldCaret() {
  73. super();
  74. }
  75. /**
  76. * Called when the component containing the caret gains
  77. * focus. This is implemented to set the caret to visible
  78. * if the component is editable, and sets the selection
  79. * to visible.
  80. *
  81. * @param e the focus event
  82. * @see FocusListener#focusGained
  83. */
  84. public void focusGained(FocusEvent e) {
  85. super.focusGained(e);
  86. JTextComponent c = getComponent();
  87. Document doc = c.getDocument();
  88. int length = doc.getLength();
  89. if (!temporaryLoss) {
  90. setDot(0);
  91. moveDot(length);
  92. }
  93. else {
  94. setDot(Math.min(tempMark, length));
  95. moveDot(Math.min(tempDot, length));
  96. temporaryLoss = false;
  97. }
  98. }
  99. /**
  100. * Called when the component containing the caret loses
  101. * focus. This is implemented to set the caret to visibility
  102. * to false, and to set the selection visibility to false.
  103. *
  104. * @param e the focus event
  105. * @see FocusListener#focusLost
  106. */
  107. public void focusLost(FocusEvent e) {
  108. if (e != null) {
  109. temporaryLoss = e.isTemporary();
  110. if (temporaryLoss) {
  111. tempDot = getDot();
  112. tempMark = getMark();
  113. }
  114. }
  115. else {
  116. temporaryLoss = false;
  117. }
  118. setDot(getDot());
  119. setVisible(false);
  120. }
  121. /**
  122. * Adjusts the visibility of the caret according to
  123. * the windows feel which seems to be to move the
  124. * caret out into the field by about a quarter of
  125. * a field length if not visible.
  126. */
  127. protected void adjustVisibility(Rectangle r) {
  128. JTextField field = (JTextField) getComponent();
  129. BoundedRangeModel vis = field.getHorizontalVisibility();
  130. int x = r.x + vis.getValue();
  131. int quarterSpan = vis.getExtent() / 4;
  132. if (x < vis.getValue()) {
  133. vis.setValue(x - quarterSpan);
  134. } else if (x > vis.getValue() + vis.getExtent()) {
  135. vis.setValue(x - (3 * quarterSpan));
  136. }
  137. }
  138. /**
  139. * Gets the painter for the Highlighter.
  140. *
  141. * @return the painter
  142. */
  143. protected Highlighter.HighlightPainter getSelectionPainter() {
  144. return WindowsTextUI.WindowsPainter;
  145. }
  146. }
  147. }