1 /*
2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 package javax.swing.plaf.basic;
26
27 import java.awt;
28 import java.awt.event.KeyEvent;
29 import java.awt.event.FocusEvent;
30 import java.awt.event.InputEvent;
31 import java.beans.PropertyChangeEvent;
32 import java.io.Reader;
33 import javax.swing;
34 import javax.swing.border;
35 import javax.swing.event;
36 import javax.swing.text;
37 import javax.swing.plaf;
38 import sun.swing.DefaultLookup;
39
40 /**
41 * Basis of a look and feel for a JTextField.
42 * <p>
43 * <strong>Warning:</strong>
44 * Serialized objects of this class will not be compatible with
45 * future Swing releases. The current serialization support is
46 * appropriate for short term storage or RMI between applications running
47 * the same version of Swing. As of 1.4, support for long term storage
48 * of all JavaBeans<sup><font size="-2">TM</font></sup>
49 * has been added to the <code>java.beans</code> package.
50 * Please see {@link java.beans.XMLEncoder}.
51 *
52 * @author Timothy Prinzing
53 */
54 public class BasicTextFieldUI extends BasicTextUI {
55
56 /**
57 * Creates a UI for a JTextField.
58 *
59 * @param c the text field
60 * @return the UI
61 */
62 public static ComponentUI createUI(JComponent c) {
63 return new BasicTextFieldUI();
64 }
65
66 /**
67 * Creates a new BasicTextFieldUI.
68 */
69 public BasicTextFieldUI() {
70 super();
71 }
72
73 /**
74 * Fetches the name used as a key to lookup properties through the
75 * UIManager. This is used as a prefix to all the standard
76 * text properties.
77 *
78 * @return the name ("TextField")
79 */
80 protected String getPropertyPrefix() {
81 return "TextField";
82 }
83
84 /**
85 * Creates a view (FieldView) based on an element.
86 *
87 * @param elem the element
88 * @return the view
89 */
90 public View create(Element elem) {
91 Document doc = elem.getDocument();
92 Object i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/);
93 if (Boolean.TRUE.equals(i18nFlag)) {
94 // To support bidirectional text, we build a more heavyweight
95 // representation of the field.
96 String kind = elem.getName();
97 if (kind != null) {
98 if (kind.equals(AbstractDocument.ContentElementName)) {
99 return new GlyphView(elem);
100 } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
101 return new I18nFieldView(elem);
102 }
103 }
104 // this shouldn't happen, should probably throw in this case.
105 }
106 return new FieldView(elem);
107 }
108
109 /**
110 * Returns the baseline.
111 *
112 * @throws NullPointerException {@inheritDoc}
113 * @throws IllegalArgumentException {@inheritDoc}
114 * @see javax.swing.JComponent#getBaseline(int, int)
115 * @since 1.6
116 */
117 public int getBaseline(JComponent c, int width, int height) {
118 super.getBaseline(c, width, height);
119 View rootView = getRootView((JTextComponent)c);
120 if (rootView.getViewCount() > 0) {
121 Insets insets = c.getInsets();
122 height = height - insets.top - insets.bottom;
123 if (height > 0) {
124 int baseline = insets.top;
125 View fieldView = rootView.getView(0);
126 int vspan = (int)fieldView.getPreferredSpan(View.Y_AXIS);
127 if (height != vspan) {
128 int slop = height - vspan;
129 baseline += slop / 2;
130 }
131 if (fieldView instanceof I18nFieldView) {
132 int fieldBaseline = BasicHTML.getBaseline(
133 fieldView, width - insets.left - insets.right,
134 height);
135 if (fieldBaseline < 0) {
136 return -1;
137 }
138 baseline += fieldBaseline;
139 }
140 else {
141 FontMetrics fm = c.getFontMetrics(c.getFont());
142 baseline += fm.getAscent();
143 }
144 return baseline;
145 }
146 }
147 return -1;
148 }
149
150 /**
151 * Returns an enum indicating how the baseline of the component
152 * changes as the size changes.
153 *
154 * @throws NullPointerException {@inheritDoc}
155 * @see javax.swing.JComponent#getBaseline(int, int)
156 * @since 1.6
157 */
158 public Component.BaselineResizeBehavior getBaselineResizeBehavior(
159 JComponent c) {
160 super.getBaselineResizeBehavior(c);
161 return Component.BaselineResizeBehavior.CENTER_OFFSET;
162 }
163
164
165 /**
166 * A field view that support bidirectional text via the
167 * support provided by ParagraphView.
168 */
169 static class I18nFieldView extends ParagraphView {
170
171 I18nFieldView(Element elem) {
172 super(elem);
173 }
174
175 /**
176 * Fetch the constraining span to flow against for
177 * the given child index. There is no limit for
178 * a field since it scrolls, so this is implemented to
179 * return <code>Integer.MAX_VALUE</code>.
180 */
181 public int getFlowSpan(int index) {
182 return Integer.MAX_VALUE;
183 }
184
185 protected void setJustification(int j) {
186 // Justification is done in adjustAllocation(), so disable
187 // ParagraphView's justification handling by doing nothing here.
188 }
189
190 static boolean isLeftToRight( java.awt.Component c ) {
191 return c.getComponentOrientation().isLeftToRight();
192 }
193
194 /**
195 * Adjusts the allocation given to the view
196 * to be a suitable allocation for a text field.
197 * If the view has been allocated more than the
198 * preferred span vertically, the allocation is
199 * changed to be centered vertically. Horizontally
200 * the view is adjusted according to the horizontal
201 * alignment property set on the associated JTextField
202 * (if that is the type of the hosting component).
203 *
204 * @param a the allocation given to the view, which may need
205 * to be adjusted.
206 * @return the allocation that the superclass should use.
207 */
208 Shape adjustAllocation(Shape a) {
209 if (a != null) {
210 Rectangle bounds = a.getBounds();
211 int vspan = (int) getPreferredSpan(Y_AXIS);
212 int hspan = (int) getPreferredSpan(X_AXIS);
213 if (bounds.height != vspan) {
214 int slop = bounds.height - vspan;
215 bounds.y += slop / 2;
216 bounds.height -= slop;
217 }
218
219 // horizontal adjustments
220 Component c = getContainer();
221 if (c instanceof JTextField) {
222 JTextField field = (JTextField) c;
223 BoundedRangeModel vis = field.getHorizontalVisibility();
224 int max = Math.max(hspan, bounds.width);
225 int value = vis.getValue();
226 int extent = Math.min(max, bounds.width - 1);
227 if ((value + extent) > max) {
228 value = max - extent;
229 }
230 vis.setRangeProperties(value, extent, vis.getMinimum(),
231 max, false);
232 if (hspan < bounds.width) {
233 // horizontally align the interior
234 int slop = bounds.width - 1 - hspan;
235
236 int align = ((JTextField)c).getHorizontalAlignment();
237 if(isLeftToRight(c)) {
238 if(align==LEADING) {
239 align = LEFT;
240 }
241 else if(align==TRAILING) {
242 align = RIGHT;
243 }
244 }
245 else {
246 if(align==LEADING) {
247 align = RIGHT;
248 }
249 else if(align==TRAILING) {
250 align = LEFT;
251 }
252 }
253
254 switch (align) {
255 case SwingConstants.CENTER:
256 bounds.x += slop / 2;
257 bounds.width -= slop;
258 break;
259 case SwingConstants.RIGHT:
260 bounds.x += slop;
261 bounds.width -= slop;
262 break;
263 }
264 } else {
265 // adjust the allocation to match the bounded range.
266 bounds.width = hspan;
267 bounds.x -= vis.getValue();
268 }
269 }
270 return bounds;
271 }
272 return null;
273 }
274
275 /**
276 * Update the visibility model with the associated JTextField
277 * (if there is one) to reflect the current visibility as a
278 * result of changes to the document model. The bounded
279 * range properties are updated. If the view hasn't yet been
280 * shown the extent will be zero and we just set it to be full
281 * until determined otherwise.
282 */
283 void updateVisibilityModel() {
284 Component c = getContainer();
285 if (c instanceof JTextField) {
286 JTextField field = (JTextField) c;
287 BoundedRangeModel vis = field.getHorizontalVisibility();
288 int hspan = (int) getPreferredSpan(X_AXIS);
289 int extent = vis.getExtent();
290 int maximum = Math.max(hspan, extent);
291 extent = (extent == 0) ? maximum : extent;
292 int value = maximum - extent;
293 int oldValue = vis.getValue();
294 if ((oldValue + extent) > maximum) {
295 oldValue = maximum - extent;
296 }
297 value = Math.max(0, Math.min(value, oldValue));
298 vis.setRangeProperties(value, extent, 0, maximum, false);
299 }
300 }
301
302 // --- View methods -------------------------------------------
303
304 /**
305 * Renders using the given rendering surface and area on that surface.
306 * The view may need to do layout and create child views to enable
307 * itself to render into the given allocation.
308 *
309 * @param g the rendering surface to use
310 * @param a the allocated region to render into
311 *
312 * @see View#paint
313 */
314 public void paint(Graphics g, Shape a) {
315 Rectangle r = (Rectangle) a;
316 g.clipRect(r.x, r.y, r.width, r.height);
317 super.paint(g, adjustAllocation(a));
318 }
319
320 /**
321 * Determines the resizability of the view along the
322 * given axis. A value of 0 or less is not resizable.
323 *
324 * @param axis View.X_AXIS or View.Y_AXIS
325 * @return the weight -> 1 for View.X_AXIS, else 0
326 */
327 public int getResizeWeight(int axis) {
328 if (axis == View.X_AXIS) {
329 return 1;
330 }
331 return 0;
332 }
333
334 /**
335 * Provides a mapping from the document model coordinate space
336 * to the coordinate space of the view mapped to it.
337 *
338 * @param pos the position to convert >= 0
339 * @param a the allocated region to render into
340 * @return the bounding box of the given position
341 * @exception BadLocationException if the given position does not
342 * represent a valid location in the associated document
343 * @see View#modelToView
344 */
345 public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
346 return super.modelToView(pos, adjustAllocation(a), b);
347 }
348
349 /**
350 * Provides a mapping from the document model coordinate space
351 * to the coordinate space of the view mapped to it.
352 *
353 * @param p0 the position to convert >= 0
354 * @param b0 the bias toward the previous character or the
355 * next character represented by p0, in case the
356 * position is a boundary of two views.
357 * @param p1 the position to convert >= 0
358 * @param b1 the bias toward the previous character or the
359 * next character represented by p1, in case the
360 * position is a boundary of two views.
361 * @param a the allocated region to render into
362 * @return the bounding box of the given position is returned
363 * @exception BadLocationException if the given position does
364 * not represent a valid location in the associated document
365 * @exception IllegalArgumentException for an invalid bias argument
366 * @see View#viewToModel
367 */
368 public Shape modelToView(int p0, Position.Bias b0,
369 int p1, Position.Bias b1, Shape a)
370 throws BadLocationException
371 {
372 return super.modelToView(p0, b0, p1, b1, adjustAllocation(a));
373 }
374
375 /**
376 * Provides a mapping from the view coordinate space to the logical
377 * coordinate space of the model.
378 *
379 * @param fx the X coordinate >= 0.0f
380 * @param fy the Y coordinate >= 0.0f
381 * @param a the allocated region to render into
382 * @return the location within the model that best represents the
383 * given point in the view
384 * @see View#viewToModel
385 */
386 public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
387 return super.viewToModel(fx, fy, adjustAllocation(a), bias);
388 }
389
390 /**
391 * Gives notification that something was inserted into the document
392 * in a location that this view is responsible for.
393 *
394 * @param changes the change information from the associated document
395 * @param a the current allocation of the view
396 * @param f the factory to use to rebuild if the view has children
397 * @see View#insertUpdate
398 */
399 public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
400 super.insertUpdate(changes, adjustAllocation(a), f);
401 updateVisibilityModel();
402 }
403
404 /**
405 * Gives notification that something was removed from the document
406 * in a location that this view is responsible for.
407 *
408 * @param changes the change information from the associated document
409 * @param a the current allocation of the view
410 * @param f the factory to use to rebuild if the view has children
411 * @see View#removeUpdate
412 */
413 public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
414 super.removeUpdate(changes, adjustAllocation(a), f);
415 updateVisibilityModel();
416 }
417
418 }
419
420 }