jdk/src/share/classes/javax/swing/text/DocumentFilter.java
author malenkov
Fri, 04 Apr 2014 20:18:53 +0400
changeset 24158 6afb40c4e9f8
parent 23010 6dadb192ad81
permissions -rw-r--r--
8039137: KSS: JTextComponent.isProcessInputMethodEventOverridden Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2000, 2013, 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * <code>DocumentFilter</code>, as the name implies, is a filter for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * <code>Document</code> mutation methods. When a <code>Document</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * containing a <code>DocumentFilter</code> is modified (either through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * <code>insert</code> or <code>remove</code>), it forwards the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * method invocation to the <code>DocumentFilter</code>. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * default implementation allows the modification to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * occur. Subclasses can filter the modifications by conditionally invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * methods on the superclass, or invoking the necessary methods on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the passed in <code>FilterBypass</code>. Subclasses should NOT call back
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * into the Document for the modification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * instead call into the superclass or the <code>FilterBypass</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * When <code>remove</code> or <code>insertString</code> is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * on the <code>DocumentFilter</code>, the <code>DocumentFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * may callback into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>FilterBypass</code> multiple times, or for different regions, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * it should not callback into the <code>FilterBypass</code> after returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * from the <code>remove</code> or <code>insertString</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * By default, text related document mutation methods such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>insertString</code>, <code>replace</code> and <code>remove</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * in <code>AbstractDocument</code> use <code>DocumentFilter</code> when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * available, and <code>Element</code> related mutation methods such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>create</code>, <code>insert</code> and <code>removeElement</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>DefaultStyledDocument</code> do not use <code>DocumentFilter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * If a method doesn't follow these defaults, this must be explicitly stated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * in the method documentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see javax.swing.text.Document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see javax.swing.text.AbstractDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see javax.swing.text.DefaultStyledDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class DocumentFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Invoked prior to removal of the specified region in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * specified Document. Subclasses that want to conditionally allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * removal should override this and only call supers implementation as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * necessary, or call directly into the <code>FilterBypass</code> as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param fb FilterBypass that can be used to mutate Document
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    71
     * @param offset the offset from the beginning &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    72
     * @param length the number of characters to remove &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @exception BadLocationException  some portion of the removal range
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *   was not a valid part of the document.  The location in the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *   is the first bad position encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void remove(FilterBypass fb, int offset, int length) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                       BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        fb.remove(offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Invoked prior to insertion of text into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * specified Document. Subclasses that want to conditionally allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * insertion should override this and only call supers implementation as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * necessary, or call directly into the FilterBypass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param fb FilterBypass that can be used to mutate Document
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    89
     * @param offset  the offset into the document to insert the content &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *    All positions that track change at or after the given location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *    will move.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param string the string to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param attr      the attributes to associate with the inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *   content.  This may be null if there are no attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @exception BadLocationException  the given insert position is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *   valid position within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public void insertString(FilterBypass fb, int offset, String string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                             AttributeSet attr) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        fb.insertString(offset, string, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Invoked prior to replacing a region of text in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * specified Document. Subclasses that want to conditionally allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * replace should override this and only call supers implementation as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * necessary, or call directly into the FilterBypass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param fb FilterBypass that can be used to mutate Document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param offset Location in Document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param length Length of text to delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param text Text to insert, null indicates no text to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param attrs AttributeSet indicating attributes of inserted text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *              null is legal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @exception BadLocationException  the given insert position is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *   valid position within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void replace(FilterBypass fb, int offset, int length, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        AttributeSet attrs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        fb.replace(offset, length, text, attrs);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Used as a way to circumvent calling back into the Document to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * change it. Document implementations that wish to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * a DocumentFilter must provide an implementation that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * not callback into the DocumentFilter when the following methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * are invoked from the DocumentFilter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static abstract class FilterBypass {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   134
         * Returns the Document the mutation is occurring on.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * @return Document that remove/insertString will operate on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        public abstract Document getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * Removes the specified region of text, bypassing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * DocumentFilter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   144
         * @param offset the offset from the beginning &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   145
         * @param length the number of characters to remove &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * @exception BadLocationException some portion of the removal range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         *   was not a valid part of the document.  The location in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         *   exception is the first bad position encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        public abstract void remove(int offset, int length) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                             BadLocationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         * Inserts the specified text, bypassing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         * DocumentFilter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         * @param offset  the offset into the document to insert the
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   157
         *   content &gt;= 0. All positions that track change at or after the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         *   given location will move.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * @param string the string to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * @param attr the attributes to associate with the inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         *   content.  This may be null if there are no attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * @exception BadLocationException  the given insert position is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         *   valid position within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        public abstract void insertString(int offset, String string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                          AttributeSet attr) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                   BadLocationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * Deletes the region of text from <code>offset</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * <code>offset + length</code>, and replaces it with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         *  <code>text</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * @param offset Location in Document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * @param length Length of text to delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * @param string Text to insert, null indicates no text to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * @param attrs AttributeSet indicating attributes of inserted text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         *              null is legal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * @exception BadLocationException  the given insert is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         *   valid position within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        public abstract void replace(int offset, int length, String string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                          AttributeSet attrs) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                   BadLocationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}