StringContentpublic final class StringContent extends Object implements Serializable, AbstractDocument$ContentAn implementation of the AbstractDocument.Content interface that is
a brute force implementation that is useful for relatively small
documents and/or debugging. It manages the character content
as a simple character array. It is also quite inefficient.
It is generally recommended that the gap buffer or piece table
implementations be used instead. This buffer does not scale up
to large sizes.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans package.
Please see {@link java.beans.XMLEncoder}. |
Fields Summary |
---|
private static final char[] | empty | private char[] | data | private int | count | transient Vector | marks |
Constructors Summary |
---|
public StringContent()Creates a new StringContent object. Initial size defaults to 10.
this(10);
| public StringContent(int initialLength)Creates a new StringContent object, with the initial
size specified. If the length is < 1, a size of 1 is used.
if (initialLength < 1) {
initialLength = 1;
}
data = new char[initialLength];
data[0] = '\n";
count = 1;
|
Methods Summary |
---|
public javax.swing.text.Position | createPosition(int offset)Creates a position within the content that will
track change as the content is mutated.
// some small documents won't have any sticky positions
// at all, so the buffer is created lazily.
if (marks == null) {
marks = new Vector();
}
return new StickyPosition(offset);
| public void | getChars(int where, int len, javax.swing.text.Segment chars)Retrieves a portion of the content. where + len must be <= length()
if (where + len > count) {
throw new BadLocationException("Invalid location", count);
}
chars.array = data;
chars.offset = where;
chars.count = len;
| protected java.util.Vector | getPositionsInRange(java.util.Vector v, int offset, int length)Returns a Vector containing instances of UndoPosRef for the
Positions in the range
offset to offset + length .
If v is not null the matching Positions are placed in
there. The vector with the resulting Positions are returned.
This is meant for internal usage, and is generally not of interest
to subclasses.
int n = marks.size();
int end = offset + length;
Vector placeIn = (v == null) ? new Vector() : v;
for (int i = 0; i < n; i++) {
PosRec mark = (PosRec) marks.elementAt(i);
if (mark.unused) {
// this record is no longer used, get rid of it
marks.removeElementAt(i);
i -= 1;
n -= 1;
} else if(mark.offset >= offset && mark.offset <= end)
placeIn.addElement(new UndoPosRef(mark));
}
return placeIn;
| public java.lang.String | getString(int where, int len)Retrieves a portion of the content. where + len must be <= length().
if (where + len > count) {
throw new BadLocationException("Invalid range", count);
}
return new String(data, where, len);
| public javax.swing.undo.UndoableEdit | insertString(int where, java.lang.String str)Inserts a string into the content.
if (where >= count || where < 0) {
throw new BadLocationException("Invalid location", count);
}
char[] chars = str.toCharArray();
replace(where, 0, chars, 0, chars.length);
if (marks != null) {
updateMarksForInsert(where, str.length());
}
return new InsertUndo(where, str.length());
| public int | length()Returns the length of the content.
return count;
| public javax.swing.undo.UndoableEdit | remove(int where, int nitems)Removes part of the content. where + nitems must be < length().
if (where + nitems >= count) {
throw new BadLocationException("Invalid range", count);
}
String removedString = getString(where, nitems);
UndoableEdit edit = new RemoveUndo(where, removedString);
replace(where, nitems, empty, 0, 0);
if (marks != null) {
updateMarksForRemove(where, nitems);
}
return edit;
| void | replace(int offset, int length, char[] replArray, int replOffset, int replLength)Replaces some of the characters in the array
int delta = replLength - length;
int src = offset + length;
int nmove = count - src;
int dest = src + delta;
if ((count + delta) >= data.length) {
// need to grow the array
int newLength = Math.max(2*data.length, count + delta);
char[] newData = new char[newLength];
System.arraycopy(data, 0, newData, 0, offset);
System.arraycopy(replArray, replOffset, newData, offset, replLength);
System.arraycopy(data, src, newData, dest, nmove);
data = newData;
} else {
// patch the existing array
System.arraycopy(data, src, data, dest, nmove);
System.arraycopy(replArray, replOffset, data, offset, replLength);
}
count = count + delta;
| void | resize(int ncount)
char[] ndata = new char[ncount];
System.arraycopy(data, 0, ndata, 0, Math.min(ncount, count));
data = ndata;
| synchronized void | updateMarksForInsert(int offset, int length)
if (offset == 0) {
// zero is a special case where we update only
// marks after it.
offset = 1;
}
int n = marks.size();
for (int i = 0; i < n; i++) {
PosRec mark = (PosRec) marks.elementAt(i);
if (mark.unused) {
// this record is no longer used, get rid of it
marks.removeElementAt(i);
i -= 1;
n -= 1;
} else if (mark.offset >= offset) {
mark.offset += length;
}
}
| synchronized void | updateMarksForRemove(int offset, int length)
int n = marks.size();
for (int i = 0; i < n; i++) {
PosRec mark = (PosRec) marks.elementAt(i);
if (mark.unused) {
// this record is no longer used, get rid of it
marks.removeElementAt(i);
i -= 1;
n -= 1;
} else if (mark.offset >= (offset + length)) {
mark.offset -= length;
} else if (mark.offset >= offset) {
mark.offset = offset;
}
}
| protected void | updateUndoPositions(java.util.Vector positions)Resets the location for all the UndoPosRef instances
in positions .
This is meant for internal usage, and is generally not of interest
to subclasses.
for(int counter = positions.size() - 1; counter >= 0; counter--) {
UndoPosRef ref = (UndoPosRef)positions.elementAt(counter);
// Check if the Position is still valid.
if(ref.rec.unused) {
positions.removeElementAt(counter);
}
else
ref.resetLocation();
}
|
|