jdk/src/java.desktop/share/classes/javax/swing/text/StyledEditorKit.java
author serb
Mon, 06 Mar 2017 22:52:45 +0300
changeset 44155 1bf93336ea45
parent 30462 507bcb03c954
permissions -rw-r--r--
8158209: Editing in TableView breaks the layout, when the document is I18n Reviewed-by: serb, alexsch Contributed-by: Abossolo Foh Guy <guy.abossolo.foh@scientificware.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.Action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.JEditorPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.KeyStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.UIManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This is the set of things needed by a text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * to be a reasonably functioning editor for some <em>type</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of text document.  This implementation provides a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * implementation which treats text as styled text and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * provides a minimal set of actions for editing styled text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22574
diff changeset
    47
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class StyledEditorKit extends DefaultEditorKit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Creates a new EditorKit used for styled documents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public StyledEditorKit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        createInputAttributeUpdated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        createInputAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Gets the input attributes for the pane.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * the caret moves and there is no selection, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * input attributes are automatically mutated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * reflect the character attributes of the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * caret location.  The styled editing actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * use the input attributes to carry out their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public MutableAttributeSet getInputAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return inputAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Fetches the element representing the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * run of character attributes for the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public Element getCharacterAttributeRun() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return currentRun;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // --- EditorKit methods ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Fetches the command list for the editor.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * the list of commands supported by the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * augmented by the collection of commands defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * locally for style operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return the command list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public Action[] getActions() {
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 20458
diff changeset
    94
        return TextAction.augmentList(super.getActions(), defaultActions);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Creates an uninitialized text storage model
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * that is appropriate for this type of editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public Document createDefaultDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return new DefaultStyledDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Called when the kit is being installed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * a JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param c the JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public void install(JEditorPane c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        c.addCaretListener(inputAttributeUpdater);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        c.addPropertyChangeListener(inputAttributeUpdater);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Caret caret = c.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (caret != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            inputAttributeUpdater.updateInputAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                  (caret.getDot(), caret.getMark(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Called when the kit is being removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * JEditorPane.  This is used to unregister any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * listeners that were attached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param c the JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void deinstall(JEditorPane c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        c.removeCaretListener(inputAttributeUpdater);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        c.removePropertyChangeListener(inputAttributeUpdater);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // remove references to current document so it can be collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        currentRun = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        currentParagraph = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Fetches a factory that is suitable for producing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * views of any models that are produced by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * kit.  This is implemented to return View implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * for the following kinds of elements:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <li>AbstractDocument.ContentElementName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <li>AbstractDocument.ParagraphElementName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <li>AbstractDocument.SectionElementName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <li>StyleConstants.ComponentElementName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <li>StyleConstants.IconElementName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public ViewFactory getViewFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return defaultFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Creates a copy of the editor kit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        StyledEditorKit o = (StyledEditorKit)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        o.currentRun = o.currentParagraph = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        o.createInputAttributeUpdated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        o.createInputAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Creates the AttributeSet used for the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22574
diff changeset
   174
    @SuppressWarnings("serial") // anonymous class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private void createInputAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        inputAttributes = new SimpleAttributeSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                return (currentParagraph != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                           currentParagraph.getAttributes() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return new SimpleAttributeSet(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Creates a new <code>AttributeTracker</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private void createInputAttributeUpdated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        inputAttributeUpdater = new AttributeTracker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private static final ViewFactory defaultFactory = new StyledViewFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    Element currentRun;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    Element currentParagraph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * This is the set of attributes used to store the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * input attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    MutableAttributeSet inputAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * This listener will be attached to the caret of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * the text component that the EditorKit gets installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * into.  This should keep the input attributes updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * for use by the styled actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private AttributeTracker inputAttributeUpdater;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Tracks caret movement and keeps the input attributes set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * to reflect the current set of attribute definitions at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * caret position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <p>This implements PropertyChangeListener to update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * input attributes when the Document changes, as if the Document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * changes the attributes will almost certainly change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22574
diff changeset
   223
    @SuppressWarnings("serial") // JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    class AttributeTracker implements CaretListener, PropertyChangeListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         * Updates the attributes. <code>dot</code> and <code>mark</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         * mark give the positions of the selection in <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        void updateInputAttributes(int dot, int mark, JTextComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // EditorKit might not have installed the StyledDocument yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            Document aDoc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (!(aDoc instanceof StyledDocument)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                return ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            int start = Math.min(dot, mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // record current character attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            StyledDocument doc = (StyledDocument)aDoc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            // If nothing is selected, get the attributes from the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // before the start of the selection, otherwise get the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            // from the character element at the start of the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            Element run;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            currentParagraph = doc.getParagraphElement(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (currentParagraph.getStartOffset() == start || dot != mark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                // Get the attributes from the character at the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                // if in a different paragrah!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                run = doc.getCharacterElement(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                run = doc.getCharacterElement(Math.max(start-1, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (run != currentRun) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                     * PENDING(prinz) All attributes that represent a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                     * glyph position and can't be inserted into should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                     * removed from the input attributes... this requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                     * mixing in an interface to indicate that condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                     * When we can add things again this logic needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                     * improved!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                currentRun = run;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                createInputAttributes(currentRun, getInputAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        public void propertyChange(PropertyChangeEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            Object newValue = evt.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            Object source = evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if ((source instanceof JTextComponent) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                (newValue instanceof Document)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                // New document will have changed selection to 0,0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                updateInputAttributes(0, 0, (JTextComponent)source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        public void caretUpdate(CaretEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            updateInputAttributes(e.getDot(), e.getMark(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                  (JTextComponent)e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Copies the key/values in <code>element</code>s AttributeSet into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <code>set</code>. This does not copy component, icon, or element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * names attributes. Subclasses may wish to refine what is and what
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * isn't copied here. But be sure to first remove all the attributes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * are in <code>set</code>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * This is called anytime the caret moves over a different location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
   291
     * @param element the element
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
   292
     * @param set the attributes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    protected void createInputAttributes(Element element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                         MutableAttributeSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (element.getAttributes().getAttributeCount() > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            || element.getEndOffset() - element.getStartOffset() > 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            || element.getEndOffset() < element.getDocument().getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            set.removeAttributes(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            set.addAttributes(element.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            set.removeAttribute(StyleConstants.ComponentAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            set.removeAttribute(StyleConstants.IconAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            set.removeAttribute(AbstractDocument.ElementNameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            set.removeAttribute(StyleConstants.ComposedTextAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    // ---- default ViewFactory implementation ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    static class StyledViewFactory implements ViewFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        public View create(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            String kind = elem.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (kind != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                if (kind.equals(AbstractDocument.ContentElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    return new LabelView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    return new ParagraphView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                } else if (kind.equals(AbstractDocument.SectionElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    return new BoxView(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                } else if (kind.equals(StyleConstants.ComponentElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    return new ComponentView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                } else if (kind.equals(StyleConstants.IconElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    return new IconView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            // default to text display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return new LabelView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    // --- Action implementations ---------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private static final Action[] defaultActions = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        new FontFamilyAction("font-family-SansSerif", "SansSerif"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        new FontFamilyAction("font-family-Monospaced", "Monospaced"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        new FontFamilyAction("font-family-Serif", "Serif"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        new FontSizeAction("font-size-8", 8),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        new FontSizeAction("font-size-10", 10),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        new FontSizeAction("font-size-12", 12),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        new FontSizeAction("font-size-14", 14),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        new FontSizeAction("font-size-16", 16),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        new FontSizeAction("font-size-18", 18),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        new FontSizeAction("font-size-24", 24),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        new FontSizeAction("font-size-36", 36),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        new FontSizeAction("font-size-48", 48),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        new AlignmentAction("left-justify", StyleConstants.ALIGN_LEFT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        new AlignmentAction("center-justify", StyleConstants.ALIGN_CENTER),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        new AlignmentAction("right-justify", StyleConstants.ALIGN_RIGHT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        new BoldAction(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        new ItalicAction(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        new StyledInsertBreakAction(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        new UnderlineAction()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * An action that assumes it's being fired on a JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * with a StyledEditorKit (or subclass) installed.  This has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * some convenience methods for causing character or paragraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * level attribute changes.  The convenience methods will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * throw an IllegalArgumentException if the assumption of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * a StyledDocument, a JEditorPane, or a StyledEditorKit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * fail to be true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * The component that gets acted upon by the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * will be the source of the ActionEvent if the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * can be narrowed to a JEditorPane type.  If the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * can't be narrowed, the most recently focused text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * component is changed.  If neither of these are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * case, the action cannot be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20158
diff changeset
   379
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22567
diff changeset
   383
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public abstract static class StyledTextAction extends TextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         * Creates a new StyledTextAction from a string action name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * @param nm the name of the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        public StyledTextAction(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            super(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         * Gets the target editor for an action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * @param e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * @return the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        protected final JEditorPane getEditor(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            JTextComponent tcomp = getTextComponent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (tcomp instanceof JEditorPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                return (JEditorPane) tcomp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         * Gets the document associated with an editor pane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * @param e the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * @return the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * @exception IllegalArgumentException for the wrong document type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        protected final StyledDocument getStyledDocument(JEditorPane e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            Document d = e.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if (d instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                return (StyledDocument) d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            throw new IllegalArgumentException("document must be StyledDocument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * Gets the editor kit associated with an editor pane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * @param e the editor pane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * @return the kit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * @exception IllegalArgumentException for the wrong document type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        protected final StyledEditorKit getStyledEditorKit(JEditorPane e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            EditorKit k = e.getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (k instanceof StyledEditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                return (StyledEditorKit) k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            throw new IllegalArgumentException("EditorKit must be StyledEditorKit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * Applies the given attributes to character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * content.  If there is a selection, the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * are applied to the selection range.  If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * is no selection, the attributes are applied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * the input attribute set which defines the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         * for any new text that gets inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * @param editor the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         * @param attr the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         * @param replace   if true, then replace the existing attributes first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        protected final void setCharacterAttributes(JEditorPane editor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                                              AttributeSet attr, boolean replace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            int p0 = editor.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            int p1 = editor.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (p0 != p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                StyledDocument doc = getStyledDocument(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            StyledEditorKit k = getStyledEditorKit(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            MutableAttributeSet inputAttributes = k.getInputAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (replace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                inputAttributes.removeAttributes(inputAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            inputAttributes.addAttributes(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         * Applies the given attributes to paragraphs.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         * there is a selection, the attributes are applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
         * to the paragraphs that intersect the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * if there is no selection, the attributes are applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         * to the paragraph at the current caret position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         * @param editor the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         * @param attr the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         * @param replace   if true, replace the existing attributes first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        protected final void setParagraphAttributes(JEditorPane editor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                           AttributeSet attr, boolean replace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            int p0 = editor.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            int p1 = editor.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            StyledDocument doc = getStyledDocument(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * An action to set the font family in the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * JEditorPane.  This will use the family specified as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * the command string on the ActionEvent if there is one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * otherwise the family that was initialized with will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20158
diff changeset
   499
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22567
diff changeset
   503
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public static class FontFamilyAction extends StyledTextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * Creates a new FontFamilyAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         * @param nm the action name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         * @param family the font family
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        public FontFamilyAction(String nm, String family) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            super(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            this.family = family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * Sets the font family.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
         * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            JEditorPane editor = getEditor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                String family = this.family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                if ((e != null) && (e.getSource() == editor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    String s = e.getActionCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        family = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                if (family != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    MutableAttributeSet attr = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    StyleConstants.setFontFamily(attr, family);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    setCharacterAttributes(editor, attr, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    UIManager.getLookAndFeel().provideErrorFeedback(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        private String family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * An action to set the font size in the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * JEditorPane.  This will use the size specified as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * the command string on the ActionEvent if there is one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * otherwise the size that was initialized with will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20158
diff changeset
   556
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22567
diff changeset
   560
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    public static class FontSizeAction extends StyledTextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         * Creates a new FontSizeAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         * @param nm the action name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         * @param size the font size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        public FontSizeAction(String nm, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            super(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         * Sets the font size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * @param e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            JEditorPane editor = getEditor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                int size = this.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                if ((e != null) && (e.getSource() == editor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    String s = e.getActionCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        size = Integer.parseInt(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                if (size != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    MutableAttributeSet attr = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    StyleConstants.setFontSize(attr, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    setCharacterAttributes(editor, attr, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    UIManager.getLookAndFeel().provideErrorFeedback(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * An action to set foreground color.  This sets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * <code>StyleConstants.Foreground</code> attribute for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * currently selected range of the target JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * This is done by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <code>StyledDocument.setCharacterAttributes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * on the styled document associated with the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * If the target text component is specified as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * source of the ActionEvent and there is a command string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * the command string will be interpreted as the foreground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * color.  It will be interpreted by called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <code>Color.decode</code>, and should therefore be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * legal input for that method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20158
diff changeset
   624
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22567
diff changeset
   628
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public static class ForegroundAction extends StyledTextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * Creates a new ForegroundAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         * @param nm the action name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         * @param fg the foreground color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        public ForegroundAction(String nm, Color fg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            super(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            this.fg = fg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         * Sets the foreground color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         * @param e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            JEditorPane editor = getEditor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                Color fg = this.fg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                if ((e != null) && (e.getSource() == editor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    String s = e.getActionCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        fg = Color.decode(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                if (fg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    MutableAttributeSet attr = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    StyleConstants.setForeground(attr, fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    setCharacterAttributes(editor, attr, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    UIManager.getLookAndFeel().provideErrorFeedback(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        private Color fg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * An action to set paragraph alignment.  This sets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * <code>StyleConstants.Alignment</code> attribute for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * currently selected range of the target JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * This is done by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * <code>StyledDocument.setParagraphAttributes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * on the styled document associated with the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * If the target text component is specified as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * source of the ActionEvent and there is a command string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * the command string will be interpreted as an integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * that should be one of the legal values for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <code>StyleConstants.Alignment</code> attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20158
diff changeset
   691
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22567
diff changeset
   695
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public static class AlignmentAction extends StyledTextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         * Creates a new AlignmentAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         * @param nm the action name
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   702
         * @param a the alignment &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        public AlignmentAction(String nm, int a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            super(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            this.a = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
         * Sets the alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         * @param e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            JEditorPane editor = getEditor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                int a = this.a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                if ((e != null) && (e.getSource() == editor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    String s = e.getActionCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        a = Integer.parseInt(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                MutableAttributeSet attr = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                StyleConstants.setAlignment(attr, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                setParagraphAttributes(editor, attr, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        private int a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * An action to toggle the bold attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20158
diff changeset
   742
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22567
diff changeset
   746
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    public static class BoldAction extends StyledTextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         * Constructs a new BoldAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        public BoldAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            super("font-bold");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
         * Toggles the bold attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         * @param e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            JEditorPane editor = getEditor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                StyledEditorKit kit = getStyledEditorKit(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                MutableAttributeSet attr = kit.getInputAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                boolean bold = (StyleConstants.isBold(attr)) ? false : true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                SimpleAttributeSet sas = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                StyleConstants.setBold(sas, bold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                setCharacterAttributes(editor, sas, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * An action to toggle the italic attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20158
diff changeset
   782
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22567
diff changeset
   786
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    public static class ItalicAction extends StyledTextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         * Constructs a new ItalicAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        public ItalicAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            super("font-italic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         * Toggles the italic attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
         * @param e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            JEditorPane editor = getEditor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                StyledEditorKit kit = getStyledEditorKit(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                MutableAttributeSet attr = kit.getInputAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                boolean italic = (StyleConstants.isItalic(attr)) ? false : true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                SimpleAttributeSet sas = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                StyleConstants.setItalic(sas, italic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                setCharacterAttributes(editor, sas, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * An action to toggle the underline attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20158
diff changeset
   822
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22567
diff changeset
   826
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    public static class UnderlineAction extends StyledTextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
         * Constructs a new UnderlineAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        public UnderlineAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            super("font-underline");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * Toggles the Underline attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         * @param e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            JEditorPane editor = getEditor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            if (editor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                StyledEditorKit kit = getStyledEditorKit(editor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                MutableAttributeSet attr = kit.getInputAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                boolean underline = (StyleConstants.isUnderline(attr)) ? false : true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                SimpleAttributeSet sas = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                StyleConstants.setUnderline(sas, underline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                setCharacterAttributes(editor, sas, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * StyledInsertBreakAction has similar behavior to that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * <code>DefaultEditorKit.InsertBreakAction</code>. That is when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * its <code>actionPerformed</code> method is invoked, a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * is inserted. Beyond that, this will reset the input attributes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * what they were before the newline was inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22574
diff changeset
   862
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    static class StyledInsertBreakAction extends StyledTextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        private SimpleAttributeSet tempSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        StyledInsertBreakAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            super(insertBreakAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            JEditorPane target = getEditor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            if (target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                if ((!target.isEditable()) || (!target.isEnabled())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    UIManager.getLookAndFeel().provideErrorFeedback(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                StyledEditorKit sek = getStyledEditorKit(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                if (tempSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                    tempSet.removeAttributes(tempSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                    tempSet = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                tempSet.addAttributes(sek.getInputAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                target.replaceSelection("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                MutableAttributeSet ia = sek.getInputAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                ia.removeAttributes(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                ia.addAttributes(tempSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                tempSet.removeAttributes(tempSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                // See if we are in a JTextComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                JTextComponent text = getTextComponent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                    if ((!text.isEditable()) || (!text.isEnabled())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                        UIManager.getLookAndFeel().provideErrorFeedback(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    text.replaceSelection("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
}