KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsTextFieldUI


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
8 package com.sun.java.swing.plaf.windows;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import java.beans.PropertyChangeEvent JavaDoc;
13 import javax.swing.plaf.*;
14 import javax.swing.plaf.basic.BasicTextFieldUI JavaDoc;
15 import javax.swing.text.*;
16 import javax.swing.*;
17 import javax.swing.plaf.UIResource JavaDoc;
18 import sun.swing.DefaultLookup;
19
20
21
22 /**
23  * Provides the Windows look and feel for a text field. This
24  * is basically the following customizations to the default
25  * look-and-feel.
26  * <ul>
27  * <li>The border is beveled (using the standard control color).
28  * <li>The background is white by default.
29  * <li>The highlight color is a dark color, blue by default.
30  * <li>The foreground color is high contrast in the selected
31  * area, white by default. The unselected foreground is black.
32  * <li>The cursor blinks at about 1/2 second intervals.
33  * <li>The entire value is selected when focus is gained.
34  * <li>Shift-left-arrow and shift-right-arrow extend selection
35  * <li>Cntrl-left-arrow and cntrl-right-arrow act like home and
36  * end respectively.
37  * </ul>
38  * <p>
39  * <strong>Warning:</strong>
40  * Serialized objects of this class will not be compatible with
41  * future Swing releases. The current serialization support is appropriate
42  * for short term storage or RMI between applications running the same
43  * version of Swing. A future release of Swing will provide support for
44  * long term persistence.
45  *
46  * @author Timothy Prinzing
47  * @version 1.23 12/19/03
48  */

49 public class WindowsTextFieldUI extends BasicTextFieldUI JavaDoc
50 {
51     /**
52      * Creates a UI for a JTextField.
53      *
54      * @param c the text field
55      * @return the UI
56      */

57     public static ComponentUI createUI(JComponent c) {
58         return new WindowsTextFieldUI();
59     }
60
61     /**
62      * Paints a background for the view. This will only be
63      * called if isOpaque() on the associated component is
64      * true. The default is to paint the background color
65      * of the component.
66      *
67      * @param g the graphics context
68      */

69     protected void paintBackground(Graphics g) {
70     super.paintBackground(g);
71     }
72
73     /**
74      * Creates the caret for a field.
75      *
76      * @return the caret
77      */

78     protected Caret createCaret() {
79     return new WindowsFieldCaret();
80     }
81
82     /**
83      * WindowsFieldCaret has different scrolling behavior than
84      * DefaultCaret.
85      */

86     static class WindowsFieldCaret extends DefaultCaret implements UIResource JavaDoc {
87
88     public WindowsFieldCaret() {
89         super();
90     }
91
92     /**
93      * Adjusts the visibility of the caret according to
94      * the windows feel which seems to be to move the
95      * caret out into the field by about a quarter of
96      * a field length if not visible.
97      */

98     protected void adjustVisibility(Rectangle r) {
99             SwingUtilities.invokeLater(new SafeScroller(r));
100     }
101
102     /**
103      * Gets the painter for the Highlighter.
104      *
105      * @return the painter
106      */

107     protected Highlighter.HighlightPainter getSelectionPainter() {
108         return WindowsTextUI.WindowsPainter;
109     }
110
111
112         private class SafeScroller implements Runnable JavaDoc {
113             SafeScroller(Rectangle r) {
114                 this.r = r;
115             }
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
152             private Rectangle r;
153         }
154     }
155
156 }
157
158
Popular Tags