Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/javax/swing/JPasswordField.java
+++ new/src/share/classes/javax/swing/JPasswordField.java
1 1 /*
2 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25 package javax.swing;
26 26
27 27 import javax.swing.text.*;
28 28 import javax.swing.plaf.*;
29 29 import javax.accessibility.*;
30 30
31 31 import java.io.ObjectOutputStream;
↓ open down ↓ |
31 lines elided |
↑ open up ↑ |
32 32 import java.io.ObjectInputStream;
33 33 import java.io.IOException;
34 34 import java.io.*;
35 35 import java.util.Arrays;
36 36
37 37 /**
38 38 * <code>JPasswordField</code> is a lightweight component that allows
39 39 * the editing of a single line of text where the view indicates
40 40 * something was typed, but does not show the original characters.
41 41 * You can find further information and examples in
42 - * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html">How to Use Text Fields</a>,
42 + * <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html">How to Use Text Fields</a>,
43 43 * a section in <em>The Java Tutorial.</em>
44 44 * <p>
45 45 * <code>JPasswordField</code> is intended
46 46 * to be source-compatible with <code>java.awt.TextField</code>
47 47 * used with <code>echoChar</code> set. It is provided separately
48 48 * to make it easier to safely change the UI for the
49 49 * <code>JTextField</code> without affecting password entries.
50 50 * <p>
51 51 * <strong>NOTE:</strong>
52 52 * By default, JPasswordField disables input methods; otherwise, input
53 53 * characters could be visible while they were composed using input methods.
54 54 * If an application needs the input methods support, please use the
55 55 * inherited method, <code>enableInputMethods(true)</code>.
56 56 * <p>
57 57 * <strong>Warning:</strong> Swing is not thread safe. For more
58 58 * information see <a
59 59 * href="package-summary.html#threading">Swing's Threading
60 60 * Policy</a>.
61 61 * <p>
62 62 * <strong>Warning:</strong>
63 63 * Serialized objects of this class will not be compatible with
64 64 * future Swing releases. The current serialization support is
65 65 * appropriate for short term storage or RMI between applications running
66 66 * the same version of Swing. As of 1.4, support for long term storage
67 67 * of all JavaBeans™
68 68 * has been added to the <code>java.beans</code> package.
69 69 * Please see {@link java.beans.XMLEncoder}.
70 70 *
71 71 * @beaninfo
72 72 * attribute: isContainer false
73 73 * description: Allows the editing of a line of text but doesn't show the characters.
74 74 *
75 75 * @author Timothy Prinzing
76 76 */
77 77 public class JPasswordField extends JTextField {
78 78
79 79 /**
80 80 * Constructs a new <code>JPasswordField</code>,
81 81 * with a default document, <code>null</code> starting
82 82 * text string, and 0 column width.
83 83 */
84 84 public JPasswordField() {
85 85 this(null,null,0);
86 86 }
87 87
88 88 /**
89 89 * Constructs a new <code>JPasswordField</code> initialized
90 90 * with the specified text. The document model is set to the
91 91 * default, and the number of columns to 0.
92 92 *
93 93 * @param text the text to be displayed, <code>null</code> if none
94 94 */
95 95 public JPasswordField(String text) {
96 96 this(null, text, 0);
97 97 }
98 98
99 99 /**
100 100 * Constructs a new empty <code>JPasswordField</code> with the specified
101 101 * number of columns. A default model is created, and the initial string
102 102 * is set to <code>null</code>.
103 103 *
104 104 * @param columns the number of columns >= 0
105 105 */
106 106 public JPasswordField(int columns) {
107 107 this(null, null, columns);
108 108 }
109 109
110 110 /**
111 111 * Constructs a new <code>JPasswordField</code> initialized with
112 112 * the specified text and columns. The document model is set to
113 113 * the default.
114 114 *
115 115 * @param text the text to be displayed, <code>null</code> if none
116 116 * @param columns the number of columns >= 0
117 117 */
118 118 public JPasswordField(String text, int columns) {
119 119 this(null, text, columns);
120 120 }
121 121
122 122 /**
123 123 * Constructs a new <code>JPasswordField</code> that uses the
124 124 * given text storage model and the given number of columns.
125 125 * This is the constructor through which the other constructors feed.
126 126 * The echo character is set to '*', but may be changed by the current
127 127 * Look and Feel. If the document model is
128 128 * <code>null</code>, a default one will be created.
129 129 *
130 130 * @param doc the text storage to use
131 131 * @param txt the text to be displayed, <code>null</code> if none
132 132 * @param columns the number of columns to use to calculate
133 133 * the preferred width >= 0; if columns is set to zero, the
134 134 * preferred width will be whatever naturally results from
135 135 * the component implementation
136 136 */
137 137 public JPasswordField(Document doc, String txt, int columns) {
138 138 super(doc, txt, columns);
139 139 // We could either leave this on, which wouldn't be secure,
140 140 // or obscure the composted text, which essentially makes displaying
141 141 // it useless. Therefore, we turn off input methods.
142 142 enableInputMethods(false);
143 143 }
144 144
145 145 /**
146 146 * Returns the name of the L&F class that renders this component.
147 147 *
148 148 * @return the string "PasswordFieldUI"
149 149 * @see JComponent#getUIClassID
150 150 * @see UIDefaults#getUI
151 151 */
152 152 public String getUIClassID() {
153 153 return uiClassID;
154 154 }
155 155
156 156
157 157 /**
158 158 * {@inheritDoc}
159 159 * @since 1.6
160 160 */
161 161 public void updateUI() {
162 162 if(!echoCharSet) {
163 163 echoChar = '*';
164 164 }
165 165 super.updateUI();
166 166 }
167 167
168 168 /**
169 169 * Returns the character to be used for echoing. The default is '*'.
170 170 * The default may be different depending on the currently running Look
171 171 * and Feel. For example, Metal/Ocean's default is a bullet character.
172 172 *
173 173 * @return the echo character, 0 if unset
174 174 * @see #setEchoChar
175 175 * @see #echoCharIsSet
176 176 */
177 177 public char getEchoChar() {
178 178 return echoChar;
179 179 }
180 180
181 181 /**
182 182 * Sets the echo character for this <code>JPasswordField</code>.
183 183 * Note that this is largely a suggestion, since the
184 184 * view that gets installed can use whatever graphic techniques
185 185 * it desires to represent the field. Setting a value of 0 indicates
186 186 * that you wish to see the text as it is typed, similar to
187 187 * the behavior of a standard <code>JTextField</code>.
188 188 *
189 189 * @param c the echo character to display
190 190 * @see #echoCharIsSet
191 191 * @see #getEchoChar
192 192 * @beaninfo
193 193 * description: character to display in place of the real characters
194 194 * attribute: visualUpdate true
195 195 */
196 196 public void setEchoChar(char c) {
197 197 echoChar = c;
198 198 echoCharSet = true;
199 199 repaint();
200 200 revalidate();
201 201 }
202 202
203 203 /**
204 204 * Returns true if this <code>JPasswordField</code> has a character
205 205 * set for echoing. A character is considered to be set if the echo
206 206 * character is not 0.
207 207 *
208 208 * @return true if a character is set for echoing
209 209 * @see #setEchoChar
210 210 * @see #getEchoChar
211 211 */
212 212 public boolean echoCharIsSet() {
213 213 return echoChar != 0;
214 214 }
215 215
216 216 // --- JTextComponent methods ----------------------------------
217 217
218 218 /**
219 219 * Invokes <code>provideErrorFeedback</code> on the current
220 220 * look and feel, which typically initiates an error beep.
221 221 * The normal behavior of transferring the
222 222 * currently selected range in the associated text model
223 223 * to the system clipboard, and removing the contents from
224 224 * the model, is not acceptable for a password field.
225 225 */
226 226 public void cut() {
227 227 if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) {
228 228 UIManager.getLookAndFeel().provideErrorFeedback(this);
229 229 } else {
230 230 super.cut();
231 231 }
232 232 }
233 233
234 234 /**
235 235 * Invokes <code>provideErrorFeedback</code> on the current
236 236 * look and feel, which typically initiates an error beep.
237 237 * The normal behavior of transferring the
238 238 * currently selected range in the associated text model
239 239 * to the system clipboard, and leaving the contents from
240 240 * the model, is not acceptable for a password field.
241 241 */
242 242 public void copy() {
243 243 if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) {
244 244 UIManager.getLookAndFeel().provideErrorFeedback(this);
245 245 } else {
246 246 super.copy();
247 247 }
248 248 }
249 249
250 250 /**
251 251 * Returns the text contained in this <code>TextComponent</code>.
252 252 * If the underlying document is <code>null</code>, will give a
253 253 * <code>NullPointerException</code>.
254 254 * <p>
255 255 * For security reasons, this method is deprecated. Use the
256 256 <code>* getPassword</code> method instead.
257 257 * @deprecated As of Java 2 platform v1.2,
258 258 * replaced by <code>getPassword</code>.
259 259 * @return the text
260 260 */
261 261 @Deprecated
262 262 public String getText() {
263 263 return super.getText();
264 264 }
265 265
266 266 /**
267 267 * Fetches a portion of the text represented by the
268 268 * component. Returns an empty string if length is 0.
269 269 * <p>
270 270 * For security reasons, this method is deprecated. Use the
271 271 * <code>getPassword</code> method instead.
272 272 * @deprecated As of Java 2 platform v1.2,
273 273 * replaced by <code>getPassword</code>.
274 274 * @param offs the offset >= 0
275 275 * @param len the length >= 0
276 276 * @return the text
277 277 * @exception BadLocationException if the offset or length are invalid
278 278 */
279 279 @Deprecated
280 280 public String getText(int offs, int len) throws BadLocationException {
281 281 return super.getText(offs, len);
282 282 }
283 283
284 284 /**
285 285 * Returns the text contained in this <code>TextComponent</code>.
286 286 * If the underlying document is <code>null</code>, will give a
287 287 * <code>NullPointerException</code>. For stronger
288 288 * security, it is recommended that the returned character array be
289 289 * cleared after use by setting each character to zero.
290 290 *
291 291 * @return the text
292 292 */
293 293 public char[] getPassword() {
294 294 Document doc = getDocument();
295 295 Segment txt = new Segment();
296 296 try {
297 297 doc.getText(0, doc.getLength(), txt); // use the non-String API
298 298 } catch (BadLocationException e) {
299 299 return null;
300 300 }
301 301 char[] retValue = new char[txt.count];
302 302 System.arraycopy(txt.array, txt.offset, retValue, 0, txt.count);
303 303 return retValue;
304 304 }
305 305
306 306 /**
307 307 * See readObject() and writeObject() in JComponent for more
308 308 * information about serialization in Swing.
309 309 */
310 310 private void writeObject(ObjectOutputStream s) throws IOException {
311 311 s.defaultWriteObject();
312 312 if (getUIClassID().equals(uiClassID)) {
313 313 byte count = JComponent.getWriteObjCounter(this);
314 314 JComponent.setWriteObjCounter(this, --count);
315 315 if (count == 0 && ui != null) {
316 316 ui.installUI(this);
317 317 }
318 318 }
319 319 }
320 320
321 321 // --- variables -----------------------------------------------
322 322
323 323 /**
324 324 * @see #getUIClassID
325 325 * @see #readObject
326 326 */
327 327 private static final String uiClassID = "PasswordFieldUI";
328 328
329 329 private char echoChar;
330 330
331 331 private boolean echoCharSet = false;
332 332
333 333
334 334 /**
335 335 * Returns a string representation of this <code>JPasswordField</code>.
336 336 * This method is intended to be used only for debugging purposes, and the
337 337 * content and format of the returned string may vary between
338 338 * implementations. The returned string may be empty but may not
339 339 * be <code>null</code>.
340 340 *
341 341 * @return a string representation of this <code>JPasswordField</code>
342 342 */
343 343 protected String paramString() {
344 344 return super.paramString() +
345 345 ",echoChar=" + echoChar;
346 346 }
347 347
348 348
349 349 /**
350 350 * This method is a hack to get around the fact that we cannot
351 351 * directly override setUIProperty because part of the inheritance hierarchy
352 352 * goes outside of the javax.swing package, and therefore calling a package
353 353 * private method isn't allowed. This method should return true if the property
354 354 * was handled, and false otherwise.
355 355 */
356 356 boolean customSetUIProperty(String propertyName, Object value) {
357 357 if (propertyName == "echoChar") {
358 358 if (!echoCharSet) {
359 359 setEchoChar((Character)value);
360 360 echoCharSet = false;
361 361 }
362 362 return true;
363 363 }
364 364 return false;
365 365 }
366 366
367 367 /////////////////
368 368 // Accessibility support
369 369 ////////////////
370 370
371 371
372 372 /**
373 373 * Returns the <code>AccessibleContext</code> associated with this
374 374 * <code>JPasswordField</code>. For password fields, the
375 375 * <code>AccessibleContext</code> takes the form of an
376 376 * <code>AccessibleJPasswordField</code>.
377 377 * A new <code>AccessibleJPasswordField</code> instance is created
378 378 * if necessary.
379 379 *
380 380 * @return an <code>AccessibleJPasswordField</code> that serves as the
381 381 * <code>AccessibleContext</code> of this
382 382 * <code>JPasswordField</code>
383 383 */
384 384 public AccessibleContext getAccessibleContext() {
385 385 if (accessibleContext == null) {
386 386 accessibleContext = new AccessibleJPasswordField();
387 387 }
388 388 return accessibleContext;
389 389 }
390 390
391 391 /**
392 392 * This class implements accessibility support for the
393 393 * <code>JPasswordField</code> class. It provides an implementation of the
394 394 * Java Accessibility API appropriate to password field user-interface
395 395 * elements.
396 396 * <p>
397 397 * <strong>Warning:</strong>
398 398 * Serialized objects of this class will not be compatible with
399 399 * future Swing releases. The current serialization support is
400 400 * appropriate for short term storage or RMI between applications running
401 401 * the same version of Swing. As of 1.4, support for long term storage
402 402 * of all JavaBeans™
403 403 * has been added to the <code>java.beans</code> package.
404 404 * Please see {@link java.beans.XMLEncoder}.
405 405 */
406 406 protected class AccessibleJPasswordField extends AccessibleJTextField {
407 407
408 408 /**
409 409 * Gets the role of this object.
410 410 *
411 411 * @return an instance of AccessibleRole describing the role of the
412 412 * object (AccessibleRole.PASSWORD_TEXT)
413 413 * @see AccessibleRole
414 414 */
415 415 public AccessibleRole getAccessibleRole() {
416 416 return AccessibleRole.PASSWORD_TEXT;
417 417 }
418 418
419 419 /**
420 420 * Gets the <code>AccessibleText</code> for the <code>JPasswordField</code>.
421 421 * The returned object also implements the
422 422 * <code>AccessibleExtendedText</code> interface.
423 423 *
424 424 * @return <code>AccessibleText</code> for the JPasswordField
425 425 * @see javax.accessibility.AccessibleContext
426 426 * @see javax.accessibility.AccessibleContext#getAccessibleText
427 427 * @see javax.accessibility.AccessibleText
428 428 * @see javax.accessibility.AccessibleExtendedText
429 429 *
430 430 * @since 1.6
431 431 */
432 432 public AccessibleText getAccessibleText() {
433 433 return this;
434 434 }
435 435
436 436 /*
437 437 * Returns a String filled with password echo characters. The String
438 438 * contains one echo character for each character (including whitespace)
439 439 * that the user entered in the JPasswordField.
440 440 */
441 441 private String getEchoString(String str) {
442 442 if (str == null) {
443 443 return null;
444 444 }
445 445 char[] buffer = new char[str.length()];
446 446 Arrays.fill(buffer, getEchoChar());
447 447 return new String(buffer);
448 448 }
449 449
450 450 /**
451 451 * Returns the <code>String</code> at a given <code>index</code>.
452 452 *
453 453 * @param part the <code>CHARACTER</code>, <code>WORD</code> or
454 454 * <code>SENTENCE</code> to retrieve
455 455 * @param index an index within the text
456 456 * @return a <code>String</code> if <code>part</code> and
457 457 * <code>index</code> are valid.
458 458 * Otherwise, <code>null</code> is returned
459 459 *
460 460 * @see javax.accessibility.AccessibleText#CHARACTER
461 461 * @see javax.accessibility.AccessibleText#WORD
462 462 * @see javax.accessibility.AccessibleText#SENTENCE
463 463 *
464 464 * @since 1.6
465 465 */
466 466 public String getAtIndex(int part, int index) {
467 467 String str = null;
468 468 if (part == AccessibleText.CHARACTER) {
469 469 str = super.getAtIndex(part, index);
470 470 } else {
471 471 // Treat the text displayed in the JPasswordField
472 472 // as one word and sentence.
473 473 char password[] = getPassword();
474 474 if (password == null ||
475 475 index < 0 || index >= password.length) {
476 476 return null;
477 477 }
478 478 str = new String(password);
479 479 }
480 480 return getEchoString(str);
481 481 }
482 482
483 483 /**
484 484 * Returns the <code>String</code> after a given <code>index</code>.
485 485 *
486 486 * @param part the <code>CHARACTER</code>, <code>WORD</code> or
487 487 * <code>SENTENCE</code> to retrieve
488 488 * @param index an index within the text
489 489 * @return a <code>String</code> if <code>part</code> and
490 490 * <code>index</code> are valid.
491 491 * Otherwise, <code>null</code> is returned
492 492 *
493 493 * @see javax.accessibility.AccessibleText#CHARACTER
494 494 * @see javax.accessibility.AccessibleText#WORD
495 495 * @see javax.accessibility.AccessibleText#SENTENCE
496 496 *
497 497 * @since 1.6
498 498 */
499 499 public String getAfterIndex(int part, int index) {
500 500 if (part == AccessibleText.CHARACTER) {
501 501 String str = super.getAfterIndex(part, index);
502 502 return getEchoString(str);
503 503 } else {
504 504 // There is no word or sentence after the text
505 505 // displayed in the JPasswordField.
506 506 return null;
507 507 }
508 508 }
509 509
510 510 /**
511 511 * Returns the <code>String</code> before a given <code>index</code>.
512 512 *
513 513 * @param part the <code>CHARACTER</code>, <code>WORD</code> or
514 514 * <code>SENTENCE</code> to retrieve
515 515 * @param index an index within the text
516 516 * @return a <code>String</code> if <code>part</code> and
517 517 * <code>index</code> are valid.
518 518 * Otherwise, <code>null</code> is returned
519 519 *
520 520 * @see javax.accessibility.AccessibleText#CHARACTER
521 521 * @see javax.accessibility.AccessibleText#WORD
522 522 * @see javax.accessibility.AccessibleText#SENTENCE
523 523 *
524 524 * @since 1.6
525 525 */
526 526 public String getBeforeIndex(int part, int index) {
527 527 if (part == AccessibleText.CHARACTER) {
528 528 String str = super.getBeforeIndex(part, index);
529 529 return getEchoString(str);
530 530 } else {
531 531 // There is no word or sentence before the text
532 532 // displayed in the JPasswordField.
533 533 return null;
534 534 }
535 535 }
536 536
537 537 /**
538 538 * Returns the text between two <code>indices</code>.
539 539 *
540 540 * @param startIndex the start index in the text
541 541 * @param endIndex the end index in the text
542 542 * @return the text string if the indices are valid.
543 543 * Otherwise, <code>null</code> is returned
544 544 *
545 545 * @since 1.6
546 546 */
547 547 public String getTextRange(int startIndex, int endIndex) {
548 548 String str = super.getTextRange(startIndex, endIndex);
549 549 return getEchoString(str);
550 550 }
551 551
552 552
553 553 /**
554 554 * Returns the <code>AccessibleTextSequence</code> at a given
555 555 * <code>index</code>.
556 556 *
557 557 * @param part the <code>CHARACTER</code>, <code>WORD</code>,
558 558 * <code>SENTENCE</code>, <code>LINE</code> or <code>ATTRIBUTE_RUN</code> to
559 559 * retrieve
560 560 * @param index an index within the text
561 561 * @return an <code>AccessibleTextSequence</code> specifying the text if
562 562 * <code>part</code> and <code>index</code> are valid. Otherwise,
563 563 * <code>null</code> is returned
564 564 *
565 565 * @see javax.accessibility.AccessibleText#CHARACTER
566 566 * @see javax.accessibility.AccessibleText#WORD
567 567 * @see javax.accessibility.AccessibleText#SENTENCE
568 568 * @see javax.accessibility.AccessibleExtendedText#LINE
569 569 * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
570 570 *
571 571 * @since 1.6
572 572 */
573 573 public AccessibleTextSequence getTextSequenceAt(int part, int index) {
574 574 if (part == AccessibleText.CHARACTER) {
575 575 AccessibleTextSequence seq = super.getTextSequenceAt(part, index);
576 576 if (seq == null) {
577 577 return null;
578 578 }
579 579 return new AccessibleTextSequence(seq.startIndex, seq.endIndex,
580 580 getEchoString(seq.text));
581 581 } else {
582 582 // Treat the text displayed in the JPasswordField
583 583 // as one word, sentence, line and attribute run
584 584 char password[] = getPassword();
585 585 if (password == null ||
586 586 index < 0 || index >= password.length) {
587 587 return null;
588 588 }
589 589 String text = new String(password);
590 590 return new AccessibleTextSequence(0, password.length - 1,
591 591 getEchoString(text));
592 592 }
593 593 }
594 594
595 595 /**
596 596 * Returns the <code>AccessibleTextSequence</code> after a given
597 597 * <code>index</code>.
598 598 *
599 599 * @param part the <code>CHARACTER</code>, <code>WORD</code>,
600 600 * <code>SENTENCE</code>, <code>LINE</code> or <code>ATTRIBUTE_RUN</code> to
601 601 * retrieve
602 602 * @param index an index within the text
603 603 * @return an <code>AccessibleTextSequence</code> specifying the text if
604 604 * <code>part</code> and <code>index</code> are valid. Otherwise,
605 605 * <code>null</code> is returned
606 606 *
607 607 * @see javax.accessibility.AccessibleText#CHARACTER
608 608 * @see javax.accessibility.AccessibleText#WORD
609 609 * @see javax.accessibility.AccessibleText#SENTENCE
610 610 * @see javax.accessibility.AccessibleExtendedText#LINE
611 611 * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
612 612 *
613 613 * @since 1.6
614 614 */
615 615 public AccessibleTextSequence getTextSequenceAfter(int part, int index) {
616 616 if (part == AccessibleText.CHARACTER) {
617 617 AccessibleTextSequence seq = super.getTextSequenceAfter(part, index);
618 618 if (seq == null) {
619 619 return null;
620 620 }
621 621 return new AccessibleTextSequence(seq.startIndex, seq.endIndex,
622 622 getEchoString(seq.text));
623 623 } else {
624 624 // There is no word, sentence, line or attribute run
625 625 // after the text displayed in the JPasswordField.
626 626 return null;
627 627 }
628 628 }
629 629
630 630 /**
631 631 * Returns the <code>AccessibleTextSequence</code> before a given
632 632 * <code>index</code>.
633 633 *
634 634 * @param part the <code>CHARACTER</code>, <code>WORD</code>,
635 635 * <code>SENTENCE</code>, <code>LINE</code> or <code>ATTRIBUTE_RUN</code> to
636 636 * retrieve
637 637 * @param index an index within the text
638 638 * @return an <code>AccessibleTextSequence</code> specifying the text if
639 639 * <code>part</code> and <code>index</code> are valid. Otherwise,
640 640 * <code>null</code> is returned
641 641 *
642 642 * @see javax.accessibility.AccessibleText#CHARACTER
643 643 * @see javax.accessibility.AccessibleText#WORD
644 644 * @see javax.accessibility.AccessibleText#SENTENCE
645 645 * @see javax.accessibility.AccessibleExtendedText#LINE
646 646 * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
647 647 *
648 648 * @since 1.6
649 649 */
650 650 public AccessibleTextSequence getTextSequenceBefore(int part, int index) {
651 651 if (part == AccessibleText.CHARACTER) {
652 652 AccessibleTextSequence seq = super.getTextSequenceBefore(part, index);
653 653 if (seq == null) {
654 654 return null;
655 655 }
656 656 return new AccessibleTextSequence(seq.startIndex, seq.endIndex,
657 657 getEchoString(seq.text));
658 658 } else {
659 659 // There is no word, sentence, line or attribute run
660 660 // before the text displayed in the JPasswordField.
661 661 return null;
662 662 }
663 663 }
664 664 }
665 665 }
↓ open down ↓ |
613 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX