API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.swing.plaf.basic. BasicTextFieldUI View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403

/*
 * @(#)BasicTextFieldUI.java	1.98 06/04/20
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package javax.swing.plaf.basic;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.event.InputEvent;
import java.beans.PropertyChangeEvent;
import java.io.Reader;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.plaf.*;
import sun.swing.DefaultLookup;

/**
 * Basis of a look and feel for a JTextField.
 * <p>
 * <strong>Warning:</strong>
 * Serialized objects of this class will not be compatible with
 * future Swing releases. The current serialization support is
 * appropriate for short term storage or RMI between applications running
 * the same version of Swing.  As of 1.4, support for long term storage
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
 * has been added to the <code>java.beans</code> package.
 * Please see {@link java.beans.XMLEncoder}.
 *
 * @author  Timothy Prinzing
 * @version 1.98 04/20/06
 */
public class BasicTextFieldUI extends BasicTextUI {

    /**
     * Creates a UI for a JTextField.
     *
     * @param c the text field
     * @return the UI
     */
    public static ComponentUI createUI(JComponent c) {
        return new BasicTextFieldUI();
    }

    /**
     * Creates a new BasicTextFieldUI.
     */
    public BasicTextFieldUI() {
	super();
    }

    /**
     * Fetches the name used as a key to lookup properties through the
     * UIManager.  This is used as a prefix to all the standard
     * text properties.
     *
     * @return the name ("TextField")
     */
    protected String getPropertyPrefix() {
	return "TextField";
    }

