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