Java JTextComponent Word getNextWordInParagraph(JTextComponent c, Element line, int offs, boolean first)

Here you can find the source of getNextWordInParagraph(JTextComponent c, Element line, int offs, boolean first)

Description

Finds the next word in the given elements text.

License

Open Source License

Declaration

static int getNextWordInParagraph(JTextComponent c, Element line, int offs, boolean first)
        throws BadLocationException 

Method Source Code

//package com.java2s;
/*/*from ww w. j av a2s  .  c  o m*/
 * @(#)Utilities.java   1.40 03/01/23
 *
 * Copyright 2003 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 {
    /**
     * 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;
    }
}

Related

  1. getNextWord(JTextComponent c, int offs)
  2. getPartialWordBeforePos(JTextComponent editor, int iPos)
  3. getPreviousWord(JTextComponent editor, int iOffset)
  4. getWordAtCaret(JTextComponent editor)
  5. getWordDimensionAtCaret(JTextComponent editor)