36 import java.awt.im.InputMethodRequests; |
36 import java.awt.im.InputMethodRequests; |
37 import sun.awt.AWTPermissions; |
37 import sun.awt.AWTPermissions; |
38 import sun.awt.InputMethodSupport; |
38 import sun.awt.InputMethodSupport; |
39 |
39 |
40 /** |
40 /** |
41 * The <code>TextComponent</code> class is the superclass of |
41 * The {@code TextComponent} class is the superclass of |
42 * any component that allows the editing of some text. |
42 * any component that allows the editing of some text. |
43 * <p> |
43 * <p> |
44 * A text component embodies a string of text. The |
44 * A text component embodies a string of text. The |
45 * <code>TextComponent</code> class defines a set of methods |
45 * {@code TextComponent} class defines a set of methods |
46 * that determine whether or not this text is editable. If the |
46 * that determine whether or not this text is editable. If the |
47 * component is editable, it defines another set of methods |
47 * component is editable, it defines another set of methods |
48 * that supports a text insertion caret. |
48 * that supports a text insertion caret. |
49 * <p> |
49 * <p> |
50 * In addition, the class defines methods that are used |
50 * In addition, the class defines methods that are used |
59 */ |
59 */ |
60 public class TextComponent extends Component implements Accessible { |
60 public class TextComponent extends Component implements Accessible { |
61 |
61 |
62 /** |
62 /** |
63 * The value of the text. |
63 * The value of the text. |
64 * A <code>null</code> value is the same as "". |
64 * A {@code null} value is the same as "". |
65 * |
65 * |
66 * @serial |
66 * @serial |
67 * @see #setText(String) |
67 * @see #setText(String) |
68 * @see #getText() |
68 * @see #getText() |
69 */ |
69 */ |
70 String text; |
70 String text; |
71 |
71 |
72 /** |
72 /** |
73 * A boolean indicating whether or not this |
73 * A boolean indicating whether or not this |
74 * <code>TextComponent</code> is editable. |
74 * {@code TextComponent} is editable. |
75 * It will be <code>true</code> if the text component |
75 * It will be {@code true} if the text component |
76 * is editable and <code>false</code> if not. |
76 * is editable and {@code false} if not. |
77 * |
77 * |
78 * @serial |
78 * @serial |
79 * @see #isEditable() |
79 * @see #isEditable() |
80 */ |
80 */ |
81 boolean editable = true; |
81 boolean editable = true; |
82 |
82 |
83 /** |
83 /** |
84 * The selection refers to the selected text, and the |
84 * The selection refers to the selected text, and the |
85 * <code>selectionStart</code> is the start position |
85 * {@code selectionStart} is the start position |
86 * of the selected text. |
86 * of the selected text. |
87 * |
87 * |
88 * @serial |
88 * @serial |
89 * @see #getSelectionStart() |
89 * @see #getSelectionStart() |
90 * @see #setSelectionStart(int) |
90 * @see #setSelectionStart(int) |
91 */ |
91 */ |
92 int selectionStart; |
92 int selectionStart; |
93 |
93 |
94 /** |
94 /** |
95 * The selection refers to the selected text, and the |
95 * The selection refers to the selected text, and the |
96 * <code>selectionEnd</code> |
96 * {@code selectionEnd} |
97 * is the end position of the selected text. |
97 * is the end position of the selected text. |
98 * |
98 * |
99 * @serial |
99 * @serial |
100 * @see #getSelectionEnd() |
100 * @see #getSelectionEnd() |
101 * @see #setSelectionEnd(int) |
101 * @see #setSelectionEnd(int) |
118 private static final long serialVersionUID = -2214773872412987419L; |
118 private static final long serialVersionUID = -2214773872412987419L; |
119 |
119 |
120 /** |
120 /** |
121 * Constructs a new text component initialized with the |
121 * Constructs a new text component initialized with the |
122 * specified text. Sets the value of the cursor to |
122 * specified text. Sets the value of the cursor to |
123 * <code>Cursor.TEXT_CURSOR</code>. |
123 * {@code Cursor.TEXT_CURSOR}. |
124 * @param text the text to be displayed; if |
124 * @param text the text to be displayed; if |
125 * <code>text</code> is <code>null</code>, the empty |
125 * {@code text} is {@code null}, the empty |
126 * string <code>""</code> will be displayed |
126 * string {@code ""} will be displayed |
127 * @exception HeadlessException if |
127 * @exception HeadlessException if |
128 * <code>GraphicsEnvironment.isHeadless</code> |
128 * {@code GraphicsEnvironment.isHeadless} |
129 * returns true |
129 * returns true |
130 * @see java.awt.GraphicsEnvironment#isHeadless |
130 * @see java.awt.GraphicsEnvironment#isHeadless |
131 * @see java.awt.Cursor |
131 * @see java.awt.Cursor |
132 */ |
132 */ |
133 TextComponent(String text) throws HeadlessException { |
133 TextComponent(String text) throws HeadlessException { |
201 super.addNotify(); |
201 super.addNotify(); |
202 enableInputMethodsIfNecessary(); |
202 enableInputMethodsIfNecessary(); |
203 } |
203 } |
204 |
204 |
205 /** |
205 /** |
206 * Removes the <code>TextComponent</code>'s peer. |
206 * Removes the {@code TextComponent}'s peer. |
207 * The peer allows us to modify the appearance of the |
207 * The peer allows us to modify the appearance of the |
208 * <code>TextComponent</code> without changing its |
208 * {@code TextComponent} without changing its |
209 * functionality. |
209 * functionality. |
210 */ |
210 */ |
211 public void removeNotify() { |
211 public void removeNotify() { |
212 synchronized (getTreeLock()) { |
212 synchronized (getTreeLock()) { |
213 TextComponentPeer peer = (TextComponentPeer)this.peer; |
213 TextComponentPeer peer = (TextComponentPeer)this.peer; |
222 |
222 |
223 /** |
223 /** |
224 * Sets the text that is presented by this |
224 * Sets the text that is presented by this |
225 * text component to be the specified text. |
225 * text component to be the specified text. |
226 * @param t the new text; |
226 * @param t the new text; |
227 * if this parameter is <code>null</code> then |
227 * if this parameter is {@code null} then |
228 * the text is set to the empty string "" |
228 * the text is set to the empty string "" |
229 * @see java.awt.TextComponent#getText |
229 * @see java.awt.TextComponent#getText |
230 */ |
230 */ |
231 public synchronized void setText(String t) { |
231 public synchronized void setText(String t) { |
232 if (t == null) { |
232 if (t == null) { |
249 |
249 |
250 /** |
250 /** |
251 * Returns the text that is presented by this text component. |
251 * Returns the text that is presented by this text component. |
252 * By default, this is an empty string. |
252 * By default, this is an empty string. |
253 * |
253 * |
254 * @return the value of this <code>TextComponent</code> |
254 * @return the value of this {@code TextComponent} |
255 * @see java.awt.TextComponent#setText |
255 * @see java.awt.TextComponent#setText |
256 */ |
256 */ |
257 public synchronized String getText() { |
257 public synchronized String getText() { |
258 TextComponentPeer peer = (TextComponentPeer)this.peer; |
258 TextComponentPeer peer = (TextComponentPeer)this.peer; |
259 if (peer != null) { |
259 if (peer != null) { |
272 return getText().substring(getSelectionStart(), getSelectionEnd()); |
272 return getText().substring(getSelectionStart(), getSelectionEnd()); |
273 } |
273 } |
274 |
274 |
275 /** |
275 /** |
276 * Indicates whether or not this text component is editable. |
276 * Indicates whether or not this text component is editable. |
277 * @return <code>true</code> if this text component is |
277 * @return {@code true} if this text component is |
278 * editable; <code>false</code> otherwise. |
278 * editable; {@code false} otherwise. |
279 * @see java.awt.TextComponent#setEditable |
279 * @see java.awt.TextComponent#setEditable |
280 * @since 1.0 |
280 * @since 1.0 |
281 */ |
281 */ |
282 public boolean isEditable() { |
282 public boolean isEditable() { |
283 return editable; |
283 return editable; |
285 |
285 |
286 /** |
286 /** |
287 * Sets the flag that determines whether or not this |
287 * Sets the flag that determines whether or not this |
288 * text component is editable. |
288 * text component is editable. |
289 * <p> |
289 * <p> |
290 * If the flag is set to <code>true</code>, this text component |
290 * If the flag is set to {@code true}, this text component |
291 * becomes user editable. If the flag is set to <code>false</code>, |
291 * becomes user editable. If the flag is set to {@code false}, |
292 * the user cannot change the text of this text component. |
292 * the user cannot change the text of this text component. |
293 * By default, non-editable text components have a background color |
293 * By default, non-editable text components have a background color |
294 * of SystemColor.control. This default can be overridden by |
294 * of SystemColor.control. This default can be overridden by |
295 * calling setBackground. |
295 * calling setBackground. |
296 * |
296 * |
365 * Sets the selection start for this text component to |
365 * Sets the selection start for this text component to |
366 * the specified position. The new start point is constrained |
366 * the specified position. The new start point is constrained |
367 * to be at or before the current selection end. It also |
367 * to be at or before the current selection end. It also |
368 * cannot be set to less than zero, the beginning of the |
368 * cannot be set to less than zero, the beginning of the |
369 * component's text. |
369 * component's text. |
370 * If the caller supplies a value for <code>selectionStart</code> |
370 * If the caller supplies a value for {@code selectionStart} |
371 * that is out of bounds, the method enforces these constraints |
371 * that is out of bounds, the method enforces these constraints |
372 * silently, and without failure. |
372 * silently, and without failure. |
373 * @param selectionStart the start position of the |
373 * @param selectionStart the start position of the |
374 * selected text |
374 * selected text |
375 * @see java.awt.TextComponent#getSelectionStart |
375 * @see java.awt.TextComponent#getSelectionStart |
401 /** |
401 /** |
402 * Sets the selection end for this text component to |
402 * Sets the selection end for this text component to |
403 * the specified position. The new end point is constrained |
403 * the specified position. The new end point is constrained |
404 * to be at or after the current selection start. It also |
404 * to be at or after the current selection start. It also |
405 * cannot be set beyond the end of the component's text. |
405 * cannot be set beyond the end of the component's text. |
406 * If the caller supplies a value for <code>selectionEnd</code> |
406 * If the caller supplies a value for {@code selectionEnd} |
407 * that is out of bounds, the method enforces these constraints |
407 * that is out of bounds, the method enforces these constraints |
408 * silently, and without failure. |
408 * silently, and without failure. |
409 * @param selectionEnd the end position of the |
409 * @param selectionEnd the end position of the |
410 * selected text |
410 * selected text |
411 * @see java.awt.TextComponent#getSelectionEnd |
411 * @see java.awt.TextComponent#getSelectionEnd |
427 * must be greater than or equal to zero. The end position must be |
427 * must be greater than or equal to zero. The end position must be |
428 * greater than or equal to the start position, and less than or |
428 * greater than or equal to the start position, and less than or |
429 * equal to the length of the text component's text. The |
429 * equal to the length of the text component's text. The |
430 * character positions are indexed starting with zero. |
430 * character positions are indexed starting with zero. |
431 * The length of the selection is |
431 * The length of the selection is |
432 * <code>endPosition</code> - <code>startPosition</code>, so the |
432 * {@code endPosition} - {@code startPosition}, so the |
433 * character at <code>endPosition</code> is not selected. |
433 * character at {@code endPosition} is not selected. |
434 * If the start and end positions of the selected text are equal, |
434 * If the start and end positions of the selected text are equal, |
435 * all text is deselected. |
435 * all text is deselected. |
436 * <p> |
436 * <p> |
437 * If the caller supplies values that are inconsistent or out of |
437 * If the caller supplies values that are inconsistent or out of |
438 * bounds, the method enforces these constraints silently, and |
438 * bounds, the method enforces these constraints silently, and |
441 * equal the text length. If the start position is less than zero, |
441 * equal the text length. If the start position is less than zero, |
442 * it is reset to zero, and if the end position is less than the |
442 * it is reset to zero, and if the end position is less than the |
443 * start position, it is reset to the start position. |
443 * start position, it is reset to the start position. |
444 * |
444 * |
445 * @param selectionStart the zero-based index of the first |
445 * @param selectionStart the zero-based index of the first |
446 character (<code>char</code> value) to be selected |
446 * character ({@code char} value) to be selected |
447 * @param selectionEnd the zero-based end position of the |
447 * @param selectionEnd the zero-based end position of the |
448 text to be selected; the character (<code>char</code> value) at |
448 * text to be selected; the character ({@code char} value) at |
449 <code>selectionEnd</code> is not selected |
449 * {@code selectionEnd} is not selected |
450 * @see java.awt.TextComponent#setSelectionStart |
450 * @see java.awt.TextComponent#setSelectionStart |
451 * @see java.awt.TextComponent#setSelectionEnd |
451 * @see java.awt.TextComponent#setSelectionEnd |
452 * @see java.awt.TextComponent#selectAll |
452 * @see java.awt.TextComponent#selectAll |
453 */ |
453 */ |
454 public synchronized void select(int selectionStart, int selectionEnd) { |
454 public synchronized void select(int selectionStart, int selectionEnd) { |
493 * Sets the position of the text insertion caret. |
493 * Sets the position of the text insertion caret. |
494 * The caret position is constrained to be between 0 |
494 * The caret position is constrained to be between 0 |
495 * and the last character of the text, inclusive. |
495 * and the last character of the text, inclusive. |
496 * If the passed-in value is greater than this range, |
496 * If the passed-in value is greater than this range, |
497 * the value is set to the last character (or 0 if |
497 * the value is set to the last character (or 0 if |
498 * the <code>TextComponent</code> contains no text) |
498 * the {@code TextComponent} contains no text) |
499 * and no error is returned. If the passed-in value is |
499 * and no error is returned. If the passed-in value is |
500 * less than 0, an <code>IllegalArgumentException</code> |
500 * less than 0, an {@code IllegalArgumentException} |
501 * is thrown. |
501 * is thrown. |
502 * |
502 * |
503 * @param position the position of the text insertion caret |
503 * @param position the position of the text insertion caret |
504 * @exception IllegalArgumentException if <code>position</code> |
504 * @exception IllegalArgumentException if {@code position} |
505 * is less than zero |
505 * is less than zero |
506 * @since 1.1 |
506 * @since 1.1 |
507 */ |
507 */ |
508 public synchronized void setCaretPosition(int position) { |
508 public synchronized void setCaretPosition(int position) { |
509 if (position < 0) { |
509 if (position < 0) { |
551 } |
551 } |
552 |
552 |
553 /** |
553 /** |
554 * Adds the specified text event listener to receive text events |
554 * Adds the specified text event listener to receive text events |
555 * from this text component. |
555 * from this text component. |
556 * If <code>l</code> is <code>null</code>, no exception is |
556 * If {@code l} is {@code null}, no exception is |
557 * thrown and no action is performed. |
557 * thrown and no action is performed. |
558 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" |
558 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" |
559 * >AWT Threading Issues</a> for details on AWT's threading model. |
559 * >AWT Threading Issues</a> for details on AWT's threading model. |
560 * |
560 * |
561 * @param l the text event listener |
561 * @param l the text event listener |
572 } |
572 } |
573 |
573 |
574 /** |
574 /** |
575 * Removes the specified text event listener so that it no longer |
575 * Removes the specified text event listener so that it no longer |
576 * receives text events from this text component |
576 * receives text events from this text component |
577 * If <code>l</code> is <code>null</code>, no exception is |
577 * If {@code l} is {@code null}, no exception is |
578 * thrown and no action is performed. |
578 * thrown and no action is performed. |
579 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" |
579 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" |
580 * >AWT Threading Issues</a> for details on AWT's threading model. |
580 * >AWT Threading Issues</a> for details on AWT's threading model. |
581 * |
581 * |
582 * @param l the text listener |
582 * @param l the text listener |
594 |
594 |
595 /** |
595 /** |
596 * Returns an array of all the text listeners |
596 * Returns an array of all the text listeners |
597 * registered on this text component. |
597 * registered on this text component. |
598 * |
598 * |
599 * @return all of this text component's <code>TextListener</code>s |
599 * @return all of this text component's {@code TextListener}s |
600 * or an empty array if no text |
600 * or an empty array if no text |
601 * listeners are currently registered |
601 * listeners are currently registered |
602 * |
602 * |
603 * |
603 * |
604 * @see #addTextListener |
604 * @see #addTextListener |
610 } |
610 } |
611 |
611 |
612 /** |
612 /** |
613 * Returns an array of all the objects currently registered |
613 * Returns an array of all the objects currently registered |
614 * as <code><em>Foo</em>Listener</code>s |
614 * as <code><em>Foo</em>Listener</code>s |
615 * upon this <code>TextComponent</code>. |
615 * upon this {@code TextComponent}. |
616 * <code><em>Foo</em>Listener</code>s are registered using the |
616 * <code><em>Foo</em>Listener</code>s are registered using the |
617 * <code>add<em>Foo</em>Listener</code> method. |
617 * <code>add<em>Foo</em>Listener</code> method. |
618 * |
618 * |
619 * <p> |
619 * <p> |
620 * You can specify the <code>listenerType</code> argument |
620 * You can specify the {@code listenerType} argument |
621 * with a class literal, such as |
621 * with a class literal, such as |
622 * <code><em>Foo</em>Listener.class</code>. |
622 * <code><em>Foo</em>Listener.class</code>. |
623 * For example, you can query a |
623 * For example, you can query a |
624 * <code>TextComponent</code> <code>t</code> |
624 * {@code TextComponent t} |
625 * for its text listeners with the following code: |
625 * for its text listeners with the following code: |
626 * |
626 * |
627 * <pre>TextListener[] tls = (TextListener[])(t.getListeners(TextListener.class));</pre> |
627 * <pre>TextListener[] tls = (TextListener[])(t.getListeners(TextListener.class));</pre> |
628 * |
628 * |
629 * If no such listeners exist, this method returns an empty array. |
629 * If no such listeners exist, this method returns an empty array. |
630 * |
630 * |
631 * @param listenerType the type of listeners requested; this parameter |
631 * @param listenerType the type of listeners requested; this parameter |
632 * should specify an interface that descends from |
632 * should specify an interface that descends from |
633 * <code>java.util.EventListener</code> |
633 * {@code java.util.EventListener} |
634 * @return an array of all objects registered as |
634 * @return an array of all objects registered as |
635 * <code><em>Foo</em>Listener</code>s on this text component, |
635 * <code><em>Foo</em>Listener</code>s on this text component, |
636 * or an empty array if no such |
636 * or an empty array if no such |
637 * listeners have been added |
637 * listeners have been added |
638 * @exception ClassCastException if <code>listenerType</code> |
638 * @exception ClassCastException if {@code listenerType} |
639 * doesn't specify a class or interface that implements |
639 * doesn't specify a class or interface that implements |
640 * <code>java.util.EventListener</code> |
640 * {@code java.util.EventListener} |
641 * |
641 * |
642 * @see #getTextListeners |
642 * @see #getTextListeners |
643 * @since 1.3 |
643 * @since 1.3 |
644 */ |
644 */ |
645 public <T extends EventListener> T[] getListeners(Class<T> listenerType) { |
645 public <T extends EventListener> T[] getListeners(Class<T> listenerType) { |
664 return super.eventEnabled(e); |
664 return super.eventEnabled(e); |
665 } |
665 } |
666 |
666 |
667 /** |
667 /** |
668 * Processes events on this text component. If the event is a |
668 * Processes events on this text component. If the event is a |
669 * <code>TextEvent</code>, it invokes the <code>processTextEvent</code> |
669 * {@code TextEvent}, it invokes the {@code processTextEvent} |
670 * method else it invokes its superclass's <code>processEvent</code>. |
670 * method else it invokes its superclass's {@code processEvent}. |
671 * <p>Note that if the event parameter is <code>null</code> |
671 * <p>Note that if the event parameter is {@code null} |
672 * the behavior is unspecified and may result in an |
672 * the behavior is unspecified and may result in an |
673 * exception. |
673 * exception. |
674 * |
674 * |
675 * @param e the event |
675 * @param e the event |
676 */ |
676 */ |
682 super.processEvent(e); |
682 super.processEvent(e); |
683 } |
683 } |
684 |
684 |
685 /** |
685 /** |
686 * Processes text events occurring on this text component by |
686 * Processes text events occurring on this text component by |
687 * dispatching them to any registered <code>TextListener</code> objects. |
687 * dispatching them to any registered {@code TextListener} objects. |
688 * <p> |
688 * <p> |
689 * NOTE: This method will not be called unless text events |
689 * NOTE: This method will not be called unless text events |
690 * are enabled for this component. This happens when one of the |
690 * are enabled for this component. This happens when one of the |
691 * following occurs: |
691 * following occurs: |
692 * <ul> |
692 * <ul> |
693 * <li>A <code>TextListener</code> object is registered |
693 * <li>A {@code TextListener} object is registered |
694 * via <code>addTextListener</code> |
694 * via {@code addTextListener} |
695 * <li>Text events are enabled via <code>enableEvents</code> |
695 * <li>Text events are enabled via {@code enableEvents} |
696 * </ul> |
696 * </ul> |
697 * <p>Note that if the event parameter is <code>null</code> |
697 * <p>Note that if the event parameter is {@code null} |
698 * the behavior is unspecified and may result in an |
698 * the behavior is unspecified and may result in an |
699 * exception. |
699 * exception. |
700 * |
700 * |
701 * @param e the text event |
701 * @param e the text event |
702 * @see Component#enableEvents |
702 * @see Component#enableEvents |
713 } |
713 } |
714 } |
714 } |
715 |
715 |
716 /** |
716 /** |
717 * Returns a string representing the state of this |
717 * Returns a string representing the state of this |
718 * <code>TextComponent</code>. This |
718 * {@code TextComponent}. This |
719 * method is intended to be used only for debugging purposes, and the |
719 * method is intended to be used only for debugging purposes, and the |
720 * content and format of the returned string may vary between |
720 * content and format of the returned string may vary between |
721 * implementations. The returned string may be empty but may not be |
721 * implementations. The returned string may be empty but may not be |
722 * <code>null</code>. |
722 * {@code null}. |
723 * |
723 * |
724 * @return the parameter string of this text component |
724 * @return the parameter string of this text component |
725 */ |
725 */ |
726 protected String paramString() { |
726 protected String paramString() { |
727 String str = super.paramString() + ",text=" + getText(); |
727 String str = super.paramString() + ",text=" + getText(); |
793 * add a listener to receive text events fired by the |
793 * add a listener to receive text events fired by the |
794 * TextComponent. Unrecognized keys or values will be |
794 * TextComponent. Unrecognized keys or values will be |
795 * ignored. |
795 * ignored. |
796 * |
796 * |
797 * @exception HeadlessException if |
797 * @exception HeadlessException if |
798 * <code>GraphicsEnvironment.isHeadless()</code> returns |
798 * {@code GraphicsEnvironment.isHeadless()} returns |
799 * <code>true</code> |
799 * {@code true} |
800 * @see #removeTextListener |
800 * @see #removeTextListener |
801 * @see #addTextListener |
801 * @see #addTextListener |
802 * @see java.awt.GraphicsEnvironment#isHeadless |
802 * @see java.awt.GraphicsEnvironment#isHeadless |
803 */ |
803 */ |
804 private void readObject(ObjectInputStream s) |
804 private void readObject(ObjectInputStream s) |