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