FileDocCategorySizeDatePackage
StringContent.javaAPI DocJava SE 5 API14237Fri Aug 26 14:58:16 BST 2005javax.swing.text

StringContent

public final class StringContent extends Object implements Serializable, AbstractDocument$Content
An 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}.

author
Timothy Prinzing
version
1.44 12/19/03

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.

param
initialLength the initial size

	if (initialLength < 1) {
	    initialLength = 1;
	}
	data = new char[initialLength];
	data[0] = '\n";
	count = 1;
    
Methods Summary
public javax.swing.text.PositioncreatePosition(int offset)
Creates a position within the content that will track change as the content is mutated.

param
offset the offset to create a position for >= 0
return
the position
exception
BadLocationException if the specified position is invalid

	// 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 voidgetChars(int where, int len, javax.swing.text.Segment chars)
Retrieves a portion of the content. where + len must be <= length()

param
where the starting position >= 0
param
len the number of characters to retrieve >= 0
param
chars the Segment object to return the characters in
exception
BadLocationException if the specified position is invalid
see
AbstractDocument.Content#getChars

	if (where + len > count) {
	    throw new BadLocationException("Invalid location", count);
	}
	chars.array = data;
	chars.offset = where;
	chars.count = len;
    
protected java.util.VectorgetPositionsInRange(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.

param
v the Vector to use, with a new one created on null
param
offset the starting offset >= 0
param
length the length >= 0
return
the set of instances

	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.StringgetString(int where, int len)
Retrieves a portion of the content. where + len must be <= length().

param
where the starting position >= 0
param
len the length to retrieve >= 0
return
a string representing the content; may be empty
exception
BadLocationException if the specified position is invalid
see
AbstractDocument.Content#getString

	if (where + len > count) {
	    throw new BadLocationException("Invalid range", count);
	}
	return new String(data, where, len);
    
public javax.swing.undo.UndoableEditinsertString(int where, java.lang.String str)
Inserts a string into the content.

param
where the starting position >= 0 && < length()
param
str the non-null string to insert
return
an UndoableEdit object for undoing
exception
BadLocationException if the specified position is invalid
see
AbstractDocument.Content#insertString

	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 intlength()
Returns the length of the content.

return
the length >= 1
see
AbstractDocument.Content#length

	return count;
    
public javax.swing.undo.UndoableEditremove(int where, int nitems)
Removes part of the content. where + nitems must be < length().

param
where the starting position >= 0
param
nitems the number of characters to remove >= 0
return
an UndoableEdit object for undoing
exception
BadLocationException if the specified position is invalid
see
AbstractDocument.Content#remove

	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;
	
    
voidreplace(int offset, int length, char[] replArray, int replOffset, int replLength)
Replaces some of the characters in the array

param
offset offset into the array to start the replace
param
length number of characters to remove
param
replArray replacement array
param
replOffset offset into the replacement array
param
replLength number of character to use from the replacement 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;
    
voidresize(int ncount)

	char[] ndata = new char[ncount];
	System.arraycopy(data, 0, ndata, 0, Math.min(ncount, count));
	data = ndata;
    
synchronized voidupdateMarksForInsert(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 voidupdateMarksForRemove(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 voidupdateUndoPositions(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.

param
positions the positions of the instances

	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();
	}