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 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.
24 */
25 package javax.swing;
26
27 import java.awt.*;
28 import java.awt.event.*;
29 import java.awt.im.InputContext;
30 import java.io.*;
31 import java.text.*;
32 import java.util.*;
33 import javax.swing.UIManager;
34 import javax.swing.event.*;
35 import javax.swing.plaf.UIResource;
36 import javax.swing.text.*;
37
38 /**
39 * <code>JFormattedTextField</code> extends <code>JTextField</code> adding
40 * support for formatting arbitrary values, as well as retrieving a particular
41 * object once the user has edited the text. The following illustrates
42 * configuring a <code>JFormattedTextField</code> to edit dates:
43 * <pre>
44 * JFormattedTextField ftf = new JFormattedTextField();
45 * ftf.setValue(new Date());
46 * </pre>
47 * <p>
48 * Once a <code>JFormattedTextField</code> has been created, you can
49 * listen for editing changes by way of adding
50 * a <code>PropertyChangeListener</code> and listening for
51 * <code>PropertyChangeEvent</code>s with the property name <code>value</code>.
52 * <p>
53 * <code>JFormattedTextField</code> allows
160 * <code>JFormattedTextField</code> you should not install your own. If you do,
161 * you are likely to see odd behavior in that the editing policy of the
162 * <code>AbstractFormatter</code> will not be enforced.
163 * <p>
164 * <strong>Warning:</strong> Swing is not thread safe. For more
165 * information see <a
166 * href="package-summary.html#threading">Swing's Threading
167 * Policy</a>.
168 * <p>
169 * <strong>Warning:</strong>
170 * Serialized objects of this class will not be compatible with
171 * future Swing releases. The current serialization support is
172 * appropriate for short term storage or RMI between applications running
173 * the same version of Swing. As of 1.4, support for long term storage
174 * of all JavaBeans™
175 * has been added to the <code>java.beans</code> package.
176 * Please see {@link java.beans.XMLEncoder}.
177 *
178 * @since 1.4
179 */
180 @SuppressWarnings("serial") // Same-version serialization only
181 public class JFormattedTextField extends JTextField {
182 private static final String uiClassID = "FormattedTextFieldUI";
183 private static final Action[] defaultActions =
184 { new CommitAction(), new CancelAction() };
185
186 /**
187 * Constant identifying that when focus is lost,
188 * <code>commitEdit</code> should be invoked. If in committing the
189 * new value a <code>ParseException</code> is thrown, the invalid
190 * value will remain.
191 *
192 * @see #setFocusLostBehavior
193 */
194 public static final int COMMIT = 0;
195
196 /**
197 * Constant identifying that when focus is lost,
198 * <code>commitEdit</code> should be invoked. If in committing the new
199 * value a <code>ParseException</code> is thrown, the value will be
341 }
342
343 /**
344 * Sets the behavior when focus is lost. This will be one of
345 * <code>JFormattedTextField.COMMIT_OR_REVERT</code>,
346 * <code>JFormattedTextField.REVERT</code>,
347 * <code>JFormattedTextField.COMMIT</code> or
348 * <code>JFormattedTextField.PERSIST</code>
349 * Note that some <code>AbstractFormatter</code>s may push changes as
350 * they occur, so that the value of this will have no effect.
351 * <p>
352 * This will throw an <code>IllegalArgumentException</code> if the object
353 * passed in is not one of the afore mentioned values.
354 * <p>
355 * The default value of this property is
356 * <code>JFormattedTextField.COMMIT_OR_REVERT</code>.
357 *
358 * @param behavior Identifies behavior when focus is lost
359 * @throws IllegalArgumentException if behavior is not one of the known
360 * values
361 * @beaninfo
362 * enum: COMMIT JFormattedTextField.COMMIT
363 * COMMIT_OR_REVERT JFormattedTextField.COMMIT_OR_REVERT
364 * REVERT JFormattedTextField.REVERT
365 * PERSIST JFormattedTextField.PERSIST
366 * description: Behavior when component loses focus
367 */
368 public void setFocusLostBehavior(int behavior) {
369 if (behavior != COMMIT && behavior != COMMIT_OR_REVERT &&
370 behavior != PERSIST && behavior != REVERT) {
371 throw new IllegalArgumentException("setFocusLostBehavior must be one of: JFormattedTextField.COMMIT, JFormattedTextField.COMMIT_OR_REVERT, JFormattedTextField.PERSIST or JFormattedTextField.REVERT");
372 }
373 focusLostBehavior = behavior;
374 }
375
376 /**
377 * Returns the behavior when focus is lost. This will be one of
378 * <code>COMMIT_OR_REVERT</code>,
379 * <code>COMMIT</code>,
380 * <code>REVERT</code> or
381 * <code>PERSIST</code>
382 * Note that some <code>AbstractFormatter</code>s may push changes as
383 * they occur, so that the value of this will have no effect.
384 *
385 * @return returns behavior when focus is lost
386 */
387 public int getFocusLostBehavior() {
391 /**
392 * Sets the <code>AbstractFormatterFactory</code>.
393 * <code>AbstractFormatterFactory</code> is
394 * able to return an instance of <code>AbstractFormatter</code> that is
395 * used to format a value for display, as well an enforcing an editing
396 * policy.
397 * <p>
398 * If you have not explicitly set an <code>AbstractFormatterFactory</code>
399 * by way of this method (or a constructor) an
400 * <code>AbstractFormatterFactory</code> and consequently an
401 * <code>AbstractFormatter</code> will be used based on the
402 * <code>Class</code> of the value. <code>NumberFormatter</code> will
403 * be used for <code>Number</code>s, <code>DateFormatter</code> will
404 * be used for <code>Dates</code>, otherwise <code>DefaultFormatter</code>
405 * will be used.
406 * <p>
407 * This is a JavaBeans bound property.
408 *
409 * @param tf <code>AbstractFormatterFactory</code> used to lookup
410 * instances of <code>AbstractFormatter</code>
411 * @beaninfo
412 * bound: true
413 * attribute: visualUpdate true
414 * description: AbstractFormatterFactory, responsible for returning an
415 * AbstractFormatter that can format the current value.
416 */
417 public void setFormatterFactory(AbstractFormatterFactory tf) {
418 AbstractFormatterFactory oldFactory = factory;
419
420 factory = tf;
421 firePropertyChange("formatterFactory", oldFactory, tf);
422 setValue(getValue(), true, false);
423 }
424
425 /**
426 * Returns the current <code>AbstractFormatterFactory</code>.
427 *
428 * @see #setFormatterFactory
429 * @return <code>AbstractFormatterFactory</code> used to determine
430 * <code>AbstractFormatter</code>s
431 */
432 public AbstractFormatterFactory getFormatterFactory() {
433 return factory;
434 }
435
436 /**
437 * Sets the current <code>AbstractFormatter</code>.
438 * <p>
439 * You should not normally invoke this, instead set the
440 * <code>AbstractFormatterFactory</code> or set the value.
441 * <code>JFormattedTextField</code> will
442 * invoke this as the state of the <code>JFormattedTextField</code>
443 * changes and requires the value to be reset.
444 * <code>JFormattedTextField</code> passes in the
445 * <code>AbstractFormatter</code> obtained from the
446 * <code>AbstractFormatterFactory</code>.
447 * <p>
448 * This is a JavaBeans bound property.
449 *
450 * @see #setFormatterFactory
451 * @param format AbstractFormatter to use for formatting
452 * @beaninfo
453 * bound: true
454 * attribute: visualUpdate true
455 * description: TextFormatter, responsible for formatting the current value
456 */
457 protected void setFormatter(AbstractFormatter format) {
458 AbstractFormatter oldFormat = this.format;
459
460 if (oldFormat != null) {
461 oldFormat.uninstall();
462 }
463 setEditValid(true);
464 this.format = format;
465 if (format != null) {
466 format.install(this);
467 }
468 setEdited(false);
469 firePropertyChange("textFormatter", oldFormat, format);
470 }
471
472 /**
473 * Returns the <code>AbstractFormatter</code> that is used to format and
474 * parse the current value.
475 *
476 * @return AbstractFormatter used for formatting
477 */
478 public AbstractFormatter getFormatter() {
479 return format;
480 }
481
482 /**
483 * Sets the value that will be formatted by an
484 * <code>AbstractFormatter</code> obtained from the current
485 * <code>AbstractFormatterFactory</code>. If no
486 * <code>AbstractFormatterFactory</code> has been specified, this will
487 * attempt to create one based on the type of <code>value</code>.
488 * <p>
489 * The default value of this property is null.
490 * <p>
491 * This is a JavaBeans bound property.
492 *
493 * @param value Current value to display
494 * @beaninfo
495 * bound: true
496 * attribute: visualUpdate true
497 * description: The value to be formatted.
498 */
499 public void setValue(Object value) {
500 if (value != null && getFormatterFactory() == null) {
501 setFormatterFactory(getDefaultFormatterFactory(value));
502 }
503 setValue(value, true, true);
504 }
505
506 /**
507 * Returns the last valid value. Based on the editing policy of
508 * the <code>AbstractFormatter</code> this may not return the current
509 * value. The currently edited value can be obtained by invoking
510 * <code>commitEdit</code> followed by <code>getValue</code>.
511 *
512 * @return Last valid value
513 */
514 public Object getValue() {
515 return value;
516 }
517
518 /**
528 AbstractFormatter format = getFormatter();
529
530 if (format != null) {
531 setValue(format.stringToValue(getText()), false, true);
532 }
533 }
534
535 /**
536 * Sets the validity of the edit on the receiver. You should not normally
537 * invoke this. This will be invoked by the
538 * <code>AbstractFormatter</code> as the user edits the value.
539 * <p>
540 * Not all formatters will allow the component to get into an invalid
541 * state, and thus this may never be invoked.
542 * <p>
543 * Based on the look and feel this may visually change the state of
544 * the receiver.
545 *
546 * @param isValid boolean indicating if the currently edited value is
547 * valid.
548 * @beaninfo
549 * bound: true
550 * attribute: visualUpdate true
551 * description: True indicates the edited value is valid
552 */
553 private void setEditValid(boolean isValid) {
554 if (isValid != editValid) {
555 editValid = isValid;
556 firePropertyChange("editValid", Boolean.valueOf(!isValid),
557 Boolean.valueOf(isValid));
558 }
559 }
560
561 /**
562 * Returns true if the current value being edited is valid. The value of
563 * this is managed by the current <code>AbstractFormatter</code>, as such
564 * there is no public setter for it.
565 *
566 * @return true if the current value being edited is valid.
567 */
568 public boolean isEditValid() {
569 return editValid;
570 }
571
572 /**
573 * Invoked when the user inputs an invalid value. This gives the
574 * component a chance to provide feedback. The default
575 * implementation beeps.
576 */
577 protected void invalidEdit() {
578 UIManager.getLookAndFeel().provideErrorFeedback(JFormattedTextField.this);
579 }
580
581 /**
582 * Processes any input method events, such as
583 * <code>InputMethodEvent.INPUT_METHOD_TEXT_CHANGED</code> or
584 * <code>InputMethodEvent.CARET_POSITION_CHANGED</code>.
585 *
586 * @param e the <code>InputMethodEvent</code>
587 * @see InputMethodEvent
657 JFormattedTextField.this.getValue(), true, true);
658 }
659 }
660 }
661 else if (fb == JFormattedTextField.REVERT) {
662 JFormattedTextField.this.setValue(
663 JFormattedTextField.this.getValue(), true, true);
664 }
665 }
666 }
667
668 /**
669 * Fetches the command list for the editor. This is
670 * the list of commands supported by the plugged-in UI
671 * augmented by the collection of commands that the
672 * editor itself supports. These are useful for binding
673 * to events, such as in a keymap.
674 *
675 * @return the command list
676 */
677 public Action[] getActions() {
678 return TextAction.augmentList(super.getActions(), defaultActions);
679 }
680
681 /**
682 * Gets the class ID for a UI.
683 *
684 * @return the string "FormattedTextFieldUI"
685 * @see JComponent#getUIClassID
686 */
687 public String getUIClassID() {
688 return uiClassID;
689 }
690
691 /**
692 * Associates the editor with a text document.
693 * The currently registered factory is used to build a view for
694 * the document, which gets displayed by the editor after revalidation.
695 * A PropertyChange event ("document") is propagated to each listener.
696 *
697 * @param doc the document to display/edit
698 * @see #getDocument
699 * @beaninfo
700 * description: the text document model
701 * bound: true
702 * expert: true
703 */
704 public void setDocument(Document doc) {
705 if (documentListener != null && getDocument() != null) {
706 getDocument().removeDocumentListener(documentListener);
707 }
708 super.setDocument(doc);
709 if (documentListener == null) {
710 documentListener = new DocumentHandler();
711 }
712 doc.addDocumentListener(documentListener);
713 }
714
715 /*
716 * See readObject and writeObject in JComponent for more
717 * information about serialization in Swing.
718 *
719 * @param s Stream to write to
720 */
721 private void writeObject(ObjectOutputStream s) throws IOException {
722 s.defaultWriteObject();
723 if (getUIClassID().equals(uiClassID)) {
|
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 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.
24 */
25 package javax.swing;
26
27 import java.awt.*;
28 import java.awt.event.*;
29 import java.awt.im.InputContext;
30 import java.beans.JavaBean;
31 import java.beans.BeanProperty;
32 import java.io.*;
33 import java.text.*;
34 import java.util.*;
35 import javax.swing.event.*;
36 import javax.swing.plaf.UIResource;
37 import javax.swing.text.*;
38
39 /**
40 * <code>JFormattedTextField</code> extends <code>JTextField</code> adding
41 * support for formatting arbitrary values, as well as retrieving a particular
42 * object once the user has edited the text. The following illustrates
43 * configuring a <code>JFormattedTextField</code> to edit dates:
44 * <pre>
45 * JFormattedTextField ftf = new JFormattedTextField();
46 * ftf.setValue(new Date());
47 * </pre>
48 * <p>
49 * Once a <code>JFormattedTextField</code> has been created, you can
50 * listen for editing changes by way of adding
51 * a <code>PropertyChangeListener</code> and listening for
52 * <code>PropertyChangeEvent</code>s with the property name <code>value</code>.
53 * <p>
54 * <code>JFormattedTextField</code> allows
161 * <code>JFormattedTextField</code> you should not install your own. If you do,
162 * you are likely to see odd behavior in that the editing policy of the
163 * <code>AbstractFormatter</code> will not be enforced.
164 * <p>
165 * <strong>Warning:</strong> Swing is not thread safe. For more
166 * information see <a
167 * href="package-summary.html#threading">Swing's Threading
168 * Policy</a>.
169 * <p>
170 * <strong>Warning:</strong>
171 * Serialized objects of this class will not be compatible with
172 * future Swing releases. The current serialization support is
173 * appropriate for short term storage or RMI between applications running
174 * the same version of Swing. As of 1.4, support for long term storage
175 * of all JavaBeans™
176 * has been added to the <code>java.beans</code> package.
177 * Please see {@link java.beans.XMLEncoder}.
178 *
179 * @since 1.4
180 */
181 @JavaBean
182 @SuppressWarnings("serial") // Same-version serialization only
183 public class JFormattedTextField extends JTextField {
184 private static final String uiClassID = "FormattedTextFieldUI";
185 private static final Action[] defaultActions =
186 { new CommitAction(), new CancelAction() };
187
188 /**
189 * Constant identifying that when focus is lost,
190 * <code>commitEdit</code> should be invoked. If in committing the
191 * new value a <code>ParseException</code> is thrown, the invalid
192 * value will remain.
193 *
194 * @see #setFocusLostBehavior
195 */
196 public static final int COMMIT = 0;
197
198 /**
199 * Constant identifying that when focus is lost,
200 * <code>commitEdit</code> should be invoked. If in committing the new
201 * value a <code>ParseException</code> is thrown, the value will be
343 }
344
345 /**
346 * Sets the behavior when focus is lost. This will be one of
347 * <code>JFormattedTextField.COMMIT_OR_REVERT</code>,
348 * <code>JFormattedTextField.REVERT</code>,
349 * <code>JFormattedTextField.COMMIT</code> or
350 * <code>JFormattedTextField.PERSIST</code>
351 * Note that some <code>AbstractFormatter</code>s may push changes as
352 * they occur, so that the value of this will have no effect.
353 * <p>
354 * This will throw an <code>IllegalArgumentException</code> if the object
355 * passed in is not one of the afore mentioned values.
356 * <p>
357 * The default value of this property is
358 * <code>JFormattedTextField.COMMIT_OR_REVERT</code>.
359 *
360 * @param behavior Identifies behavior when focus is lost
361 * @throws IllegalArgumentException if behavior is not one of the known
362 * values
363 */
364 @BeanProperty(bound = false, enumerationValues = {
365 "JFormattedTextField.COMMIT",
366 "JFormattedTextField.COMMIT_OR_REVERT",
367 "JFormattedTextField.REVERT",
368 "JFormattedTextField.PERSIST"}, description
369 = "Behavior when component loses focus")
370 public void setFocusLostBehavior(int behavior) {
371 if (behavior != COMMIT && behavior != COMMIT_OR_REVERT &&
372 behavior != PERSIST && behavior != REVERT) {
373 throw new IllegalArgumentException("setFocusLostBehavior must be one of: JFormattedTextField.COMMIT, JFormattedTextField.COMMIT_OR_REVERT, JFormattedTextField.PERSIST or JFormattedTextField.REVERT");
374 }
375 focusLostBehavior = behavior;
376 }
377
378 /**
379 * Returns the behavior when focus is lost. This will be one of
380 * <code>COMMIT_OR_REVERT</code>,
381 * <code>COMMIT</code>,
382 * <code>REVERT</code> or
383 * <code>PERSIST</code>
384 * Note that some <code>AbstractFormatter</code>s may push changes as
385 * they occur, so that the value of this will have no effect.
386 *
387 * @return returns behavior when focus is lost
388 */
389 public int getFocusLostBehavior() {
393 /**
394 * Sets the <code>AbstractFormatterFactory</code>.
395 * <code>AbstractFormatterFactory</code> is
396 * able to return an instance of <code>AbstractFormatter</code> that is
397 * used to format a value for display, as well an enforcing an editing
398 * policy.
399 * <p>
400 * If you have not explicitly set an <code>AbstractFormatterFactory</code>
401 * by way of this method (or a constructor) an
402 * <code>AbstractFormatterFactory</code> and consequently an
403 * <code>AbstractFormatter</code> will be used based on the
404 * <code>Class</code> of the value. <code>NumberFormatter</code> will
405 * be used for <code>Number</code>s, <code>DateFormatter</code> will
406 * be used for <code>Dates</code>, otherwise <code>DefaultFormatter</code>
407 * will be used.
408 * <p>
409 * This is a JavaBeans bound property.
410 *
411 * @param tf <code>AbstractFormatterFactory</code> used to lookup
412 * instances of <code>AbstractFormatter</code>
413 */
414 @BeanProperty(visualUpdate = true, description
415 = "AbstractFormatterFactory, responsible for returning an AbstractFormatter that can format the current value.")
416 public void setFormatterFactory(AbstractFormatterFactory tf) {
417 AbstractFormatterFactory oldFactory = factory;
418
419 factory = tf;
420 firePropertyChange("formatterFactory", oldFactory, tf);
421 setValue(getValue(), true, false);
422 }
423
424 /**
425 * Returns the current <code>AbstractFormatterFactory</code>.
426 *
427 * @see #setFormatterFactory
428 * @return <code>AbstractFormatterFactory</code> used to determine
429 * <code>AbstractFormatter</code>s
430 */
431 public AbstractFormatterFactory getFormatterFactory() {
432 return factory;
433 }
434
435 /**
436 * Sets the current <code>AbstractFormatter</code>.
437 * <p>
438 * You should not normally invoke this, instead set the
439 * <code>AbstractFormatterFactory</code> or set the value.
440 * <code>JFormattedTextField</code> will
441 * invoke this as the state of the <code>JFormattedTextField</code>
442 * changes and requires the value to be reset.
443 * <code>JFormattedTextField</code> passes in the
444 * <code>AbstractFormatter</code> obtained from the
445 * <code>AbstractFormatterFactory</code>.
446 * <p>
447 * This is a JavaBeans bound property.
448 *
449 * @see #setFormatterFactory
450 * @param format AbstractFormatter to use for formatting
451 */
452 protected void setFormatter(AbstractFormatter format) {
453 AbstractFormatter oldFormat = this.format;
454
455 if (oldFormat != null) {
456 oldFormat.uninstall();
457 }
458 setEditValid(true);
459 this.format = format;
460 if (format != null) {
461 format.install(this);
462 }
463 setEdited(false);
464 firePropertyChange("textFormatter", oldFormat, format);
465 }
466
467 /**
468 * Returns the <code>AbstractFormatter</code> that is used to format and
469 * parse the current value.
470 *
471 * @return AbstractFormatter used for formatting
472 */
473 @BeanProperty(visualUpdate = true, description
474 = "TextFormatter, responsible for formatting the current value")
475 public AbstractFormatter getFormatter() {
476 return format;
477 }
478
479 /**
480 * Sets the value that will be formatted by an
481 * <code>AbstractFormatter</code> obtained from the current
482 * <code>AbstractFormatterFactory</code>. If no
483 * <code>AbstractFormatterFactory</code> has been specified, this will
484 * attempt to create one based on the type of <code>value</code>.
485 * <p>
486 * The default value of this property is null.
487 * <p>
488 * This is a JavaBeans bound property.
489 *
490 * @param value Current value to display
491 */
492 @BeanProperty(visualUpdate = true, description
493 = "The value to be formatted.")
494 public void setValue(Object value) {
495 if (value != null && getFormatterFactory() == null) {
496 setFormatterFactory(getDefaultFormatterFactory(value));
497 }
498 setValue(value, true, true);
499 }
500
501 /**
502 * Returns the last valid value. Based on the editing policy of
503 * the <code>AbstractFormatter</code> this may not return the current
504 * value. The currently edited value can be obtained by invoking
505 * <code>commitEdit</code> followed by <code>getValue</code>.
506 *
507 * @return Last valid value
508 */
509 public Object getValue() {
510 return value;
511 }
512
513 /**
523 AbstractFormatter format = getFormatter();
524
525 if (format != null) {
526 setValue(format.stringToValue(getText()), false, true);
527 }
528 }
529
530 /**
531 * Sets the validity of the edit on the receiver. You should not normally
532 * invoke this. This will be invoked by the
533 * <code>AbstractFormatter</code> as the user edits the value.
534 * <p>
535 * Not all formatters will allow the component to get into an invalid
536 * state, and thus this may never be invoked.
537 * <p>
538 * Based on the look and feel this may visually change the state of
539 * the receiver.
540 *
541 * @param isValid boolean indicating if the currently edited value is
542 * valid.
543 */
544 @BeanProperty(visualUpdate = true, description
545 = "True indicates the edited value is valid")
546 private void setEditValid(boolean isValid) {
547 if (isValid != editValid) {
548 editValid = isValid;
549 firePropertyChange("editValid", Boolean.valueOf(!isValid),
550 Boolean.valueOf(isValid));
551 }
552 }
553
554 /**
555 * Returns true if the current value being edited is valid. The value of
556 * this is managed by the current <code>AbstractFormatter</code>, as such
557 * there is no public setter for it.
558 *
559 * @return true if the current value being edited is valid.
560 */
561 @BeanProperty(bound = false)
562 public boolean isEditValid() {
563 return editValid;
564 }
565
566 /**
567 * Invoked when the user inputs an invalid value. This gives the
568 * component a chance to provide feedback. The default
569 * implementation beeps.
570 */
571 protected void invalidEdit() {
572 UIManager.getLookAndFeel().provideErrorFeedback(JFormattedTextField.this);
573 }
574
575 /**
576 * Processes any input method events, such as
577 * <code>InputMethodEvent.INPUT_METHOD_TEXT_CHANGED</code> or
578 * <code>InputMethodEvent.CARET_POSITION_CHANGED</code>.
579 *
580 * @param e the <code>InputMethodEvent</code>
581 * @see InputMethodEvent
651 JFormattedTextField.this.getValue(), true, true);
652 }
653 }
654 }
655 else if (fb == JFormattedTextField.REVERT) {
656 JFormattedTextField.this.setValue(
657 JFormattedTextField.this.getValue(), true, true);
658 }
659 }
660 }
661
662 /**
663 * Fetches the command list for the editor. This is
664 * the list of commands supported by the plugged-in UI
665 * augmented by the collection of commands that the
666 * editor itself supports. These are useful for binding
667 * to events, such as in a keymap.
668 *
669 * @return the command list
670 */
671 @BeanProperty(bound = false)
672 public Action[] getActions() {
673 return TextAction.augmentList(super.getActions(), defaultActions);
674 }
675
676 /**
677 * Gets the class ID for a UI.
678 *
679 * @return the string "FormattedTextFieldUI"
680 * @see JComponent#getUIClassID
681 */
682 @BeanProperty(bound = false)
683 public String getUIClassID() {
684 return uiClassID;
685 }
686
687 /**
688 * Associates the editor with a text document.
689 * The currently registered factory is used to build a view for
690 * the document, which gets displayed by the editor after revalidation.
691 * A PropertyChange event ("document") is propagated to each listener.
692 *
693 * @param doc the document to display/edit
694 * @see #getDocument
695 */
696 @BeanProperty(expert = true, description
697 = "the text document model")
698 public void setDocument(Document doc) {
699 if (documentListener != null && getDocument() != null) {
700 getDocument().removeDocumentListener(documentListener);
701 }
702 super.setDocument(doc);
703 if (documentListener == null) {
704 documentListener = new DocumentHandler();
705 }
706 doc.addDocumentListener(documentListener);
707 }
708
709 /*
710 * See readObject and writeObject in JComponent for more
711 * information about serialization in Swing.
712 *
713 * @param s Stream to write to
714 */
715 private void writeObject(ObjectOutputStream s) throws IOException {
716 s.defaultWriteObject();
717 if (getUIClassID().equals(uiClassID)) {
|