Java JTextComponent Word getWordEnd(JTextComponent editor, int iOffset)

Here you can find the source of getWordEnd(JTextComponent editor, int iOffset)

Description

get Word End

License

Apache License

Declaration

public static int getWordEnd(JTextComponent editor, int iOffset) throws BadLocationException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.swing.text.BadLocationException;

import javax.swing.text.JTextComponent;

public class Main {
    public static int getWordEnd(JTextComponent editor, int iOffset) throws BadLocationException {
        String text = editor.getText();
        iOffset = maybeAdjustOffsetToNextWord(text, iOffset);

        int iEnd = iOffset;
        for (; iEnd < text.length() && Character.isJavaIdentifierPart(text.charAt(iEnd)); iEnd++)
            ;/*from w  w  w.  j a v  a 2s .c  om*/
        if (iEnd == iOffset && !Character.isWhitespace(text.charAt(iEnd))) {
            // the word is a single, non-identifier character
            iEnd++;
        }
        return iEnd;
    }

    private static int maybeAdjustOffsetToNextWord(String text, int iOffset) throws BadLocationException {
        if (text.length() < iOffset) {
            throw new BadLocationException("Index out of bounds. Offset: " + iOffset + "  Length: " + text.length(),
                    iOffset);
        }

        if (iOffset > 0 && !text.isEmpty()
                && (text.length() == iOffset || Character.isWhitespace(text.charAt(iOffset)))) {
            if (Character.isWhitespace(text.charAt(iOffset - 1))) {
                while (iOffset < text.length()) {
                    if (!Character.isWhitespace(text.charAt(iOffset))) {
                        return iOffset;
                    }
                    iOffset++;
                }
            }
            iOffset--;
        }
        return iOffset;
    }
}

Related

  1. getPreviousWord(JTextComponent editor, int iOffset)
  2. getWordAtCaret(JTextComponent editor)
  3. getWordDimensionAtCaret(JTextComponent editor)
  4. getWordDimensionBeforeCaret(JTextComponent editor)
  5. getWordEnd(JTextComponent c, int offs)
  6. getWordStart(JTextComponent c, int offs)
  7. getWordWrappedTextDimension(JTextComponent textComponent, int width)
  8. replaceWordAtCaretNice(JTextComponent editor, String strText)
  9. searchInTxComp(JTextComponent src, String word)