下面列出了java.text.BreakIterator#current ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void selectWord(int wordPositionInArea) {
if(area.getLength() == 0) {
return;
}
BreakIterator breakIterator = BreakIterator.getWordInstance( getArea().getLocale() );
breakIterator.setText(area.getText());
breakIterator.preceding(wordPositionInArea);
breakIterator.next();
int wordStart = breakIterator.current();
breakIterator.following(wordPositionInArea);
breakIterator.next();
int wordEnd = breakIterator.current();
selectRange(wordStart, wordEnd);
}
/**
* Bug 4068137
*/
public void TestEndBehavior()
{
String testString = "boo.";
BreakIterator wb = BreakIterator.getWordInstance();
wb.setText(testString);
if (wb.first() != 0)
errln("Didn't get break at beginning of string.");
if (wb.next() != 3)
errln("Didn't get break before period in \"boo.\"");
if (wb.current() != 4 && wb.next() != 4)
errln("Didn't get break at end of string.");
}
/**
* Bug 4068137
*/
public void TestEndBehavior()
{
String testString = "boo.";
BreakIterator wb = BreakIterator.getWordInstance();
wb.setText(testString);
if (wb.first() != 0)
errln("Didn't get break at beginning of string.");
if (wb.next() != 3)
errln("Didn't get break before period in \"boo.\"");
if (wb.current() != 4 && wb.next() != 4)
errln("Didn't get break at end of string.");
}
/**
* Bug 4068137
*/
public void TestEndBehavior()
{
String testString = "boo.";
BreakIterator wb = BreakIterator.getWordInstance();
wb.setText(testString);
if (wb.first() != 0)
errln("Didn't get break at beginning of string.");
if (wb.next() != 3)
errln("Didn't get break before period in \"boo.\"");
if (wb.current() != 4 && wb.next() != 4)
errln("Didn't get break at end of string.");
}
/**
* Bug 4068137
*/
public void TestEndBehavior()
{
String testString = "boo.";
BreakIterator wb = BreakIterator.getWordInstance();
wb.setText(testString);
if (wb.first() != 0)
errln("Didn't get break at beginning of string.");
if (wb.next() != 3)
errln("Didn't get break before period in \"boo.\"");
if (wb.current() != 4 && wb.next() != 4)
errln("Didn't get break at end of string.");
}
private void testFirstAndLast(BreakIterator bi, String text, String boundaries) {
String message = "Text: " + text;
int current = bi.current();
assertEquals(message, boundaries.indexOf('^'), current);
assertEquals(message, current, bi.first());
assertEquals(message, current, bi.current());
current = bi.last();
assertEquals(boundaries.lastIndexOf('^'), current);
assertEquals(message, current, bi.current());
}
/**
* Returns a string comprised of spaces and '^' only at the boundaries.
*/
private String readBoundariesToString(BreakIterator bi, String text) {
// init markers to spaces
StringBuilder markers = new StringBuilder();
markers.setLength(text.length() + 1);
for (int k = 0; k < markers.length(); k++) {
markers.setCharAt(k, ' ');
}
bi.setText(text);
for (int boundary = bi.current(); boundary != BreakIterator.DONE; boundary = bi.next()) {
markers.setCharAt(boundary, '^');
}
return markers.toString();
}
/**
* Bug 4068137
*/
public void TestEndBehavior()
{
String testString = "boo.";
BreakIterator wb = BreakIterator.getWordInstance();
wb.setText(testString);
if (wb.first() != 0)
errln("Didn't get break at beginning of string.");
if (wb.next() != 3)
errln("Didn't get break before period in \"boo.\"");
if (wb.current() != 4 && wb.next() != 4)
errln("Didn't get break at end of string.");
}
/**
* Creates a new spell check iterator.
*
* @param document the document containing the specified partition
* @param region the region to spell check
* @param locale the locale to use for spell checking
* @param breakIterator the break-iterator
*/
public SpellCheckIterator(IDocument document, IRegion region, Locale locale, BreakIterator breakIterator) {
fOffset= region.getOffset();
fWordIterator= breakIterator;
fDelimiter= TextUtilities.getDefaultLineDelimiter(document);
String content;
try {
content= document.get(region.getOffset(), region.getLength());
} catch (Exception exception) {
content= ""; //$NON-NLS-1$
}
fContent= content;
fWordIterator.setText(content);
fPredecessor= fWordIterator.first();
fSuccessor= fWordIterator.next();
final BreakIterator iterator= BreakIterator.getSentenceInstance(locale);
iterator.setText(content);
int offset= iterator.current();
while (offset != BreakIterator.DONE) {
fSentenceBreaks.add(new Integer(offset));
offset= iterator.next();
}
}
/**
* @see com.mulgasoft.emacsplus.commands.SexpHandler#getNextPosition(org.eclipse.jface.text.IDocument, java.text.BreakIterator)
*/
@Override
protected int getNextPosition(IDocument document, BreakIterator iter) {
int pos = iter.current();
int result = iter.previous();
if (result != BreakIterator.DONE) {
result = checkUnder(document,iter,result);
result = checkDot(document,pos,result);
}
return result;
}
/**
* @see com.mulgasoft.emacsplus.commands.SexpHandler#getNextPosition(org.eclipse.jface.text.IDocument, java.text.BreakIterator)
*/
@Override
protected int getNextPosition(IDocument document, BreakIterator iter) {
int pos = iter.current();
int result = iter.next();
if (result != BreakIterator.DONE) {
result = checkDot(document,pos,result);
result = checkUnder(document,result);
}
return result;
}
/** Assumes that {@code getArea().getLength != 0} is true and {@link BreakIterator#setText(String)} has been called */
private int calculatePositionViaBreakingForwards(int numOfBreaks, BreakIterator breakIterator, int position) {
breakIterator.following(position);
for (int i = 1; i < numOfBreaks; i++) {
breakIterator.next(numOfBreaks);
}
return breakIterator.current();
}
/** Assumes that {@code getArea().getLength != 0} is true and {@link BreakIterator#setText(String)} has been called */
private int calculatePositionViaBreakingBackwards(int numOfBreaks, BreakIterator breakIterator, int position) {
breakIterator.preceding(position);
for (int i = 1; i < numOfBreaks; i++) {
breakIterator.previous();
}
return breakIterator.current();
}
/**
* Breaks will be at least {@code minLength} apart (to the extent possible),
* while trying to position the match inside the fragment according to {@code fragmentAlignment}.
*/
public static LengthGoalBreakIterator createMinLength(BreakIterator baseIter, int minLength,
float fragmentAlignment) {
return new LengthGoalBreakIterator(baseIter, minLength, fragmentAlignment, true, baseIter.current());
}
/**
* Breaks will be on average {@code targetLength} apart; the closest break to this target (before or after)
* is chosen. The match will be positioned according to {@code fragmentAlignment} as much as possible.
*/
public static LengthGoalBreakIterator createClosestToLength(BreakIterator baseIter, int targetLength,
float fragmentAlignment) {
return new LengthGoalBreakIterator(baseIter, targetLength, fragmentAlignment, false, baseIter.current());
}
/** Asserts that two breakiterators break the text the same way */
public static void assertSameBreaks(CharacterIterator one, CharacterIterator two, BreakIterator expected, BreakIterator actual) {
expected.setText(one);
actual.setText(two);
assertEquals(expected.current(), actual.current());
// next()
int v = expected.current();
while (v != BreakIterator.DONE) {
assertEquals(v = expected.next(), actual.next());
assertEquals(expected.current(), actual.current());
}
// first()
assertEquals(expected.first(), actual.first());
assertEquals(expected.current(), actual.current());
// last()
assertEquals(expected.last(), actual.last());
assertEquals(expected.current(), actual.current());
// previous()
v = expected.current();
while (v != BreakIterator.DONE) {
assertEquals(v = expected.previous(), actual.previous());
assertEquals(expected.current(), actual.current());
}
// following()
for (int i = one.getBeginIndex(); i <= one.getEndIndex(); i++) {
expected.first();
actual.first();
assertEquals(expected.following(i), actual.following(i));
assertEquals(expected.current(), actual.current());
}
// preceding()
for (int i = one.getBeginIndex(); i <= one.getEndIndex(); i++) {
expected.last();
actual.last();
assertEquals(expected.preceding(i), actual.preceding(i));
assertEquals(expected.current(), actual.current());
}
}
@Override // to select words containing underscores
public void selectWord()
{
if ( getLength() == 0 ) return;
CaretSelectionBind<?,?,?> csb = getCaretSelectionBind();
int paragraph = csb.getParagraphIndex();
int position = csb.getColumnPosition();
String paragraphText = getText( paragraph );
BreakIterator breakIterator = BreakIterator.getWordInstance( getLocale() );
breakIterator.setText( paragraphText );
breakIterator.preceding( position );
int start = breakIterator.current();
while ( start > 0 && paragraphText.charAt( start-1 ) == '_' )
{
if ( --start > 0 && ! breakIterator.isBoundary( start-1 ) )
{
breakIterator.preceding( start );
start = breakIterator.current();
}
}
breakIterator.following( position );
int end = breakIterator.current();
int len = paragraphText.length();
while ( end < len && paragraphText.charAt( end ) == '_' )
{
if ( ++end < len && ! breakIterator.isBoundary( end+1 ) )
{
breakIterator.following( end );
end = breakIterator.current();
}
// For some reason single digits aren't picked up so ....
else if ( Character.isDigit( paragraphText.charAt( end ) ) )
{
end++;
}
}
csb.selectRange( paragraph, start, paragraph, end );
}