    /**
     * Creates a view (FieldView) based on an element.
     *
     * @param elem the element
     * @return the view
     */
    public View create(Element elem) {
	Document doc = elem.getDocument();
	Object i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/);
	if (Boolean.TRUE.equals(i18nFlag)) {
	    // To support bidirectional text, we build a more heavyweight
	    // representation of the field.
	    String kind = elem.getName();
	    if (kind != null) {
		if (kind.equals(AbstractDocument.ContentElementName)) {
		    return new GlyphView(elem);
		} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
		    return new I18nFieldView(elem);
		}
	    }
	    // this shouldn't happen, should probably throw in this case.
	}
	return new FieldView(elem);
    }

    /**
     * Returns the baseline.
     *
     * @throws NullPointerException {@inheritDoc}
     * @throws IllegalArgumentException {@inheritDoc}
     * @see javax.swing.JComponent#getBaseline(int, int)
     * @since 1.6
     */
    public int getBaseline(JComponent c, int width, int height) {
        super.getBaseline(c, width, height);
        View rootView = getRootView((JTextComponent)c);
        if (rootView.getViewCount() > 0) {
            Insets insets = c.getInsets();
            height = height - insets.top - insets.bottom;
            if (height > 0) {
                int baseline = insets.top;
                View fieldView = rootView.getView(0);
                int vspan = (int)fieldView.getPreferredSpan(View.Y_AXIS);
                if (height != vspan) {
                    int slop = height - vspan;
                    baseline += slop / 2;
                }
                if (fieldView instanceof I18nFieldView) {
                    int fieldBaseline = BasicHTML.getBaseline(
                            fieldView, width - insets.left - insets.right,
                            height);
                    if (fieldBaseline < 0) {
                        return -1;
                    }
                    baseline += fieldBaseline;
                }
                else {
                    FontMetrics fm = c.getFontMetrics(c.getFont());
                    baseline += fm.getAscent();
                }
                return baseline;
            }
        }
        return -1;
    }

    /**
     * Returns an enum indicating how the baseline of the component
     * changes as the size changes.
     *
     * @throws NullPointerException {@inheritDoc}
     * @see javax.swing.JComponent#getBaseline(int, int)
     * @since 1.6
     */
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
            JComponent c) {
        super.getBaselineResizeBehavior(c);
        return Component.BaselineResizeBehavior.CENTER_OFFSET;
    }


    /**
     * A field view that support bidirectional text via the
     * support provided by ParagraphView.
     */
    static class I18nFieldView extends ParagraphView {

	I18nFieldView(Element elem) {
	    super(elem);
	}

	/**
	 * Fetch the constraining span to flow against for
	 * the given child index.  There is no limit for
	 * a field since it scrolls, so this is implemented to
	 * return <code>Integer.MAX_VALUE</code>.
	 */
        public int getFlowSpan(int index) {
	    return Integer.MAX_VALUE;
	}

	protected void setJustification(int j) {
	    // Justification is done in adjustAllocation(), so disable 
	    // ParagraphView's justification handling by doing nothing here.
	}

	static boolean isLeftToRight( java.awt.Component c ) {
	    return c.getComponentOrientation().isLeftToRight();
	}

	/**
	 * Adjusts the allocation given to the view
	 * to be a suitable allocation for a text field.
	 * If the view has been allocated more than the 
	 * preferred span vertically, the allocation is
	 * changed to be centered vertically.  Horizontally
	 * the view is adjusted according to the horizontal
	 * alignment property set on the associated JTextField
	 * (if that is the type of the hosting component).
	 *
	 * @param a the allocation given to the view, which may need
	 *  to be adjusted.
	 * @return the allocation that the superclass should use.
	 */
        Shape adjustAllocation(Shape a) {
	    if (a != null) {
		Rectangle bounds = a.getBounds();
		int vspan = (int) getPreferredSpan(Y_AXIS);
		int hspan = (int) getPreferredSpan(X_AXIS);
		if (bounds.height != vspan) {
		    int slop = bounds.height - vspan;
		    bounds.y += slop / 2;
		    bounds.height -= slop;
		}
		
		// horizontal adjustments
		Component c = getContainer();
		if (c instanceof JTextField) {
		    JTextField field = (JTextField) c;
		    BoundedRangeModel vis = field.getHorizontalVisibility();
		    int max = Math.max(hspan, bounds.width);
		    int value = vis.getValue();
		    int extent = Math.min(max, bounds.width - 1);
		    if ((value + extent) > max) {
			value = max - extent;
		    }
		    vis.setRangeProperties(value, extent, vis.getMinimum(),
					   max, false);
		    if (hspan < bounds.width) {
			// horizontally align the interior
			int slop = bounds.width - 1 - hspan;
			
			int align = ((JTextField)c).getHorizontalAlignment();
			if(isLeftToRight(c)) {
			    if(align==LEADING) {
				align = LEFT;
			    }
			    else if(align==TRAILING) {
				align = RIGHT;
			    }
			}
			else {
			    if(align==LEADING) {
				align = RIGHT;
			    }
			    else if(align==TRAILING) {
				align = LEFT;
			    }
			}

			switch (align) {
			case SwingConstants.CENTER:
			    bounds.x += slop / 2;
			    bounds.width -= slop;
			    break;
			case SwingConstants.RIGHT:
			    bounds.x += slop;
			    bounds.width -= slop;
			    break;
			}
		    } else {
			// adjust the allocation to match the bounded range.
			bounds.width = hspan;
			bounds.x -= vis.getValue();
		    }
		}
		return bounds;
	    }
	    return null;
	}

	/**
	 * Update the visibility model with the associated JTextField
	 * (if there is one) to reflect the current visibility as a
	 * result of changes to the document model.  The bounded
	 * range properties are updated.  If the view hasn't yet been
	 * shown the extent will be zero and we just set it to be full
	 * until determined otherwise.
	 */
	void updateVisibilityModel() {
	    Component c = getContainer();
	    if (c instanceof JTextField) {
		JTextField field = (JTextField) c;
		BoundedRangeModel vis = field.getHorizontalVisibility();
		int hspan = (int) getPreferredSpan(X_AXIS);
		int extent = vis.getExtent();
		int maximum = Math.max(hspan, extent);
		extent = (extent == 0) ? maximum : extent;
		int value = maximum - extent;
		int oldValue = vis.getValue();
		if ((oldValue + extent) > maximum) {
		    oldValue = maximum - extent;
		}
		value = Math.max(0, Math.min(value, oldValue));
		vis.setRangeProperties(value, extent, 0, maximum, false);
	    }
	}

	// --- View methods -------------------------------------------
	
	/**
	 * Renders using the given rendering surface and area on that surface.
	 * The view may need to do layout and create child views to enable
	 * itself to render into the given allocation.
	 *
	 * @param g the rendering surface to use
	 * @param a the allocated region to render into
	 *
	 * @see View#paint
	 */
        public void paint(Graphics g, Shape a) {
	    Rectangle r = (Rectangle) a;
	    g.clipRect(r.x, r.y, r.width, r.height);
	    super.paint(g, adjustAllocation(a));
	}
	
	/**
	 * Determines the resizability of the view along the
	 * given axis.  A value of 0 or less is not resizable.
	 *
	 * @param axis View.X_AXIS or View.Y_AXIS
	 * @return the weight -> 1 for View.X_AXIS, else 0
	 */
        public int getResizeWeight(int axis) {
	    if (axis == View.X_AXIS) {
		return 1;
	    }
	    return 0;
	}
	
	/**
	 * Provides a mapping from the document model coordinate space
	 * to the coordinate space of the view mapped to it.
	 *
	 * @param pos the position to convert >= 0
	 * @param a the allocated region to render into
	 * @return the bounding box of the given position
	 * @exception BadLocationException  if the given position does not
	 *   represent a valid location in the associated document
	 * @see View#modelToView
	 */
        public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
	    return super.modelToView(pos, adjustAllocation(a), b);
	}
	
        /**
         * Provides a mapping from the document model coordinate space
         * to the coordinate space of the view mapped to it.
         *
         * @param p0 the position to convert >= 0
         * @param b0 the bias toward the previous character or the
         *  next character represented by p0, in case the 
         *  position is a boundary of two views. 
         * @param p1 the position to convert >= 0
         * @param b1 the bias toward the previous character or the
         *  next character represented by p1, in case the 
         *  position is a boundary of two views. 
         * @param a the allocated region to render into
         * @return the bounding box of the given position is returned
         * @exception BadLocationException  if the given position does
         *   not represent a valid location in the associated document
         * @exception IllegalArgumentException for an invalid bias argument
         * @see View#viewToModel
         */
        public Shape modelToView(int p0, Position.Bias b0, 
                                 int p1, Position.Bias b1, Shape a) 
            throws BadLocationException 
        {
            return super.modelToView(p0, b0, p1, b1, adjustAllocation(a));
        }

	/**
	 * Provides a mapping from the view coordinate space to the logical
	 * coordinate space of the model.
	 *
	 * @param fx the X coordinate >= 0.0f
	 * @param fy the Y coordinate >= 0.0f
	 * @param a the allocated region to render into
	 * @return the location within the model that best represents the
	 *  given point in the view
	 * @see View#viewToModel
	 */
        public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
	    return super.viewToModel(fx, fy, adjustAllocation(a), bias);
	}
	
	/**
	 * Gives notification that something was inserted into the document
	 * in a location that this view is responsible for.
	 *
	 * @param changes the change information from the associated document
	 * @param a the current allocation of the view
	 * @param f the factory to use to rebuild if the view has children
	 * @see View#insertUpdate
	 */
        public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
	    super.insertUpdate(changes, adjustAllocation(a), f);
	    updateVisibilityModel();
	}
	
	/**
	 * Gives notification that something was removed from the document
	 * in a location that this view is responsible for.
	 *
	 * @param changes the change information from the associated document
	 * @param a the current allocation of the view
	 * @param f the factory to use to rebuild if the view has children
	 * @see View#removeUpdate
	 */
        public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
	    super.removeUpdate(changes, adjustAllocation(a), f);
	    updateVisibilityModel();
	}
	
    }

}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar