jdk/src/java.desktop/share/classes/javax/swing/text/GapContent.java
author ddehaven
Tue, 19 Aug 2014 10:32:16 -0700
changeset 26037 508779ce6619
parent 26004 jdk/src/share/classes/javax/swing/text/GapContent.java@7507a1b93f67
parent 25859 jdk/src/share/classes/javax/swing/text/GapContent.java@3317bb8137f4
child 28976 8c912c147654
permissions -rw-r--r--
Merge
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) 1998, 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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.undo.AbstractUndoableEdit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.undo.CannotRedoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.undo.CannotUndoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.undo.UndoableEdit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.SwingUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * An implementation of the AbstractDocument.Content interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * implemented using a gapped buffer similar to that used by emacs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The underlying storage is a array of unicode characters with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * a gap somewhere.  The gap is moved to the location of changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * to take advantage of common behavior where most changes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * in the same location.  Changes that occur at a gap boundary are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * generally cheap and moving the gap is generally cheaper than
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
    47
 * moving the array contents directly to accommodate the change.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The positions tracking change are also generally cheap to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * maintain.  The Position implementations (marks) store the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * index and can easily calculate the sequential position from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * the current gap location.  Changes only require update to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * the marks between the old and new gap boundaries when the gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * is moved, so generally updating the marks is pretty cheap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The marks are stored sorted so they can be located quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * with a binary search.  This increases the cost of adding a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * mark, and decreases the cost of keeping the mark updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
    61
@SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class GapContent extends GapVector implements AbstractDocument.Content, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Creates a new GapContent object.  Initial size defaults to 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public GapContent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Creates a new GapContent object, with the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * size specified.  The initial size will not be allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * to go below 2, to give room for the implied break and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * the gap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param initialLength the initial size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public GapContent(int initialLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        super(Math.max(initialLength,2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        char[] implied = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        implied[0] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        replace(0, 0, implied, implied.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        marks = new MarkVector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        search = new MarkData(0);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    87
        queue = new ReferenceQueue<StickyPosition>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Allocate an array to store items of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * appropriate (which is determined by the subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected Object allocateArray(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Get the length of the allocated array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected int getArrayLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        char[] carray = (char[]) getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return carray.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // --- AbstractDocument.Content methods -------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Returns the length of the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   111
     * @return the length &gt;= 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see AbstractDocument.Content#length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int len = getArrayLength() - (getGapEnd() - getGapStart());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Inserts a string into the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   122
     * @param where the starting position &gt;= 0, &lt; length()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param str the non-null string to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @return an UndoableEdit object for undoing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @exception BadLocationException if the specified position is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see AbstractDocument.Content#insertString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public UndoableEdit insertString(int where, String str) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (where > length() || where < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new BadLocationException("Invalid insert", length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        char[] chars = str.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        replace(where, 0, chars, chars.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return new InsertUndo(where, str.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Removes part of the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   140
     * @param where the starting position &gt;= 0, where + nitems &lt; length()
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   141
     * @param nitems the number of characters to remove &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return an UndoableEdit object for undoing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception BadLocationException if the specified position is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see AbstractDocument.Content#remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public UndoableEdit remove(int where, int nitems) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (where + nitems >= length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new BadLocationException("Invalid remove", length() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        String removedString = getString(where, nitems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        UndoableEdit edit = new RemoveUndo(where, removedString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        replace(where, nitems, empty, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return edit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
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
     * Retrieves a portion of the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   160
     * @param where the starting position &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   161
     * @param len the length to retrieve &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return a string representing the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @exception BadLocationException if the specified position is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see AbstractDocument.Content#getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public String getString(int where, int len) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        Segment s = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        getChars(where, len, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return new String(s.array, s.offset, s.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Retrieves a portion of the content.  If the desired content spans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * the gap, we copy the content.  If the desired content does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * span the gap, the actual store is returned to avoid the copy since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * it is contiguous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   178
     * @param where the starting position &gt;= 0, where + len &lt;= length()
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   179
     * @param len the number of characters to retrieve &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param chars the Segment object to return the characters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @exception BadLocationException if the specified position is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see AbstractDocument.Content#getChars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void getChars(int where, int len, Segment chars) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int end = where + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (where < 0 || end < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new BadLocationException("Invalid location", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (end > length() || where > length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new BadLocationException("Invalid location", length() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int g0 = getGapStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        int g1 = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        char[] array = (char[]) getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if ((where + len) <= g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // below gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            chars.array = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            chars.offset = where;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else if (where >= g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // above gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            chars.array = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            chars.offset = g1 + where - g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // spans the gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            int before = g0 - where;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (chars.isPartialReturn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                // partial return allowed, return amount before the gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                chars.array = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                chars.offset = where;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                chars.count = before;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // partial return not allowed, must copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            chars.array = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            chars.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            System.arraycopy(array, where, chars.array, 0, before);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            System.arraycopy(array, g1, chars.array, before, len - before);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        chars.count = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Creates a position within the content that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * track change as the content is mutated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   226
     * @param offset the offset to track &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @return the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @exception BadLocationException if the specified position is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public Position createPosition(int offset) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        while ( queue.poll() != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            unusedMarks++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (unusedMarks > Math.max(5, (marks.size() / 10))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            removeUnusedMarks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int g0 = getGapStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        int g1 = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int index = (offset < g0) ? offset : offset + (g1 - g0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        search.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int sortIndex = findSortIndex(search);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        MarkData m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        StickyPosition position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (sortIndex < marks.size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            && (m = marks.elementAt(sortIndex)).index == index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            && (position = m.getPosition()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            //position references the correct StickyPostition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            position = new StickyPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            m = new MarkData(index,position,queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            position.setMark(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            marks.insertElementAt(m, sortIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Holds the data for a mark... separately from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * the real mark so that the real mark (Position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * that the caller of createPosition holds) can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * collected if there are no more references to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * it.  The update table holds only a reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * to this data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   266
    final class MarkData extends WeakReference<StickyPosition> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        MarkData(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   272
        MarkData(int index, StickyPosition position, ReferenceQueue<? super StickyPosition> queue) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            super(position, queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         * Fetch the location in the contiguous sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * being modeled.  The index in the gap array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * is held by the mark, so it is adjusted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         * to it's relationship to the gap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         */
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 25193
diff changeset
   283
        public int getOffset() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            int g0 = getGapStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            int g1 = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            int offs = (index < g0) ? index : index - (g1 - g0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return Math.max(offs, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        StickyPosition getPosition() {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   291
            return get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    final class StickyPosition implements Position {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        StickyPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        void setMark(MarkData mark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            this.mark = mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 25193
diff changeset
   305
        public int getOffset() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return mark.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return Integer.toString(getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        MarkData mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    // --- variables --------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private static final char[] empty = new char[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private transient MarkVector marks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Record used for searching for the place to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * start updating mark indexs when the gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * boundaries are moved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private transient MarkData search;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * The number of unused mark entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private transient int unusedMarks = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   333
    private transient ReferenceQueue<StickyPosition> queue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    final static int GROWTH_SIZE = 1024 * 512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    // --- gap management -------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Make the gap bigger, moving any necessary data and updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * the appropriate marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    protected void shiftEnd(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        int oldGapEnd = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        super.shiftEnd(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // Adjust marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        int dg = getGapEnd() - oldGapEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        int adjustIndex = findMarkAdjustIndex(oldGapEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        int n = marks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        for (int i = adjustIndex; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            MarkData mark = marks.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            mark.index += dg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Overridden to make growth policy less agressive for large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * text amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    int getNewArraySize(int reqSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (reqSize < GROWTH_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return super.getNewArraySize(reqSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return reqSize + GROWTH_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Move the start of the gap to a new location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * without changing the size of the gap.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * moves the data in the array and updates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * marks accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    protected void shiftGap(int newGapStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int oldGapStart = getGapStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        int dg = newGapStart - oldGapStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        int oldGapEnd = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int newGapEnd = oldGapEnd + dg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int gapSize = oldGapEnd - oldGapStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // shift gap in the character array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        super.shiftGap(newGapStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        // update the marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (dg > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            // Move gap up, move data and marks down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            int adjustIndex = findMarkAdjustIndex(oldGapStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            int n = marks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            for (int i = adjustIndex; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                MarkData mark = marks.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                if (mark.index >= newGapEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                mark.index -= gapSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        } else if (dg < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            // Move gap down, move data and marks up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            int adjustIndex = findMarkAdjustIndex(newGapStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            int n = marks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            for (int i = adjustIndex; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                MarkData mark = marks.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                if (mark.index >= oldGapEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                mark.index += gapSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        resetMarksAtZero();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Resets all the marks that have an offset of 0 to have an index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * zero as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    protected void resetMarksAtZero() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (marks != null && getGapStart() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            int g1 = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            for (int counter = 0, maxCounter = marks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                 counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                MarkData mark = marks.elementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                if (mark.index <= g1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    mark.index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
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
     * Adjust the gap end downward.  This doesn't move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * any data, but it does update any marks affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * by the boundary change.  All marks from the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * gap start down to the new gap start are squeezed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * to the end of the gap (their location has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * removed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    protected void shiftGapStartDown(int newGapStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        // Push aside all marks from oldGapStart down to newGapStart.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        int adjustIndex = findMarkAdjustIndex(newGapStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        int n = marks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        int g0 = getGapStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        int g1 = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        for (int i = adjustIndex; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            MarkData mark = marks.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (mark.index > g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                // no more marks to adjust
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            mark.index = g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // shift the gap in the character array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        super.shiftGapStartDown(newGapStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        resetMarksAtZero();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Adjust the gap end upward.  This doesn't move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * any data, but it does update any marks affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * by the boundary change. All marks from the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * gap end up to the new gap end are squeezed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * to the end of the gap (their location has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * removed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    protected void shiftGapEndUp(int newGapEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        int adjustIndex = findMarkAdjustIndex(getGapEnd());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int n = marks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        for (int i = adjustIndex; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            MarkData mark = marks.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (mark.index >= newGapEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            mark.index = newGapEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        // shift the gap in the character array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        super.shiftGapEndUp(newGapEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        resetMarksAtZero();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Compares two marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @param o1 the first object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param o2 the second object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @return < 0 if o1 < o2, 0 if the same, > 0 if o1 > o2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    final int compare(MarkData o1, MarkData o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (o1.index < o2.index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        } else if (o1.index > o2.index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Finds the index to start mark adjustments given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * some search index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    final int findMarkAdjustIndex(int searchIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        search.index = Math.max(searchIndex, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        int index = findSortIndex(search);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        // return the first in the series
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // (ie. there may be duplicates).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        for (int i = index - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            MarkData d = marks.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (d.index != search.index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            index -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Finds the index of where to insert a new mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param o the mark to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    final int findSortIndex(MarkData o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        int lower = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        int upper = marks.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        int mid = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (upper == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   539
        int cmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        MarkData last = marks.elementAt(upper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        cmp = compare(o, last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            return upper + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        while (lower <= upper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            mid = lower + ((upper - lower) / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            MarkData entry = marks.elementAt(mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            cmp = compare(o, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            if (cmp == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                // found a match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                return mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            } else if (cmp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                upper = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                lower = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        // didn't find it, but we indicate the index of where it would belong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return (cmp < 0) ? mid : mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Remove all unused marks out of the sorted collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * of marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    final void removeUnusedMarks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int n = marks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        MarkVector cleaned = new MarkVector(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            MarkData mark = marks.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (mark.get() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                cleaned.addElement(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        marks = cleaned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        unusedMarks = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
   581
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    static class MarkVector extends GapVector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        MarkVector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        MarkVector(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            super(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         * Allocate an array to store items of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * appropriate (which is determined by the subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        protected Object allocateArray(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            return new MarkData[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         * Get the length of the allocated array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        protected int getArrayLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            MarkData[] marks = (MarkData[]) getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            return marks.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * Returns the number of marks currently held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            int len = getArrayLength() - (getGapEnd() - getGapStart());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         * Inserts a mark into the vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        public void insertElementAt(MarkData m, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            oneMark[0] = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            replace(index, 0, oneMark, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         * Add a mark to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        public void addElement(MarkData m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            insertElementAt(m, size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * Fetches the mark at the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        public MarkData elementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            int g0 = getGapStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            int g1 = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            MarkData[] array = (MarkData[]) getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            if (index < g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                // below gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                return array[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                // above gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                index += g1 - g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                return array[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         * Replaces the elements in the specified range with the passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         * in objects. This will NOT adjust the gap. The passed in indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         * do not account for the gap, they are the same as would be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         * int <code>elementAt</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        protected void replaceRange(int start, int end, Object[] marks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            int g0 = getGapStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            int g1 = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            int index = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            int newIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            Object[] array = (Object[]) getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            if (start >= g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                // Completely passed gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                index += (g1 - g0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                end += (g1 - g0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            else if (end >= g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                // straddles gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                end += (g1 - g0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                while (index < g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    array[index++] = marks[newIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                index = g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                // below gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                while (index < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    array[index++] = marks[newIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            while (index < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                array[index++] = marks[newIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        MarkData[] oneMark = new MarkData[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    // --- serialization -------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
      throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        marks = new MarkVector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        search = new MarkData(0);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   695
        queue = new ReferenceQueue<StickyPosition>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    // --- undo support --------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Returns a Vector containing instances of UndoPosRef for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * Positions in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * <code>offset</code> to <code>offset</code> + <code>length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * If <code>v</code> is not null the matching Positions are placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * there. The vector with the resulting Positions are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * @param v the Vector to use, with a new one created on null
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   709
     * @param offset the starting offset &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   710
     * @param length the length &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @return the set of instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23697
diff changeset
   713
    protected Vector<UndoPosRef> getPositionsInRange(Vector<UndoPosRef> v,
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23697
diff changeset
   714
                                                     int offset, int length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        int endOffset = offset + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        int startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        int endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        int g0 = getGapStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        int g1 = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        // Find the index of the marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if (offset < g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            if (offset == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                // findMarkAdjustIndex start at 1!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                startIndex = findMarkAdjustIndex(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            if (endOffset >= g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                endIndex = findMarkAdjustIndex(endOffset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            startIndex = findMarkAdjustIndex(offset + (g1 - g0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23697
diff changeset
   742
        Vector<UndoPosRef> placeIn = (v == null) ?
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23697
diff changeset
   743
            new Vector<>(Math.max(1, endIndex - startIndex)) :
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23697
diff changeset
   744
            v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        for (int counter = startIndex; counter < endIndex; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            placeIn.addElement(new UndoPosRef(marks.elementAt(counter)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return placeIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * Resets the location for all the UndoPosRef instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * in <code>positions</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * This is meant for internal usage, and is generally not of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * to subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @param positions the UndoPosRef instances to reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23697
diff changeset
   761
    protected void updateUndoPositions(Vector<UndoPosRef> positions, int offset,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                                       int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        // Find the indexs of the end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        int endOffset = offset + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        int g1 = getGapEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        int startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        int endIndex = findMarkAdjustIndex(g1 + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (offset != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            startIndex = findMarkAdjustIndex(g1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        // Reset the location of the refenences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        for(int counter = positions.size() - 1; counter >= 0; counter--) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23697
diff changeset
   778
            UndoPosRef ref = positions.elementAt(counter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            ref.resetLocation(endOffset, g1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        // We have to resort the marks in the range startIndex to endIndex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        // We can take advantage of the fact that it will be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        // increasing order, accept there will be a bunch of MarkData's with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        // the index g1 (or 0 if offset == 0) interspersed throughout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        if (startIndex < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            Object[] sorted = new Object[endIndex - startIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            int addIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            int counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (offset == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                // If the offset is 0, the positions won't have incremented,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                // have to do the reverse thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                // Find the elements in startIndex whose index is 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                for (counter = startIndex; counter < endIndex; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    MarkData mark = marks.elementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    if (mark.index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                        sorted[addIndex++] = mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                for (counter = startIndex; counter < endIndex; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    MarkData mark = marks.elementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    if (mark.index != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                        sorted[addIndex++] = mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                for (counter = startIndex; counter < endIndex; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    MarkData mark = marks.elementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    if (mark.index != g1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                        sorted[addIndex++] = mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                for (counter = startIndex; counter < endIndex; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    MarkData mark = marks.elementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    if (mark.index == g1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                        sorted[addIndex++] = mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            // And replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            marks.replaceRange(startIndex, endIndex, sorted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Used to hold a reference to a Mark that is being reset as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * result of removing from the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    final class UndoPosRef {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        UndoPosRef(MarkData rec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            this.rec = rec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            this.undoLocation = rec.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
         * Resets the location of the Position to the offset when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * receiver was instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         * @param endOffset end location of inserted string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         * @param g1 resulting end of gap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        protected void resetLocation(int endOffset, int g1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            if (undoLocation != endOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                this.rec.index = undoLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                this.rec.index = g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        /** Previous Offset of rec. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        protected int undoLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        /** Mark to reset offset. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        protected MarkData rec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    } // End of GapContent.UndoPosRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * UnoableEdit created for inserts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
   861
    @SuppressWarnings("serial") // Superclass is a JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    class InsertUndo extends AbstractUndoableEdit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        protected InsertUndo(int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        public void undo() throws CannotUndoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            super.undo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                // Get the Positions in the range being removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                posRefs = getPositionsInRange(null, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                string = getString(offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                remove(offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            } catch (BadLocationException bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
              throw new CannotUndoException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        public void redo() throws CannotRedoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            super.redo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                insertString(offset, string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                string = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                // Update the Positions that were in the range removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                if(posRefs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    updateUndoPositions(posRefs, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                    posRefs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            } catch (BadLocationException bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                throw new CannotRedoException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        /** Where string was inserted. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        protected int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        /** Length of string inserted. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        protected int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        /** The string that was inserted. This will only be valid after an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
         * undo. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        protected String string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        /** An array of instances of UndoPosRef for the Positions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
         * range that was removed, valid after undo. */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23697
diff changeset
   905
        protected Vector<UndoPosRef> posRefs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    } // GapContent.InsertUndo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * UndoableEdit created for removes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
   912
    @SuppressWarnings("serial") // JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    class RemoveUndo extends AbstractUndoableEdit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        protected RemoveUndo(int offset, String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            this.string = string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            this.length = string.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            posRefs = getPositionsInRange(null, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        public void undo() throws CannotUndoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            super.undo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                insertString(offset, string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                // Update the Positions that were in the range removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                if(posRefs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    updateUndoPositions(posRefs, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    posRefs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                string = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            } catch (BadLocationException bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
              throw new CannotUndoException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        public void redo() throws CannotRedoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            super.redo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                string = getString(offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                // Get the Positions in the range being removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                posRefs = getPositionsInRange(null, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                remove(offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            } catch (BadLocationException bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
              throw new CannotRedoException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        /** Where the string was removed from. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        protected int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        /** Length of string removed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        protected int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        /** The string that was removed. This is valid when redo is valid. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        protected String string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        /** An array of instances of UndoPosRef for the Positions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         * range that was removed, valid before undo. */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23697
diff changeset
   957
        protected Vector<UndoPosRef> posRefs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    } // GapContent.RemoveUndo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
}