358 * throw an IllegalArgumentException if the assumption of
359 * a StyledDocument, a JEditorPane, or a StyledEditorKit
360 * fail to be true.
361 * <p>
362 * The component that gets acted upon by the action
363 * will be the source of the ActionEvent if the source
364 * can be narrowed to a JEditorPane type. If the source
365 * can't be narrowed, the most recently focused text
366 * component is changed. If neither of these are the
367 * case, the action cannot be performed.
368 * <p>
369 * <strong>Warning:</strong>
370 * Serialized objects of this class will not be compatible with
371 * future Swing releases. The current serialization support is
372 * appropriate for short term storage or RMI between applications running
373 * the same version of Swing. As of 1.4, support for long term storage
374 * of all JavaBeans™
375 * has been added to the <code>java.beans</code> package.
376 * Please see {@link java.beans.XMLEncoder}.
377 */
378 public abstract static class StyledTextAction extends TextAction {
379
380 /**
381 * Creates a new StyledTextAction from a string action name.
382 *
383 * @param nm the name of the action
384 */
385 public StyledTextAction(String nm) {
386 super(nm);
387 }
388
389 /**
390 * Gets the target editor for an action.
391 *
392 * @param e the action event
393 * @return the editor
394 */
395 protected final JEditorPane getEditor(ActionEvent e) {
396 JTextComponent tcomp = getTextComponent(e);
397 if (tcomp instanceof JEditorPane) {
477 doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
478 }
479
480 }
481
482 /**
483 * An action to set the font family in the associated
484 * JEditorPane. This will use the family specified as
485 * the command string on the ActionEvent if there is one,
486 * otherwise the family that was initialized with will be used.
487 * <p>
488 * <strong>Warning:</strong>
489 * Serialized objects of this class will not be compatible with
490 * future Swing releases. The current serialization support is
491 * appropriate for short term storage or RMI between applications running
492 * the same version of Swing. As of 1.4, support for long term storage
493 * of all JavaBeans™
494 * has been added to the <code>java.beans</code> package.
495 * Please see {@link java.beans.XMLEncoder}.
496 */
497 public static class FontFamilyAction extends StyledTextAction {
498
499 /**
500 * Creates a new FontFamilyAction.
501 *
502 * @param nm the action name
503 * @param family the font family
504 */
505 public FontFamilyAction(String nm, String family) {
506 super(nm);
507 this.family = family;
508 }
509
510 /**
511 * Sets the font family.
512 *
513 * @param e the event
514 */
515 public void actionPerformed(ActionEvent e) {
516 JEditorPane editor = getEditor(e);
533 }
534
535 private String family;
536 }
537
538 /**
539 * An action to set the font size in the associated
540 * JEditorPane. This will use the size specified as
541 * the command string on the ActionEvent if there is one,
542 * otherwise the size that was initialized with will be used.
543 * <p>
544 * <strong>Warning:</strong>
545 * Serialized objects of this class will not be compatible with
546 * future Swing releases. The current serialization support is
547 * appropriate for short term storage or RMI between applications running
548 * the same version of Swing. As of 1.4, support for long term storage
549 * of all JavaBeans™
550 * has been added to the <code>java.beans</code> package.
551 * Please see {@link java.beans.XMLEncoder}.
552 */
553 public static class FontSizeAction extends StyledTextAction {
554
555 /**
556 * Creates a new FontSizeAction.
557 *
558 * @param nm the action name
559 * @param size the font size
560 */
561 public FontSizeAction(String nm, int size) {
562 super(nm);
563 this.size = size;
564 }
565
566 /**
567 * Sets the font size.
568 *
569 * @param e the action event
570 */
571 public void actionPerformed(ActionEvent e) {
572 JEditorPane editor = getEditor(e);
600 * <code>StyledDocument.setCharacterAttributes</code>
601 * on the styled document associated with the target
602 * JEditorPane.
603 * <p>
604 * If the target text component is specified as the
605 * source of the ActionEvent and there is a command string,
606 * the command string will be interpreted as the foreground
607 * color. It will be interpreted by called
608 * <code>Color.decode</code>, and should therefore be
609 * legal input for that method.
610 * <p>
611 * <strong>Warning:</strong>
612 * Serialized objects of this class will not be compatible with
613 * future Swing releases. The current serialization support is
614 * appropriate for short term storage or RMI between applications running
615 * the same version of Swing. As of 1.4, support for long term storage
616 * of all JavaBeans™
617 * has been added to the <code>java.beans</code> package.
618 * Please see {@link java.beans.XMLEncoder}.
619 */
620 public static class ForegroundAction extends StyledTextAction {
621
622 /**
623 * Creates a new ForegroundAction.
624 *
625 * @param nm the action name
626 * @param fg the foreground color
627 */
628 public ForegroundAction(String nm, Color fg) {
629 super(nm);
630 this.fg = fg;
631 }
632
633 /**
634 * Sets the foreground color.
635 *
636 * @param e the action event
637 */
638 public void actionPerformed(ActionEvent e) {
639 JEditorPane editor = getEditor(e);
666 * This is done by calling
667 * <code>StyledDocument.setParagraphAttributes</code>
668 * on the styled document associated with the target
669 * JEditorPane.
670 * <p>
671 * If the target text component is specified as the
672 * source of the ActionEvent and there is a command string,
673 * the command string will be interpreted as an integer
674 * that should be one of the legal values for the
675 * <code>StyleConstants.Alignment</code> attribute.
676 * <p>
677 * <strong>Warning:</strong>
678 * Serialized objects of this class will not be compatible with
679 * future Swing releases. The current serialization support is
680 * appropriate for short term storage or RMI between applications running
681 * the same version of Swing. As of 1.4, support for long term storage
682 * of all JavaBeans™
683 * has been added to the <code>java.beans</code> package.
684 * Please see {@link java.beans.XMLEncoder}.
685 */
686 public static class AlignmentAction extends StyledTextAction {
687
688 /**
689 * Creates a new AlignmentAction.
690 *
691 * @param nm the action name
692 * @param a the alignment >= 0
693 */
694 public AlignmentAction(String nm, int a) {
695 super(nm);
696 this.a = a;
697 }
698
699 /**
700 * Sets the alignment.
701 *
702 * @param e the action event
703 */
704 public void actionPerformed(ActionEvent e) {
705 JEditorPane editor = getEditor(e);
716 StyleConstants.setAlignment(attr, a);
717 setParagraphAttributes(editor, attr, false);
718 }
719 }
720
721 private int a;
722 }
723
724 /**
725 * An action to toggle the bold attribute.
726 * <p>
727 * <strong>Warning:</strong>
728 * Serialized objects of this class will not be compatible with
729 * future Swing releases. The current serialization support is
730 * appropriate for short term storage or RMI between applications running
731 * the same version of Swing. As of 1.4, support for long term storage
732 * of all JavaBeans™
733 * has been added to the <code>java.beans</code> package.
734 * Please see {@link java.beans.XMLEncoder}.
735 */
736 public static class BoldAction extends StyledTextAction {
737
738 /**
739 * Constructs a new BoldAction.
740 */
741 public BoldAction() {
742 super("font-bold");
743 }
744
745 /**
746 * Toggles the bold attribute.
747 *
748 * @param e the action event
749 */
750 public void actionPerformed(ActionEvent e) {
751 JEditorPane editor = getEditor(e);
752 if (editor != null) {
753 StyledEditorKit kit = getStyledEditorKit(editor);
754 MutableAttributeSet attr = kit.getInputAttributes();
755 boolean bold = (StyleConstants.isBold(attr)) ? false : true;
756 SimpleAttributeSet sas = new SimpleAttributeSet();
757 StyleConstants.setBold(sas, bold);
758 setCharacterAttributes(editor, sas, false);
759 }
760 }
761 }
762
763 /**
764 * An action to toggle the italic attribute.
765 * <p>
766 * <strong>Warning:</strong>
767 * Serialized objects of this class will not be compatible with
768 * future Swing releases. The current serialization support is
769 * appropriate for short term storage or RMI between applications running
770 * the same version of Swing. As of 1.4, support for long term storage
771 * of all JavaBeans™
772 * has been added to the <code>java.beans</code> package.
773 * Please see {@link java.beans.XMLEncoder}.
774 */
775 public static class ItalicAction extends StyledTextAction {
776
777 /**
778 * Constructs a new ItalicAction.
779 */
780 public ItalicAction() {
781 super("font-italic");
782 }
783
784 /**
785 * Toggles the italic attribute.
786 *
787 * @param e the action event
788 */
789 public void actionPerformed(ActionEvent e) {
790 JEditorPane editor = getEditor(e);
791 if (editor != null) {
792 StyledEditorKit kit = getStyledEditorKit(editor);
793 MutableAttributeSet attr = kit.getInputAttributes();
794 boolean italic = (StyleConstants.isItalic(attr)) ? false : true;
795 SimpleAttributeSet sas = new SimpleAttributeSet();
796 StyleConstants.setItalic(sas, italic);
797 setCharacterAttributes(editor, sas, false);
798 }
799 }
800 }
801
802 /**
803 * An action to toggle the underline attribute.
804 * <p>
805 * <strong>Warning:</strong>
806 * Serialized objects of this class will not be compatible with
807 * future Swing releases. The current serialization support is
808 * appropriate for short term storage or RMI between applications running
809 * the same version of Swing. As of 1.4, support for long term storage
810 * of all JavaBeans™
811 * has been added to the <code>java.beans</code> package.
812 * Please see {@link java.beans.XMLEncoder}.
813 */
814 public static class UnderlineAction extends StyledTextAction {
815
816 /**
817 * Constructs a new UnderlineAction.
818 */
819 public UnderlineAction() {
820 super("font-underline");
821 }
822
823 /**
824 * Toggles the Underline attribute.
825 *
826 * @param e the action event
827 */
828 public void actionPerformed(ActionEvent e) {
829 JEditorPane editor = getEditor(e);
830 if (editor != null) {
831 StyledEditorKit kit = getStyledEditorKit(editor);
832 MutableAttributeSet attr = kit.getInputAttributes();
833 boolean underline = (StyleConstants.isUnderline(attr)) ? false : true;
|
358 * throw an IllegalArgumentException if the assumption of
359 * a StyledDocument, a JEditorPane, or a StyledEditorKit
360 * fail to be true.
361 * <p>
362 * The component that gets acted upon by the action
363 * will be the source of the ActionEvent if the source
364 * can be narrowed to a JEditorPane type. If the source
365 * can't be narrowed, the most recently focused text
366 * component is changed. If neither of these are the
367 * case, the action cannot be performed.
368 * <p>
369 * <strong>Warning:</strong>
370 * Serialized objects of this class will not be compatible with
371 * future Swing releases. The current serialization support is
372 * appropriate for short term storage or RMI between applications running
373 * the same version of Swing. As of 1.4, support for long term storage
374 * of all JavaBeans™
375 * has been added to the <code>java.beans</code> package.
376 * Please see {@link java.beans.XMLEncoder}.
377 */
378 @SuppressWarnings("serial") // Same-version serialization only
379 public abstract static class StyledTextAction extends TextAction {
380
381 /**
382 * Creates a new StyledTextAction from a string action name.
383 *
384 * @param nm the name of the action
385 */
386 public StyledTextAction(String nm) {
387 super(nm);
388 }
389
390 /**
391 * Gets the target editor for an action.
392 *
393 * @param e the action event
394 * @return the editor
395 */
396 protected final JEditorPane getEditor(ActionEvent e) {
397 JTextComponent tcomp = getTextComponent(e);
398 if (tcomp instanceof JEditorPane) {
478 doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
479 }
480
481 }
482
483 /**
484 * An action to set the font family in the associated
485 * JEditorPane. This will use the family specified as
486 * the command string on the ActionEvent if there is one,
487 * otherwise the family that was initialized with will be used.
488 * <p>
489 * <strong>Warning:</strong>
490 * Serialized objects of this class will not be compatible with
491 * future Swing releases. The current serialization support is
492 * appropriate for short term storage or RMI between applications running
493 * the same version of Swing. As of 1.4, support for long term storage
494 * of all JavaBeans™
495 * has been added to the <code>java.beans</code> package.
496 * Please see {@link java.beans.XMLEncoder}.
497 */
498 @SuppressWarnings("serial") // Same-version serialization only
499 public static class FontFamilyAction extends StyledTextAction {
500
501 /**
502 * Creates a new FontFamilyAction.
503 *
504 * @param nm the action name
505 * @param family the font family
506 */
507 public FontFamilyAction(String nm, String family) {
508 super(nm);
509 this.family = family;
510 }
511
512 /**
513 * Sets the font family.
514 *
515 * @param e the event
516 */
517 public void actionPerformed(ActionEvent e) {
518 JEditorPane editor = getEditor(e);
535 }
536
537 private String family;
538 }
539
540 /**
541 * An action to set the font size in the associated
542 * JEditorPane. This will use the size specified as
543 * the command string on the ActionEvent if there is one,
544 * otherwise the size that was initialized with will be used.
545 * <p>
546 * <strong>Warning:</strong>
547 * Serialized objects of this class will not be compatible with
548 * future Swing releases. The current serialization support is
549 * appropriate for short term storage or RMI between applications running
550 * the same version of Swing. As of 1.4, support for long term storage
551 * of all JavaBeans™
552 * has been added to the <code>java.beans</code> package.
553 * Please see {@link java.beans.XMLEncoder}.
554 */
555 @SuppressWarnings("serial") // Same-version serialization only
556 public static class FontSizeAction extends StyledTextAction {
557
558 /**
559 * Creates a new FontSizeAction.
560 *
561 * @param nm the action name
562 * @param size the font size
563 */
564 public FontSizeAction(String nm, int size) {
565 super(nm);
566 this.size = size;
567 }
568
569 /**
570 * Sets the font size.
571 *
572 * @param e the action event
573 */
574 public void actionPerformed(ActionEvent e) {
575 JEditorPane editor = getEditor(e);
603 * <code>StyledDocument.setCharacterAttributes</code>
604 * on the styled document associated with the target
605 * JEditorPane.
606 * <p>
607 * If the target text component is specified as the
608 * source of the ActionEvent and there is a command string,
609 * the command string will be interpreted as the foreground
610 * color. It will be interpreted by called
611 * <code>Color.decode</code>, and should therefore be
612 * legal input for that method.
613 * <p>
614 * <strong>Warning:</strong>
615 * Serialized objects of this class will not be compatible with
616 * future Swing releases. The current serialization support is
617 * appropriate for short term storage or RMI between applications running
618 * the same version of Swing. As of 1.4, support for long term storage
619 * of all JavaBeans™
620 * has been added to the <code>java.beans</code> package.
621 * Please see {@link java.beans.XMLEncoder}.
622 */
623 @SuppressWarnings("serial") // Same-version serialization only
624 public static class ForegroundAction extends StyledTextAction {
625
626 /**
627 * Creates a new ForegroundAction.
628 *
629 * @param nm the action name
630 * @param fg the foreground color
631 */
632 public ForegroundAction(String nm, Color fg) {
633 super(nm);
634 this.fg = fg;
635 }
636
637 /**
638 * Sets the foreground color.
639 *
640 * @param e the action event
641 */
642 public void actionPerformed(ActionEvent e) {
643 JEditorPane editor = getEditor(e);
670 * This is done by calling
671 * <code>StyledDocument.setParagraphAttributes</code>
672 * on the styled document associated with the target
673 * JEditorPane.
674 * <p>
675 * If the target text component is specified as the
676 * source of the ActionEvent and there is a command string,
677 * the command string will be interpreted as an integer
678 * that should be one of the legal values for the
679 * <code>StyleConstants.Alignment</code> attribute.
680 * <p>
681 * <strong>Warning:</strong>
682 * Serialized objects of this class will not be compatible with
683 * future Swing releases. The current serialization support is
684 * appropriate for short term storage or RMI between applications running
685 * the same version of Swing. As of 1.4, support for long term storage
686 * of all JavaBeans™
687 * has been added to the <code>java.beans</code> package.
688 * Please see {@link java.beans.XMLEncoder}.
689 */
690 @SuppressWarnings("serial") // Same-version serialization only
691 public static class AlignmentAction extends StyledTextAction {
692
693 /**
694 * Creates a new AlignmentAction.
695 *
696 * @param nm the action name
697 * @param a the alignment >= 0
698 */
699 public AlignmentAction(String nm, int a) {
700 super(nm);
701 this.a = a;
702 }
703
704 /**
705 * Sets the alignment.
706 *
707 * @param e the action event
708 */
709 public void actionPerformed(ActionEvent e) {
710 JEditorPane editor = getEditor(e);
721 StyleConstants.setAlignment(attr, a);
722 setParagraphAttributes(editor, attr, false);
723 }
724 }
725
726 private int a;
727 }
728
729 /**
730 * An action to toggle the bold attribute.
731 * <p>
732 * <strong>Warning:</strong>
733 * Serialized objects of this class will not be compatible with
734 * future Swing releases. The current serialization support is
735 * appropriate for short term storage or RMI between applications running
736 * the same version of Swing. As of 1.4, support for long term storage
737 * of all JavaBeans™
738 * has been added to the <code>java.beans</code> package.
739 * Please see {@link java.beans.XMLEncoder}.
740 */
741 @SuppressWarnings("serial") // Same-version serialization only
742 public static class BoldAction extends StyledTextAction {
743
744 /**
745 * Constructs a new BoldAction.
746 */
747 public BoldAction() {
748 super("font-bold");
749 }
750
751 /**
752 * Toggles the bold attribute.
753 *
754 * @param e the action event
755 */
756 public void actionPerformed(ActionEvent e) {
757 JEditorPane editor = getEditor(e);
758 if (editor != null) {
759 StyledEditorKit kit = getStyledEditorKit(editor);
760 MutableAttributeSet attr = kit.getInputAttributes();
761 boolean bold = (StyleConstants.isBold(attr)) ? false : true;
762 SimpleAttributeSet sas = new SimpleAttributeSet();
763 StyleConstants.setBold(sas, bold);
764 setCharacterAttributes(editor, sas, false);
765 }
766 }
767 }
768
769 /**
770 * An action to toggle the italic attribute.
771 * <p>
772 * <strong>Warning:</strong>
773 * Serialized objects of this class will not be compatible with
774 * future Swing releases. The current serialization support is
775 * appropriate for short term storage or RMI between applications running
776 * the same version of Swing. As of 1.4, support for long term storage
777 * of all JavaBeans™
778 * has been added to the <code>java.beans</code> package.
779 * Please see {@link java.beans.XMLEncoder}.
780 */
781 @SuppressWarnings("serial") // Same-version serialization only
782 public static class ItalicAction extends StyledTextAction {
783
784 /**
785 * Constructs a new ItalicAction.
786 */
787 public ItalicAction() {
788 super("font-italic");
789 }
790
791 /**
792 * Toggles the italic attribute.
793 *
794 * @param e the action event
795 */
796 public void actionPerformed(ActionEvent e) {
797 JEditorPane editor = getEditor(e);
798 if (editor != null) {
799 StyledEditorKit kit = getStyledEditorKit(editor);
800 MutableAttributeSet attr = kit.getInputAttributes();
801 boolean italic = (StyleConstants.isItalic(attr)) ? false : true;
802 SimpleAttributeSet sas = new SimpleAttributeSet();
803 StyleConstants.setItalic(sas, italic);
804 setCharacterAttributes(editor, sas, false);
805 }
806 }
807 }
808
809 /**
810 * An action to toggle the underline attribute.
811 * <p>
812 * <strong>Warning:</strong>
813 * Serialized objects of this class will not be compatible with
814 * future Swing releases. The current serialization support is
815 * appropriate for short term storage or RMI between applications running
816 * the same version of Swing. As of 1.4, support for long term storage
817 * of all JavaBeans™
818 * has been added to the <code>java.beans</code> package.
819 * Please see {@link java.beans.XMLEncoder}.
820 */
821 @SuppressWarnings("serial") // Same-version serialization only
822 public static class UnderlineAction extends StyledTextAction {
823
824 /**
825 * Constructs a new UnderlineAction.
826 */
827 public UnderlineAction() {
828 super("font-underline");
829 }
830
831 /**
832 * Toggles the Underline attribute.
833 *
834 * @param e the action event
835 */
836 public void actionPerformed(ActionEvent e) {
837 JEditorPane editor = getEditor(e);
838 if (editor != null) {
839 StyledEditorKit kit = getStyledEditorKit(editor);
840 MutableAttributeSet attr = kit.getInputAttributes();
841 boolean underline = (StyleConstants.isUnderline(attr)) ? false : true;
|