1. /*
  2. * @(#)WindowsTextFieldUI.java 1.15 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 java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.plaf.*;
  14. import javax.swing.plaf.basic.BasicTextFieldUI;
  15. import javax.swing.text.*;
  16. import javax.swing.*;
  17. import javax.swing.plaf.UIResource;
  18. /**
  19. * Provides the Windows look and feel for a text field. This
  20. * is basically the following customizations to the default
  21. * look-and-feel.
  22. * <ul>
  23. * <li>The border is beveled (using the standard control color).
  24. * <li>The background is white by default.
  25. * <li>The highlight color is a dark color, blue by default.
  26. * <li>The foreground color is high contrast in the selected
  27. * area, white by default. The unselected foreground is black.
  28. * <li>The cursor blinks at about 1/2 second intervals.
  29. * <li>The entire value is selected when focus is gained.
  30. * <li>Shift-left-arrow and shift-right-arrow extend selection
  31. * <li>Cntrl-left-arrow and cntrl-right-arrow act like home and
  32. * end respectively.
  33. * </ul>
  34. * <p>
  35. * <strong>Warning:</strong>
  36. * Serialized objects of this class will not be compatible with
  37. * future Swing releases. The current serialization support is appropriate
  38. * for short term storage or RMI between applications running the same
  39. * version of Swing. A future release of Swing will provide support for
  40. * long term persistence.
  41. *
  42. * @author Timothy Prinzing
  43. * @version 1.15 02/02/00
  44. */
  45. public class WindowsTextFieldUI extends BasicTextFieldUI
  46. {
  47. /**
  48. * Creates a UI for a JTextField.
  49. *
  50. * @param c the text field
  51. * @return the UI
  52. */
  53. public static ComponentUI createUI(JComponent c) {
  54. return new WindowsTextFieldUI();
  55. }
  56. /**
  57. * Creates the caret for a field.
  58. *
  59. * @return the caret
  60. */
  61. protected Caret createCaret() {
  62. return new WindowsFieldCaret();
  63. }
  64. /**
  65. * WindowsFieldCaret has different scrolling behavior than
  66. * DefaultCaret.
  67. */
  68. static class WindowsFieldCaret extends DefaultCaret implements UIResource {
  69. public WindowsFieldCaret() {
  70. super();
  71. }
  72. /**
  73. * Adjusts the visibility of the caret according to
  74. * the windows feel which seems to be to move the
  75. * caret out into the field by about a quarter of
  76. * a field length if not visible.
  77. */
  78. protected void adjustVisibility(Rectangle r) {
  79. JTextField field = (JTextField) getComponent();
  80. BoundedRangeModel vis = field.getHorizontalVisibility();
  81. int x = r.x + vis.getValue();
  82. int quarterSpan = vis.getExtent() / 4;
  83. if (x < vis.getValue()) {
  84. vis.setValue(x - quarterSpan);
  85. } else if (x > vis.getValue() + vis.getExtent()) {
  86. vis.setValue(x - (3 * quarterSpan));
  87. }
  88. }
  89. /**
  90. * Gets the painter for the Highlighter.
  91. *
  92. * @return the painter
  93. */
  94. protected Highlighter.HighlightPainter getSelectionPainter() {
  95. return WindowsTextUI.WindowsPainter;
  96. }
  97. }
  98. }