下面列出了怎么用org.eclipse.jface.text.DefaultPositionUpdater的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Creates a new edition on the document of this factory.
*
* @param offset the offset of the edition at the point when is created.
* @param length the length of the edition (not updated via the position update mechanism)
* @param text the text to be replaced on the document
* @return an <code>Edit</code> reflecting the edition on the document
*/
public Edit createEdit(int offset, int length, String text) throws BadLocationException {
if (!fDocument.containsPositionCategory(fCategory)) {
fDocument.addPositionCategory(fCategory);
fUpdater= new DefaultPositionUpdater(fCategory);
fDocument.addPositionUpdater(fUpdater);
}
Position position= new Position(offset);
try {
fDocument.addPosition(fCategory, position);
} catch (BadPositionCategoryException e) {
Assert.isTrue(false);
}
return new Edit(fDocument, length, text, position);
}
/**
* Returns a document with the given content and the given positions
* registered with the {@link DefaultPositionUpdater}.
*
* @param content the content
* @param positions the positions
* @return the document
* @throws IllegalArgumentException
*/
private static Document createDocument(String content, Position[] positions) throws IllegalArgumentException {
Document doc= new Document(content);
try {
if (positions != null) {
final String POS_CATEGORY= "myCategory"; //$NON-NLS-1$
doc.addPositionCategory(POS_CATEGORY);
doc.addPositionUpdater(new DefaultPositionUpdater(POS_CATEGORY) {
protected boolean notDeleted() {
if (this.fOffset < this.fPosition.offset && (this.fPosition.offset + this.fPosition.length < this.fOffset + this.fLength)) {
this.fPosition.offset= this.fOffset + this.fLength; // deleted positions: set to end of remove
return false;
}
return true;
}
});
for (int i= 0; i < positions.length; i++) {
try {
doc.addPosition(POS_CATEGORY, positions[i]);
} catch (BadLocationException e) {
throw new IllegalArgumentException("Position outside of string. offset: " + positions[i].offset + ", length: " + positions[i].length + ", string size: " + content.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
}
}
}
} catch (BadPositionCategoryException cannotHappen) {
// can not happen: category is correctly set up
}
return doc;
}
private static Document createDocument(String string, Position[] positions) throws IllegalArgumentException {
Document doc= new Document(string);
try {
if (positions != null) {
final String POS_CATEGORY= "myCategory"; //$NON-NLS-1$
doc.addPositionCategory(POS_CATEGORY);
doc.addPositionUpdater(new DefaultPositionUpdater(POS_CATEGORY) {
protected boolean notDeleted() {
int start= this.fOffset;
int end= start + this.fLength;
if (start < this.fPosition.offset && (this.fPosition.offset + this.fPosition.length < end)) {
this.fPosition.offset= end; // deleted positions: set to end of remove
return false;
}
return true;
}
});
for (int i= 0; i < positions.length; i++) {
try {
doc.addPosition(POS_CATEGORY, positions[i]);
} catch (BadLocationException e) {
throw new IllegalArgumentException("Position outside of string. offset: " + positions[i].offset + ", length: " + positions[i].length + ", string size: " + string.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
}
}
}
} catch (BadPositionCategoryException cannotHappen) {
// can not happen: category is correctly set up
}
return doc;
}
/**
* Creates a new partitioner that uses the given scanner and may return partitions of the given legal content types.
*
* @param scanner
* the scanner this partitioner is supposed to use
* @param legalContentTypes
* the legal content types of this partitioner
* @since 2.2
*/
public DocumentPartitioner(IPartitionTokenScanner scanner, String[] legalContentTypes) {
fScanner = scanner;
fLegalContentTypes = TextUtilities.copy(legalContentTypes);
fPositionCategory = CONTENT_TYPES_CATEGORY + hashCode();
fPositionUpdater = new DefaultPositionUpdater(fPositionCategory);
}
/**
* Creates a new partitioner that uses the given scanner and may return
* partitions of the given legal content types.
*
* @param scanner the scanner this partitioner is supposed to use
* @param legalContentTypes the legal content types of this partitioner
*/
public TLAFastPartitioner(IPartitionTokenScanner scanner, String[] legalContentTypes) {
fScanner= (TLAPartitionScanner) scanner;
fLegalContentTypes= TextUtilities.copy(legalContentTypes);
fPositionCategory= CONTENT_TYPES_CATEGORY + hashCode();
fPositionUpdater= new DefaultPositionUpdater(fPositionCategory);
}
/**
* Creates a new partitioner that uses the given scanner and may return
* partitions of the given legal content types.
*
* @param scanner the scanner this partitioner is supposed to use
* @param legalContentTypes the legal content types of this partitioner
*/
public FastPartitioner(IPartitionTokenScanner scanner, String[] legalContentTypes) {
fScanner = scanner;
fLegalContentTypes = TextUtilities.copy(legalContentTypes);
fPositionCategory = CONTENT_TYPES_CATEGORY + hashCode();
fPositionUpdater = new DefaultPositionUpdater(fPositionCategory);
}