169 }
170
171 /**
172 * Creates the AttributeSet used for the selection.
173 */
174 @SuppressWarnings("serial") // anonymous class
175 private void createInputAttributes() {
176 inputAttributes = new SimpleAttributeSet() {
177 public AttributeSet getResolveParent() {
178 return (currentParagraph != null) ?
179 currentParagraph.getAttributes() : null;
180 }
181
182 public Object clone() {
183 return new SimpleAttributeSet(this);
184 }
185 };
186 }
187
188 /**
189 * Creates a new <code>AttributeTracker</code>.
190 */
191 private void createInputAttributeUpdated() {
192 inputAttributeUpdater = new AttributeTracker();
193 }
194
195
196 private static final ViewFactory defaultFactory = new StyledViewFactory();
197
198 Element currentRun;
199 Element currentParagraph;
200
201 /**
202 * This is the set of attributes used to store the
203 * input attributes.
204 */
205 MutableAttributeSet inputAttributes;
206
207 /**
208 * This listener will be attached to the caret of
209 * the text component that the EditorKit gets installed
210 * into. This should keep the input attributes updated
211 * for use by the styled actions.
212 */
213 private AttributeTracker inputAttributeUpdater;
214
215 /**
216 * Tracks caret movement and keeps the input attributes set
217 * to reflect the current set of attribute definitions at the
218 * caret position.
219 * <p>This implements PropertyChangeListener to update the
220 * input attributes when the Document changes, as if the Document
221 * changes the attributes will almost certainly change.
222 */
223 @SuppressWarnings("serial") // JDK-implementation class
224 class AttributeTracker implements CaretListener, PropertyChangeListener, Serializable {
225
226 /**
227 * Updates the attributes. <code>dot</code> and <code>mark</code>
228 * mark give the positions of the selection in <code>c</code>.
229 */
230 void updateInputAttributes(int dot, int mark, JTextComponent c) {
231 // EditorKit might not have installed the StyledDocument yet.
232 Document aDoc = c.getDocument();
233 if (!(aDoc instanceof StyledDocument)) {
234 return ;
235 }
236 int start = Math.min(dot, mark);
237 // record current character attributes.
238 StyledDocument doc = (StyledDocument)aDoc;
239 // If nothing is selected, get the attributes from the character
240 // before the start of the selection, otherwise get the attributes
241 // from the character element at the start of the selection.
242 Element run;
243 currentParagraph = doc.getParagraphElement(start);
244 if (currentParagraph.getStartOffset() == start || dot != mark) {
245 // Get the attributes from the character at the selection
246 // if in a different paragrah!
247 run = doc.getCharacterElement(start);
248 }
264 }
265
266 public void propertyChange(PropertyChangeEvent evt) {
267 Object newValue = evt.getNewValue();
268 Object source = evt.getSource();
269
270 if ((source instanceof JTextComponent) &&
271 (newValue instanceof Document)) {
272 // New document will have changed selection to 0,0.
273 updateInputAttributes(0, 0, (JTextComponent)source);
274 }
275 }
276
277 public void caretUpdate(CaretEvent e) {
278 updateInputAttributes(e.getDot(), e.getMark(),
279 (JTextComponent)e.getSource());
280 }
281 }
282
283 /**
284 * Copies the key/values in <code>element</code>s AttributeSet into
285 * <code>set</code>. This does not copy component, icon, or element
286 * names attributes. Subclasses may wish to refine what is and what
287 * isn't copied here. But be sure to first remove all the attributes that
288 * are in <code>set</code>.<p>
289 * This is called anytime the caret moves over a different location.
290 *
291 * @param element the element
292 * @param set the attributes
293 */
294 protected void createInputAttributes(Element element,
295 MutableAttributeSet set) {
296 if (element.getAttributes().getAttributeCount() > 0
297 || element.getEndOffset() - element.getStartOffset() > 1
298 || element.getEndOffset() < element.getDocument().getLength()) {
299 set.removeAttributes(set);
300 set.addAttributes(element.getAttributes());
301 set.removeAttribute(StyleConstants.ComponentAttribute);
302 set.removeAttribute(StyleConstants.IconAttribute);
303 set.removeAttribute(AbstractDocument.ElementNameAttribute);
304 set.removeAttribute(StyleConstants.ComposedTextAttribute);
305 }
306 }
307
308 // ---- default ViewFactory implementation ---------------------
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
400 */
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 *
520 * @param e the event
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 *
577 * @param e the action event
585 try {
586 size = Integer.parseInt(s, 10);
587 } catch (NumberFormatException nfe) {
588 }
589 }
590 if (size != 0) {
591 MutableAttributeSet attr = new SimpleAttributeSet();
592 StyleConstants.setFontSize(attr, size);
593 setCharacterAttributes(editor, attr, false);
594 } else {
595 UIManager.getLookAndFeel().provideErrorFeedback(editor);
596 }
597 }
598 }
599
600 private int size;
601 }
602
603 /**
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 *
645 * @param e the action event
653 try {
654 fg = Color.decode(s);
655 } catch (NumberFormatException nfe) {
656 }
657 }
658 if (fg != null) {
659 MutableAttributeSet attr = new SimpleAttributeSet();
660 StyleConstants.setForeground(attr, fg);
661 setCharacterAttributes(editor, attr, false);
662 } else {
663 UIManager.getLookAndFeel().provideErrorFeedback(editor);
664 }
665 }
666 }
667
668 private Color fg;
669 }
670
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 *
712 * @param e the action event
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);
843 if (editor != null) {
844 StyledEditorKit kit = getStyledEditorKit(editor);
845 MutableAttributeSet attr = kit.getInputAttributes();
846 boolean underline = (StyleConstants.isUnderline(attr)) ? false : true;
847 SimpleAttributeSet sas = new SimpleAttributeSet();
848 StyleConstants.setUnderline(sas, underline);
849 setCharacterAttributes(editor, sas, false);
850 }
851 }
852 }
853
854
855 /**
856 * StyledInsertBreakAction has similar behavior to that of
857 * <code>DefaultEditorKit.InsertBreakAction</code>. That is when
858 * its <code>actionPerformed</code> method is invoked, a newline
859 * is inserted. Beyond that, this will reset the input attributes to
860 * what they were before the newline was inserted.
861 */
862 @SuppressWarnings("serial") // Superclass is not serializable across versions
863 static class StyledInsertBreakAction extends StyledTextAction {
864 private SimpleAttributeSet tempSet;
865
866 StyledInsertBreakAction() {
867 super(insertBreakAction);
868 }
869
870 public void actionPerformed(ActionEvent e) {
871 JEditorPane target = getEditor(e);
872
873 if (target != null) {
874 if ((!target.isEditable()) || (!target.isEnabled())) {
875 UIManager.getLookAndFeel().provideErrorFeedback(target);
876 return;
877 }
878 StyledEditorKit sek = getStyledEditorKit(target);
|
169 }
170
171 /**
172 * Creates the AttributeSet used for the selection.
173 */
174 @SuppressWarnings("serial") // anonymous class
175 private void createInputAttributes() {
176 inputAttributes = new SimpleAttributeSet() {
177 public AttributeSet getResolveParent() {
178 return (currentParagraph != null) ?
179 currentParagraph.getAttributes() : null;
180 }
181
182 public Object clone() {
183 return new SimpleAttributeSet(this);
184 }
185 };
186 }
187
188 /**
189 * Creates a new {@code AttributeTracker}.
190 */
191 private void createInputAttributeUpdated() {
192 inputAttributeUpdater = new AttributeTracker();
193 }
194
195
196 private static final ViewFactory defaultFactory = new StyledViewFactory();
197
198 Element currentRun;
199 Element currentParagraph;
200
201 /**
202 * This is the set of attributes used to store the
203 * input attributes.
204 */
205 MutableAttributeSet inputAttributes;
206
207 /**
208 * This listener will be attached to the caret of
209 * the text component that the EditorKit gets installed
210 * into. This should keep the input attributes updated
211 * for use by the styled actions.
212 */
213 private AttributeTracker inputAttributeUpdater;
214
215 /**
216 * Tracks caret movement and keeps the input attributes set
217 * to reflect the current set of attribute definitions at the
218 * caret position.
219 * <p>This implements PropertyChangeListener to update the
220 * input attributes when the Document changes, as if the Document
221 * changes the attributes will almost certainly change.
222 */
223 @SuppressWarnings("serial") // JDK-implementation class
224 class AttributeTracker implements CaretListener, PropertyChangeListener, Serializable {
225
226 /**
227 * Updates the attributes. {@code dot} and {@code mark}
228 * mark give the positions of the selection in {@code c}.
229 */
230 void updateInputAttributes(int dot, int mark, JTextComponent c) {
231 // EditorKit might not have installed the StyledDocument yet.
232 Document aDoc = c.getDocument();
233 if (!(aDoc instanceof StyledDocument)) {
234 return ;
235 }
236 int start = Math.min(dot, mark);
237 // record current character attributes.
238 StyledDocument doc = (StyledDocument)aDoc;
239 // If nothing is selected, get the attributes from the character
240 // before the start of the selection, otherwise get the attributes
241 // from the character element at the start of the selection.
242 Element run;
243 currentParagraph = doc.getParagraphElement(start);
244 if (currentParagraph.getStartOffset() == start || dot != mark) {
245 // Get the attributes from the character at the selection
246 // if in a different paragrah!
247 run = doc.getCharacterElement(start);
248 }
264 }
265
266 public void propertyChange(PropertyChangeEvent evt) {
267 Object newValue = evt.getNewValue();
268 Object source = evt.getSource();
269
270 if ((source instanceof JTextComponent) &&
271 (newValue instanceof Document)) {
272 // New document will have changed selection to 0,0.
273 updateInputAttributes(0, 0, (JTextComponent)source);
274 }
275 }
276
277 public void caretUpdate(CaretEvent e) {
278 updateInputAttributes(e.getDot(), e.getMark(),
279 (JTextComponent)e.getSource());
280 }
281 }
282
283 /**
284 * Copies the key/values in {@code element}s AttributeSet into
285 * {@code set}. This does not copy component, icon, or element
286 * names attributes. Subclasses may wish to refine what is and what
287 * isn't copied here. But be sure to first remove all the attributes that
288 * are in {@code set}.<p>
289 * This is called anytime the caret moves over a different location.
290 *
291 * @param element the element
292 * @param set the attributes
293 */
294 protected void createInputAttributes(Element element,
295 MutableAttributeSet set) {
296 if (element.getAttributes().getAttributeCount() > 0
297 || element.getEndOffset() - element.getStartOffset() > 1
298 || element.getEndOffset() < element.getDocument().getLength()) {
299 set.removeAttributes(set);
300 set.addAttributes(element.getAttributes());
301 set.removeAttribute(StyleConstants.ComponentAttribute);
302 set.removeAttribute(StyleConstants.IconAttribute);
303 set.removeAttribute(AbstractDocument.ElementNameAttribute);
304 set.removeAttribute(StyleConstants.ComposedTextAttribute);
305 }
306 }
307
308 // ---- default ViewFactory implementation ---------------------
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} 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
400 */
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} 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 *
520 * @param e the event
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} 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 *
577 * @param e the action event
585 try {
586 size = Integer.parseInt(s, 10);
587 } catch (NumberFormatException nfe) {
588 }
589 }
590 if (size != 0) {
591 MutableAttributeSet attr = new SimpleAttributeSet();
592 StyleConstants.setFontSize(attr, size);
593 setCharacterAttributes(editor, attr, false);
594 } else {
595 UIManager.getLookAndFeel().provideErrorFeedback(editor);
596 }
597 }
598 }
599
600 private int size;
601 }
602
603 /**
604 * An action to set foreground color. This sets the
605 * {@code StyleConstants.Foreground} attribute for the
606 * currently selected range of the target JEditorPane.
607 * This is done by calling
608 * {@code StyledDocument.setCharacterAttributes}
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}, 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} 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 *
645 * @param e the action event
653 try {
654 fg = Color.decode(s);
655 } catch (NumberFormatException nfe) {
656 }
657 }
658 if (fg != null) {
659 MutableAttributeSet attr = new SimpleAttributeSet();
660 StyleConstants.setForeground(attr, fg);
661 setCharacterAttributes(editor, attr, false);
662 } else {
663 UIManager.getLookAndFeel().provideErrorFeedback(editor);
664 }
665 }
666 }
667
668 private Color fg;
669 }
670
671 /**
672 * An action to set paragraph alignment. This sets the
673 * {@code StyleConstants.Alignment} attribute for the
674 * currently selected range of the target JEditorPane.
675 * This is done by calling
676 * {@code StyledDocument.setParagraphAttributes}
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} 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} 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 *
712 * @param e the action event
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} 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} 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} 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);
843 if (editor != null) {
844 StyledEditorKit kit = getStyledEditorKit(editor);
845 MutableAttributeSet attr = kit.getInputAttributes();
846 boolean underline = (StyleConstants.isUnderline(attr)) ? false : true;
847 SimpleAttributeSet sas = new SimpleAttributeSet();
848 StyleConstants.setUnderline(sas, underline);
849 setCharacterAttributes(editor, sas, false);
850 }
851 }
852 }
853
854
855 /**
856 * StyledInsertBreakAction has similar behavior to that of
857 * {@code DefaultEditorKit.InsertBreakAction}. That is when
858 * its {@code actionPerformed} method is invoked, a newline
859 * is inserted. Beyond that, this will reset the input attributes to
860 * what they were before the newline was inserted.
861 */
862 @SuppressWarnings("serial") // Superclass is not serializable across versions
863 static class StyledInsertBreakAction extends StyledTextAction {
864 private SimpleAttributeSet tempSet;
865
866 StyledInsertBreakAction() {
867 super(insertBreakAction);
868 }
869
870 public void actionPerformed(ActionEvent e) {
871 JEditorPane target = getEditor(e);
872
873 if (target != null) {
874 if ((!target.isEditable()) || (!target.isEnabled())) {
875 UIManager.getLookAndFeel().provideErrorFeedback(target);
876 return;
877 }
878 StyledEditorKit sek = getStyledEditorKit(target);
|