jdk/src/share/classes/javax/swing/text/PlainDocument.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 438 2ae294e4518c
child 1299 027d966d5658
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A plain document that maintains no character attributes.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * default element structure for this document is a map of the lines in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the text.  The Element returned by getDefaultRootElement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a map of the lines, and each child element represents a line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This model does not maintain any character level attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * but each line can be tagged with an arbitrary set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Line to offset, and offset to line translations can be quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * performed using the default root element.  The structure information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of the DocumentEvent's fired by edits will indicate the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * structure changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The default content storage management is performed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * gapped buffer implementation (GapContent).  It supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * editing reasonably large documents with good efficiency when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the edits are contiguous or clustered, as is typical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see     Document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see     AbstractDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * to Use Threads</A> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param offs the starting offset >= 0
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)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                StringBuffer filtered = new StringBuffer(str);
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
                int rmCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                removed.addElement(rmCandidate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if ((offset + length == rmOffs1) && (lastOffset != rmOffs1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    ((index+1) < lineMap.getElementCount())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    rmCount += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    Element e = lineMap.getElement(index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    removed.addElement(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    rmOffs1 = e.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (lastOffset < rmOffs1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    added.addElement(createLeafElement(lineMap, null, lastOffset, rmOffs1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                Element[] aelems = new Element[added.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                added.copyInto(aelems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                Element[] relems = new Element[removed.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                removed.copyInto(relems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                ElementEdit ee = new ElementEdit(lineMap, index, relems, aelems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                chng.addEdit(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                lineMap.replace(index, relems.length, aelems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (Utilities.isComposedTextAttributeDefined(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                insertComposedTextUpdate(chng, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new Error("Internal error: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        super.insertUpdate(chng, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Updates any document structure as a result of text removal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * This will happen within a write lock. Since the structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * represents a line map, this just checks to see if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * removal spans lines.  If it does, the two lines outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * of the removal area are joined together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param chng the change event describing the edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    protected void removeUpdate(DefaultDocumentEvent chng) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        removed.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        BranchElement map = (BranchElement) getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        int offset = chng.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        int length = chng.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int line0 = map.getElementIndex(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int line1 = map.getElementIndex(offset + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (line0 != line1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // a line was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            for (int i = line0; i <= line1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                removed.addElement(map.getElement(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int p0 = map.getElement(line0).getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            int p1 = map.getElement(line1).getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            Element[] aelems = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            aelems[0] = createLeafElement(map, null, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            Element[] relems = new Element[removed.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            removed.copyInto(relems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            ElementEdit ee = new ElementEdit(map, line0, relems, aelems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            chng.addEdit(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            map.replace(line0, relems.length, aelems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            //Check for the composed text element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            Element line = map.getElement(line0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (!line.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                Element leaf = line.getElement(line.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                if (Utilities.isComposedTextElement(leaf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    Element[] aelem = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    aelem[0] = createLeafElement(map, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        line.getStartOffset(), line.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    Element[] relem = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    relem[0] = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    ElementEdit ee = new ElementEdit(map, line0, relem, aelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    chng.addEdit(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    map.replace(line0, 1, aelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        super.removeUpdate(chng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    // Inserts the composed text of an input method. The line element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    // where the composed text is inserted into becomes an branch element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    // which contains leaf elements of the composed text and the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // backing store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private void insertComposedTextUpdate(DefaultDocumentEvent chng, AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        added.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        BranchElement lineMap = (BranchElement) getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int offset = chng.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int length = chng.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int index = lineMap.getElementIndex(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        Element elem = lineMap.getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        int elemStart = elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int elemEnd = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        BranchElement[] abelem = new BranchElement[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        abelem[0] = (BranchElement) createBranchElement(lineMap, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        Element[] relem = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        relem[0] = elem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (elemStart != offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            added.addElement(createLeafElement(abelem[0], null, elemStart, offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        added.addElement(createLeafElement(abelem[0], attr, offset, offset+length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (elemEnd != offset+length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            added.addElement(createLeafElement(abelem[0], null, offset+length, elemEnd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        Element[] alelem = new Element[added.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        added.copyInto(alelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        ElementEdit ee = new ElementEdit(lineMap, index, relem, abelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        chng.addEdit(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        abelem[0].replace(0, 0, alelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        lineMap.replace(index, 1, abelem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private AbstractElement defaultRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private Vector added = new Vector();     // Vector<Element>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private Vector removed = new Vector();   // Vector<Element>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private transient Segment s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
}