public class GapContent extends Object implements AbstractDocument.Content, Serializable
AbstractDocument.Content
uses a gapped buffer.
This takes advantage of the fact that text area content is mostly inserted
sequentially. The buffer is a char array that maintains a gap at the current
insertion point. If characters a inserted at gap boundaries, the cost is
minimal (simple array access). The array only has to be shifted around when
the insertion point moves (then the gap also moves and one array copy is
necessary) or when the gap is filled up and the buffer has to be enlarged.Constructor and Description |
---|
GapContent()
Creates a new GapContent object.
|
GapContent(int size)
Creates a new GapContent object with a specified initial size.
|
Modifier and Type | Method and Description |
---|---|
protected Object |
allocateArray(int size)
Allocates an array of the specified length that can then be used as
buffer.
|
Position |
createPosition(int offset)
Creates and returns a mark at the specified position.
|
protected Object |
getArray()
Returns the allocated buffer array.
|
protected int |
getArrayLength()
Returns the length of the allocated buffer array.
|
void |
getChars(int where,
int len,
Segment txt)
Fetches a piece of content and stores it in a
Segment object. |
protected int |
getGapEnd()
Returns the end index of the gap within the buffer array.
|
protected int |
getGapStart()
Returns the start index of the gap within the buffer array.
|
protected Vector |
getPositionsInRange(Vector v,
int offset,
int length)
Returns all
Position s that are in the range specified by
offset and length within the buffer array. |
String |
getString(int where,
int len)
Returns a piece of content as String.
|
UndoableEdit |
insertString(int where,
String str)
Inserts a string at the specified position.
|
int |
length()
Returns the length of the content.
|
UndoableEdit |
remove(int where,
int nitems)
Removes a piece of content at th specified position.
|
protected void |
replace(int position,
int rmSize,
Object addItems,
int addSize)
Replaces a portion of the storage with the specified items.
|
protected void |
resetMarksAtZero()
Resets all
Position that have an offset of 0 ,
to also have an array index of 0 . |
protected void |
shiftEnd(int newSize)
Enlarges the gap.
|
protected void |
shiftGap(int newGapStart)
Shifts the gap to the specified position.
|
protected void |
shiftGapEndUp(int newGapEnd)
Shifts the gap end upwards.
|
protected void |
shiftGapStartDown(int newGapStart)
Shifts the gap start downwards.
|
protected void |
updateUndoPositions(Vector positions,
int offset,
int length)
Resets the positions in the specified range to their original offset
after a undo operation is performed.
|
public GapContent()
public GapContent(int size)
size
- the initial size of the bufferprotected Object allocateArray(int size)
size
- the size of the array to be allocatedprotected int getArrayLength()
public int length()
length
in interface AbstractDocument.Content
public UndoableEdit insertString(int where, String str) throws BadLocationException
insertString
in interface AbstractDocument.Content
where
- the position where the string is insertedstr
- the string that is to be insertedBadLocationException
- if where
is not a valid
location in the bufferpublic UndoableEdit remove(int where, int nitems) throws BadLocationException
remove
in interface AbstractDocument.Content
where
- the position where the content is to be removednitems
- number of characters to be removedBadLocationException
- if where
is not a valid
location in the bufferpublic String getString(int where, int len) throws BadLocationException
getString
in interface AbstractDocument.Content
where
- the start location of the fragmentlen
- the length of the fragmentBadLocationException
- if where
or
where + len
are no valid locations in the bufferpublic void getChars(int where, int len, Segment txt) throws BadLocationException
Segment
object.
If the requested piece of text spans the gap, the content is copied into a
new array. If it doesn't then it is contiguous and the actual content
store is returned.getChars
in interface AbstractDocument.Content
where
- the start location of the fragmentlen
- the length of the fragmenttxt
- the Segment object to store the fragment inBadLocationException
- if where
or
where + len
are no valid locations in the bufferpublic Position createPosition(int offset) throws BadLocationException
createPosition
in interface AbstractDocument.Content
offset
- the position at which to create the markBadLocationException
- if the offset is not a valid position in the
bufferprotected void shiftEnd(int newSize)
newSize
- the new size of the gapprotected void shiftGap(int newGapStart)
newGapStart
- the new start position of the gapprotected void shiftGapStartDown(int newGapStart)
newGapStart
- the new gap startprotected void shiftGapEndUp(int newGapEnd)
newGapEnd
- the new gap startprotected final Object getArray()
protected void replace(int position, int rmSize, Object addItems, int addSize)
position
- the position at which to remove itemsrmSize
- the number of items to removeaddItems
- the items to add at locationaddSize
- the number of items to addprotected final int getGapStart()
protected final int getGapEnd()
protected Vector getPositionsInRange(Vector v, int offset, int length)
Position
s that are in the range specified by
offset
and length within the buffer array.v
- the vector to use; if null
, a new Vector is allocatedoffset
- the start offset of the range to searchlength
- the length of the range to searchprotected void resetMarksAtZero()
Position
that have an offset of 0
,
to also have an array index of 0
. This might be necessary
after a call to shiftGap(0)
, since then the marks at offset
0
get shifted to gapEnd
.protected void updateUndoPositions(Vector positions, int offset, int length)
positions
- the positions to updateoffset
- length
-