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