jdk/src/share/classes/javax/swing/text/PlainDocument.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 22574 7f8ce0c8c20a
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, 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: 1299
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: 1299
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: 1299
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
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.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * A plain document that maintains no character attributes.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * default element structure for this document is a map of the lines in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * the text.  The Element returned by getDefaultRootElement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * a map of the lines, and each child element represents a line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This model does not maintain any character level attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * but each line can be tagged with an arbitrary set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Line to offset, and offset to line translations can be quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * performed using the default root element.  The structure information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * of the DocumentEvent's fired by edits will indicate the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * structure changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The default content storage management is performed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * gapped buffer implementation (GapContent).  It supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * editing reasonably large documents with good efficiency when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the edits are contiguous or clustered, as is typical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * 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: 20455
diff changeset
    51
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see     Document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see     AbstractDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    59
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class PlainDocument extends AbstractDocument {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Name of the attribute that specifies the tab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * size for tabs contained in the content.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * type for the value is Integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final String tabSizeAttribute = "tabSize";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Name of the attribute that specifies the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * length of a line, if there is a maximum length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * The type for the value is Integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static final String lineLimitAttribute = "lineLimit";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Constructs a plain text document.  A default model using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <code>GapContent</code> is constructed and set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public PlainDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this(new GapContent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Constructs a plain text document.  A default root element is created,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * and the tab size set to 8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param c  the container for the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public PlainDocument(Content c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        super(c);
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
    92
        putProperty(tabSizeAttribute, Integer.valueOf(8));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        defaultRoot = createDefaultRoot();
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
     * Inserts some content into the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Inserting content causes a write lock to be held while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * actual changes are taking place, followed by notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * to the observers on the thread that grabbed the write lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * are not. Please see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20158
diff changeset
   104
     * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 7668
diff changeset
   105
     * in Swing</A> for more information.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10316
diff changeset
   107
     * @param offs the starting offset &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param str the string to insert; does nothing with null/empty strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param a the attributes for the inserted content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception BadLocationException  the given insert position is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *   position within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see Document#insertString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // fields don't want to have multiple lines.  We may provide a field-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // model in the future in which case the filtering logic here will no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // be needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Object filterNewlines = getProperty("filterNewlines");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if ((filterNewlines instanceof Boolean) && filterNewlines.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if ((str != null) && (str.indexOf('\n') >= 0)) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   121
                StringBuilder filtered = new StringBuilder(str);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                int n = filtered.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    if (filtered.charAt(i) == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        filtered.setCharAt(i, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                str = filtered.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        super.insertString(offs, str, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Gets the default root element for the document model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see Document#getDefaultRootElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public Element getDefaultRootElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return defaultRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Creates the root element to be used to represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * default document structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return the element base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    protected AbstractElement createDefaultRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        BranchElement map = (BranchElement) createBranchElement(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Element line = createLeafElement(map, null, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Element[] lines = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        lines[0] = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        map.replace(0, 0, lines);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Get the paragraph element containing the given position.  Since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * document only models lines, it returns the line instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public Element getParagraphElement(int pos){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        Element lineMap = getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return lineMap.getElement( lineMap.getElementIndex( pos ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Updates document structure as a result of text insertion.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * will happen within a write lock.  Since this document simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * maps out lines, we refresh the line map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param chng the change event describing the dit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param attr the set of attributes for the inserted text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        removed.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        added.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        BranchElement lineMap = (BranchElement) getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int offset = chng.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int length = chng.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (offset > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
          offset -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
          length += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int index = lineMap.getElementIndex(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        Element rmCandidate = lineMap.getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        int rmOffs0 = rmCandidate.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int rmOffs1 = rmCandidate.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int lastOffset = rmOffs0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                s = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            getContent().getChars(offset, length, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            boolean hasBreaks = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                char c = s.array[s.offset + i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    int breakOffset = offset + i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    added.addElement(createLeafElement(lineMap, null, lastOffset, breakOffset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    lastOffset = breakOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    hasBreaks = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (hasBreaks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                removed.addElement(rmCandidate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                if ((offset + length == rmOffs1) && (lastOffset != rmOffs1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    ((index+1) < lineMap.getElementCount())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    Element e = lineMap.getElement(index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    removed.addElement(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    rmOffs1 = e.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (lastOffset < rmOffs1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    added.addElement(createLeafElement(lineMap, null, lastOffset, rmOffs1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                Element[] aelems = new Element[added.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                added.copyInto(aelems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                Element[] relems = new Element[removed.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                removed.copyInto(relems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                ElementEdit ee = new ElementEdit(lineMap, index, relems, aelems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                chng.addEdit(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                lineMap.replace(index, relems.length, aelems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (Utilities.isComposedTextAttributeDefined(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                insertComposedTextUpdate(chng, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new Error("Internal error: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        super.insertUpdate(chng, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Updates any document structure as a result of text removal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * This will happen within a write lock. Since the structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * represents a line map, this just checks to see if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * removal spans lines.  If it does, the two lines outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * of the removal area are joined together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param chng the change event describing the edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    protected void removeUpdate(DefaultDocumentEvent chng) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        removed.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        BranchElement map = (BranchElement) getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int offset = chng.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int length = chng.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        int line0 = map.getElementIndex(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        int line1 = map.getElementIndex(offset + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (line0 != line1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // a line was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            for (int i = line0; i <= line1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                removed.addElement(map.getElement(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            int p0 = map.getElement(line0).getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int p1 = map.getElement(line1).getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            Element[] aelems = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            aelems[0] = createLeafElement(map, null, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            Element[] relems = new Element[removed.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            removed.copyInto(relems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            ElementEdit ee = new ElementEdit(map, line0, relems, aelems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            chng.addEdit(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            map.replace(line0, relems.length, aelems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            //Check for the composed text element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            Element line = map.getElement(line0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (!line.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                Element leaf = line.getElement(line.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                if (Utilities.isComposedTextElement(leaf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    Element[] aelem = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    aelem[0] = createLeafElement(map, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        line.getStartOffset(), line.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    Element[] relem = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    relem[0] = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    ElementEdit ee = new ElementEdit(map, line0, relem, aelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    chng.addEdit(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    map.replace(line0, 1, aelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        super.removeUpdate(chng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    // Inserts the composed text of an input method. The line element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    // where the composed text is inserted into becomes an branch element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    // which contains leaf elements of the composed text and the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    // backing store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private void insertComposedTextUpdate(DefaultDocumentEvent chng, AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        added.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        BranchElement lineMap = (BranchElement) getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int offset = chng.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int length = chng.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int index = lineMap.getElementIndex(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Element elem = lineMap.getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int elemStart = elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int elemEnd = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        BranchElement[] abelem = new BranchElement[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        abelem[0] = (BranchElement) createBranchElement(lineMap, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        Element[] relem = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        relem[0] = elem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (elemStart != offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            added.addElement(createLeafElement(abelem[0], null, elemStart, offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        added.addElement(createLeafElement(abelem[0], attr, offset, offset+length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (elemEnd != offset+length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            added.addElement(createLeafElement(abelem[0], null, offset+length, elemEnd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        Element[] alelem = new Element[added.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        added.copyInto(alelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        ElementEdit ee = new ElementEdit(lineMap, index, relem, abelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        chng.addEdit(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        abelem[0].replace(0, 0, alelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        lineMap.replace(index, 1, abelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private AbstractElement defaultRoot;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   319
    private Vector<Element> added = new Vector<Element>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   320
    private Vector<Element> removed = new Vector<Element>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private transient Segment s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
}