1. /*
  2. * @(#)WindowsTextFieldUI.java 1.20 03/06/24
  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 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.20 06/24/03
  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. * Paints a background for the view. This will only be
  55. * called if isOpaque() on the associated component is
  56. * true. The default is to paint the background color
  57. * of the component.
  58. *
  59. * @param g the graphics context
  60. */
  61. protected void paintBackground(Graphics g) {
  62. XPStyle xp = XPStyle.getXP();
  63. JTextComponent editor = getComponent();
  64. Color bgColor = editor.getBackground();
  65. if (xp != null && (bgColor == null || bgColor instanceof UIResource)) {
  66. String key;
  67. if (!editor.isEnabled()) {
  68. key = "edit.edittext(disabled).fillcolor";
  69. } else if (!editor.isEditable()) {
  70. key = "edit.edittext(readonly).fillcolor";
  71. } else {
  72. key = "edit.fillcolor";
  73. }
  74. g.setColor(xp.getColor(key, bgColor));
  75. g.fillRect(0, 0, editor.getWidth(), editor.getHeight());
  76. } else {
  77. super.paintBackground(g);
  78. }
  79. }
  80. /**
  81. * Creates the caret for a field.
  82. *
  83. * @return the caret
  84. */
  85. protected Caret createCaret() {
  86. return new WindowsFieldCaret();
  87. }
  88. /**
  89. * WindowsFieldCaret has different scrolling behavior than
  90. * DefaultCaret.
  91. */
  92. static class WindowsFieldCaret extends DefaultCaret implements UIResource {
  93. public WindowsFieldCaret() {
  94. super();
  95. }
  96. /**
  97. * Adjusts the visibility of the caret according to
  98. * the windows feel which seems to be to move the
  99. * caret out into the field by about a quarter of
  100. * a field length if not visible.
  101. */
  102. protected void adjustVisibility(Rectangle r) {
  103. SwingUtilities.invokeLater(new SafeScroller(r));
  104. }
  105. /**
  106. * Gets the painter for the Highlighter.
  107. *
  108. * @return the painter
  109. */
  110. protected Highlighter.HighlightPainter getSelectionPainter() {
  111. return WindowsTextUI.WindowsPainter;
  112. }
  113. private class SafeScroller implements Runnable {
  114. SafeScroller(Rectangle r) {
  115. this.r = r;
  116. }
  117. public void run() {
  118. JTextField field = (JTextField) getComponent();
  119. if (field != null) {
  120. TextUI ui = field.getUI();
  121. int dot = getDot();
  122. // PENDING: We need to expose the bias in DefaultCaret.
  123. Position.Bias bias = Position.Bias.Forward;
  124. Rectangle startRect = null;
  125. try {
  126. startRect = ui.modelToView(field, dot, bias);
  127. } catch (BadLocationException ble) {}
  128. BoundedRangeModel vis = field.getHorizontalVisibility();
  129. int x = r.x + vis.getValue();
  130. int quarterSpan = vis.getExtent() / 4;
  131. if (x < vis.getValue()) {
  132. vis.setValue(x - quarterSpan);
  133. } else if (x > vis.getValue() + vis.getExtent()) {
  134. vis.setValue(x - (3 * quarterSpan));
  135. }
  136. // If we scroll, our visual location will have changed,
  137. // but we won't have updated our internal location as
  138. // the model hasn't changed. This checks for the change,
  139. // and if necessary, resets the internal location.
  140. if (startRect != null) {
  141. try {
  142. Rectangle endRect;
  143. endRect = ui.modelToView(field, dot, bias);
  144. if (endRect != null && !endRect.equals(startRect)){
  145. damage(endRect);
  146. }
  147. } catch (BadLocationException ble) {}
  148. }
  149. }
  150. }
  151. private Rectangle r;
  152. }
  153. }
  154. }