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