Here you can find the source of getNextWord(JTextComponent c, int offs)
Parameter | Description |
---|---|
c | the editor |
offs | the offset in the document >= 0 |
public static final int getNextWord(JTextComponent c, int offs) throws BadLocationException
//package com.java2s; /*/*w ww. jav a 2 s . co m*/ * @(#)Utilities.java 1.38 01/12/03 * * Copyright 2002 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ import javax.swing.text.*; import java.text.*; public class Main { /** * Determines the start of the next word for the given location. * Uses BreakIterator.getWordInstance() to actually get the words. * * @param c the editor * @param offs the offset in the document >= 0 * @return the location in the model of the word start >= 0 * @exception BadLocationException if the offset is out of range */ public static final int getNextWord(JTextComponent c, int offs) throws BadLocationException { int nextWord; Element line = getParagraphElement(c, offs); for (nextWord = getNextWordInParagraph(c, line, offs, false); nextWord == BreakIterator.DONE; nextWord = getNextWordInParagraph(c, line, offs, true)) { // didn't find in this line, try the next line offs = line.getEndOffset(); line = getParagraphElement(c, offs); } return nextWord; } /** * Determines the element to use for a paragraph/line. * * @param c the editor * @param offs the starting offset in the document >= 0 * @return the element */ public static final Element getParagraphElement(JTextComponent c, int offs) { Document doc = c.getDocument(); if (doc instanceof StyledDocument) { return ((StyledDocument) doc).getParagraphElement(offs); } Element map = doc.getDefaultRootElement(); int index = map.getElementIndex(offs); Element paragraph = map.getElement(index); if ((offs >= paragraph.getStartOffset()) && (offs < paragraph.getEndOffset())) { return paragraph; } return null; } /** * Finds the next word in the given elements text. The first * parameter allows searching multiple paragraphs where even * the first offset is desired. * Returns the offset of the next word, or BreakIterator.DONE * if there are no more words in the element. */ static int getNextWordInParagraph(JTextComponent c, Element line, int offs, boolean first) throws BadLocationException { if (line == null) { throw new BadLocationException("No more words", offs); } Document doc = line.getDocument(); int lineStart = line.getStartOffset(); int lineEnd = Math.min(line.getEndOffset(), doc.getLength()); if ((offs >= lineEnd) || (offs < lineStart)) { throw new BadLocationException("No more words", offs); } String s = doc.getText(lineStart, lineEnd - lineStart); BreakIterator words = BreakIterator.getWordInstance(c.getLocale()); words.setText(s); if ((first && (words.first() == (offs - lineStart))) && (!Character.isWhitespace(s.charAt(words.first())))) { return offs; } int wordPosition = words.following(offs - lineStart); if ((wordPosition == BreakIterator.DONE) || (wordPosition >= s.length())) { // there are no more words on this line. return BreakIterator.DONE; } // if we haven't shot past the end... check to // see if the current boundary represents whitespace. // if so, we need to try again char ch = s.charAt(wordPosition); if (!Character.isWhitespace(ch)) { return lineStart + wordPosition; } // it was whitespace, try again. The assumption // is that it must be a word start if the last // one had whitespace following it. wordPosition = words.next(); if (wordPosition != BreakIterator.DONE) { offs = lineStart + wordPosition; if (offs != lineEnd) { return offs; } } return BreakIterator.DONE; } }