1. /*
  2. * @(#)WindowsTextFieldUI.java 1.23 03/12/19
  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 java.awt.*;
  9. import java.awt.event.*;
  10. import java.beans.PropertyChangeEvent;
  11. import javax.swing.plaf.*;
  12. import javax.swing.plaf.basic.BasicTextFieldUI;
  13. import javax.swing.text.*;
  14. import javax.swing.*;
  15. import javax.swing.plaf.UIResource;
  16. import sun.swing.DefaultLookup;
  17. /**
  18. * Provides the Windows look and feel for a text field. This
  19. * is basically the following customizations to the default
  20. * look-and-feel.
  21. * <ul>
  22. * <li>The border is beveled (using the standard control color).
  23. * <li>The background is white by default.
  24. * <li>The highlight color is a dark color, blue by default.
  25. * <li>The foreground color is high contrast in the selected
  26. * area, white by default. The unselected foreground is black.
  27. * <li>The cursor blinks at about 1/2 second intervals.
  28. * <li>The entire value is selected when focus is gained.
  29. * <li>Shift-left-arrow and shift-right-arrow extend selection
  30. * <li>Cntrl-left-arrow and cntrl-right-arrow act like home and
  31. * end respectively.
  32. * </ul>
  33. * <p>
  34. * <strong>Warning:</strong>
  35. * Serialized objects of this class will not be compatible with
  36. * future Swing releases. The current serialization support is appropriate
  37. * for short term storage or RMI between applications running the same
  38. * version of Swing. A future release of Swing will provide support for
  39. * long term persistence.
  40. *
  41. * @author Timothy Prinzing
  42. * @version 1.23 12/19/03
  43. */
  44. public class WindowsTextFieldUI extends BasicTextFieldUI
  45. {
  46. /**
  47. * Creates a UI for a JTextField.
  48. *
  49. * @param c the text field
  50. * @return the UI
  51. */
  52. public static ComponentUI createUI(JComponent c) {
  53. return new WindowsTextFieldUI();
  54. }
  55. /**
  56. * Paints a background for the view. This will only be
  57. * called if isOpaque() on the associated component is
  58. * true. The default is to paint the background color
  59. * of the component.
  60. *
  61. * @param g the graphics context
  62. */
  63. protected void paintBackground(Graphics g) {
  64. super.paintBackground(g);
  65. }
  66. /**
  67. * Creates the caret for a field.
  68. *
  69. * @return the caret
  70. */
  71. protected Caret createCaret() {
  72. return new WindowsFieldCaret();
  73. }
  74. /**
  75. * WindowsFieldCaret has different scrolling behavior than
  76. * DefaultCaret.
  77. */
  78. static class WindowsFieldCaret extends DefaultCaret implements UIResource {
  79. public WindowsFieldCaret() {
  80. super();
  81. }
  82. /**
  83. * Adjusts the visibility of the caret according to
  84. * the windows feel which seems to be to move the
  85. * caret out into the field by about a quarter of
  86. * a field length if not visible.
  87. */
  88. protected void adjustVisibility(Rectangle r) {
  89. SwingUtilities.invokeLater(new SafeScroller(r));
  90. }
  91. /**
  92. * Gets the painter for the Highlighter.
  93. *
  94. * @return the painter
  95. */
  96. protected Highlighter.HighlightPainter getSelectionPainter() {
  97. return WindowsTextUI.WindowsPainter;
  98. }
  99. private class SafeScroller implements Runnable {
  100. SafeScroller(Rectangle r) {
  101. this.r = r;
  102. }
  103. public void run() {
  104. JTextField field = (JTextField) getComponent();
  105. if (field != null) {
  106. TextUI ui = field.getUI();
  107. int dot = getDot();
  108. // PENDING: We need to expose the bias in DefaultCaret.
  109. Position.Bias bias = Position.Bias.Forward;
  110. Rectangle startRect = null;
  111. try {
  112. startRect = ui.modelToView(field, dot, bias);
  113. } catch (BadLocationException ble) {}
  114. BoundedRangeModel vis = field.getHorizontalVisibility();
  115. int x = r.x + vis.getValue();
  116. int quarterSpan = vis.getExtent() / 4;
  117. if (x < vis.getValue()) {
  118. vis.setValue(x - quarterSpan);
  119. } else if (x > vis.getValue() + vis.getExtent()) {
  120. vis.setValue(x - (3 * quarterSpan));
  121. }
  122. // If we scroll, our visual location will have changed,
  123. // but we won't have updated our internal location as
  124. // the model hasn't changed. This checks for the change,
  125. // and if necessary, resets the internal location.
  126. if (startRect != null) {
  127. try {
  128. Rectangle endRect;
  129. endRect = ui.modelToView(field, dot, bias);
  130. if (endRect != null && !endRect.equals(startRect)){
  131. damage(endRect);
  132. }
  133. } catch (BadLocationException ble) {}
  134. }
  135. }
  136. }
  137. private Rectangle r;
  138. }
  139. }
  140. }