📄 stylededitorkit.java
字号:
/* StyledEditorKit.java -- Copyright (C) 2002, 2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.text;import java.awt.Color;import java.awt.event.ActionEvent;import javax.swing.Action;import javax.swing.JEditorPane;import javax.swing.event.CaretEvent;import javax.swing.event.CaretListener;/** * An {@link EditorKit} that supports editing styled text. * * @author Andrew Selkirk * @author Roman Kennke (roman@kennke.org) */public class StyledEditorKit extends DefaultEditorKit{ /** The serialVersionUID. */ private static final long serialVersionUID = 7002391892985555948L; /** * Toggles the underline attribute for the selected text. */ public static class UnderlineAction extends StyledEditorKit.StyledTextAction { /** * Creates an instance of <code>UnderlineAction</code>. */ public UnderlineAction() { super("TODO"); // TODO: Figure out name for this action. } /** * Performs the action. * * @param event the <code>ActionEvent</code> that describes the action */ public void actionPerformed(ActionEvent event) { JEditorPane editor = getEditor(event); StyledDocument doc = getStyledDocument(editor); Element el = doc.getCharacterElement(editor.getSelectionStart()); boolean isUnderline = StyleConstants.isUnderline(el.getAttributes()); SimpleAttributeSet atts = new SimpleAttributeSet(); StyleConstants.setUnderline(atts, ! isUnderline); setCharacterAttributes(editor, atts, false); } } /** * Toggles the italic attribute for the selected text. */ public static class ItalicAction extends StyledEditorKit.StyledTextAction { /** * Creates an instance of <code>ItalicAction</code>. */ public ItalicAction() { super("TODO"); // TODO: Figure out correct name of this Action. } /** * Performs the action. * * @param event the <code>ActionEvent</code> that describes the action */ public void actionPerformed(ActionEvent event) { JEditorPane editor = getEditor(event); StyledDocument doc = getStyledDocument(editor); Element el = doc.getCharacterElement(editor.getSelectionStart()); boolean isItalic = StyleConstants.isItalic(el.getAttributes()); SimpleAttributeSet atts = new SimpleAttributeSet(); StyleConstants.setItalic(atts, ! isItalic); setCharacterAttributes(editor, atts, false); } } /** * Toggles the bold attribute for the selected text. */ public static class BoldAction extends StyledEditorKit.StyledTextAction { /** * Creates an instance of <code>BoldAction</code>. */ public BoldAction() { super("TODO"); // TODO: Figure out correct name of this Action. } /** * Performs the action. * * @param event the <code>ActionEvent</code> that describes the action */ public void actionPerformed(ActionEvent event) { JEditorPane editor = getEditor(event); StyledDocument doc = getStyledDocument(editor); Element el = doc.getCharacterElement(editor.getSelectionStart()); boolean isBold = StyleConstants.isBold(el.getAttributes()); SimpleAttributeSet atts = new SimpleAttributeSet(); StyleConstants.setItalic(atts, ! isBold); setCharacterAttributes(editor, atts, false); } } /** * Sets the alignment attribute on the selected text. */ public static class AlignmentAction extends StyledEditorKit.StyledTextAction { /** * The aligment to set. */ private int a; /** * Creates a new instance of <code>AlignmentAction</code> to set the * alignment to <code>a</code>. * * @param nm the name of the Action * @param a the alignment to set */ public AlignmentAction(String nm, int a) { super(nm); this.a = a; } /** * Performs the action. * * @param event the <code>ActionEvent</code> that describes the action */ public void actionPerformed(ActionEvent event) { SimpleAttributeSet atts = new SimpleAttributeSet(); StyleConstants.setAlignment(atts, a); setParagraphAttributes(getEditor(event), atts, false); } } /** * Sets the foreground color attribute on the selected text. */ public static class ForegroundAction extends StyledEditorKit.StyledTextAction { /** * The foreground color to set. */ private Color fg; /** * Creates a new instance of <code>ForegroundAction</code> to set the * foreground color to <code>fg</code>. * * @param nm the name of the Action * @param fg the foreground color to set */ public ForegroundAction(String nm, Color fg) { super(nm); this.fg = fg; } /** * Performs the action. * * @param event the <code>ActionEvent</code> that describes the action */ public void actionPerformed(ActionEvent event) { SimpleAttributeSet atts = new SimpleAttributeSet(); StyleConstants.setForeground(atts, fg); setCharacterAttributes(getEditor(event), atts, false); } } /** * Sets the font size attribute on the selected text. */ public static class FontSizeAction extends StyledEditorKit.StyledTextAction { /** * The font size to set. */ private int size; /** * Creates a new instance of <code>FontSizeAction</code> to set the * font size to <code>size</code>. * * @param nm the name of the Action * @param size the font size to set */ public FontSizeAction(String nm, int size) { super(nm); this.size = size; } /** * Performs the action. * * @param event the <code>ActionEvent</code> that describes the action */ public void actionPerformed(ActionEvent event) { SimpleAttributeSet atts = new SimpleAttributeSet(); StyleConstants.setFontSize(atts, size); setCharacterAttributes(getEditor(event), atts, false); } } /** * Sets the font family attribute on the selected text. */ public static class FontFamilyAction extends StyledEditorKit.StyledTextAction { /** * The font family to set. */ private String family; /** * Creates a new instance of <code>FontFamilyAction</code> to set the * font family to <code>family</code>. * * @param nm the name of the Action * @param family the font family to set */ public FontFamilyAction(String nm, String family) { super(nm); this.family = family; } /** * Performs the action. * * @param event the <code>ActionEvent</code> that describes the action */ public void actionPerformed(ActionEvent event) { SimpleAttributeSet atts = new SimpleAttributeSet(); StyleConstants.setFontFamily(atts, family); setCharacterAttributes(getEditor(event), atts, false); } } /** * The abstract superclass of all styled TextActions. This class * provides some useful methods to manipulate the text attributes. */ public abstract static class StyledTextAction extends TextAction { /** * Creates a new instance of <code>StyledTextAction</code>. * * @param nm the name of the <code>StyledTextAction</code> */ public StyledTextAction(String nm) { super(nm); } /** * Returns the <code>JEditorPane</code> component from which the * <code>ActionEvent</code> originated. * * @param event the <code>ActionEvent</code> * @return the <code>JEditorPane</code> component from which the * <code>ActionEvent</code> originated */ protected final JEditorPane getEditor(ActionEvent event) { return (JEditorPane) getTextComponent(event); } /** * Sets the specified character attributes on the currently selected * text of <code>editor</code>. If <code>editor</code> does not have * a selection, then the attributes are used as input attributes * for newly inserted content. * * @param editor the <code>JEditorPane</code> component * @param atts the text attributes to set * @param replace if <code>true</code> the current attributes of the * selection are replaces, otherwise they are merged */ protected final void setCharacterAttributes(JEditorPane editor, AttributeSet atts, boolean replace) { Document doc = editor.getDocument(); if (doc instanceof StyledDocument) { StyledDocument styleDoc = (StyledDocument) editor.getDocument(); EditorKit kit = editor.getEditorKit(); if (!(kit instanceof StyledEditorKit)) { StyledEditorKit styleKit = (StyledEditorKit) kit; int start = editor.getSelectionStart(); int end = editor.getSelectionEnd(); int dot = editor.getCaret().getDot(); if (start == dot && end == dot) { // If there is no selection, then we only update the // input attributes. MutableAttributeSet inputAttributes =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -