Java JTextComponent Word replaceWordAtCaretNice(JTextComponent editor, String strText)

Here you can find the source of replaceWordAtCaretNice(JTextComponent editor, String strText)

Description

replace Word At Caret Nice

License

Apache License

Declaration

public static void replaceWordAtCaretNice(JTextComponent editor, String strText) 

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 void replaceWordAtCaretNice(JTextComponent editor, String strText) {
        String strWordAtCaret = getWordAtCaret(editor);
        if (strWordAtCaret != null && strWordAtCaret.length() > 0
                && Character.isLetterOrDigit(strWordAtCaret.charAt(0))) {
            replaceWordAtCaret(editor, strText);
            return;
        }/*from  w  ww. j  a  v  a2 s.co  m*/

        String strWordBeforeCaret = getPartialWordBeforeCaret(editor);
        if (strWordBeforeCaret != null && strWordBeforeCaret.length() > 0
                && Character.isLetterOrDigit(strWordBeforeCaret.charAt(0))) {
            replaceWordBeforeCaret(editor, strText);
            return;
        }

        editor.replaceSelection(strText == null ? "" : strText);
    }

    public static String getWordAtCaret(JTextComponent editor) {
        try {
            int iCaretPos = editor.getCaretPosition();
            int iStart = getWordStart(editor, iCaretPos);
            int iEnd = getWordEnd(editor, iCaretPos);
            return editor.getText(iStart, iEnd - iStart);
        } catch (BadLocationException e) {
            return getPartialWordBeforeCaret(editor);
        }
    }

    /**
     * @return offset of replaced word
     */
    public static int replaceWordAtCaret(JTextComponent editor, String strText) {
        selectWordAtCaret(editor);
        int selectionStart = editor.getSelectionStart();
        editor.replaceSelection(strText == null ? "" : strText);
        return selectionStart;
    }

    public static String getPartialWordBeforeCaret(JTextComponent editor) {
        return getPartialWordBeforePos(editor, editor.getCaretPosition());
    }

    /**
     * @return offset of replaced word
     */
    public static int replaceWordBeforeCaret(JTextComponent editor, String strText) {
        int iStart = 0;
        int iEnd = 0;
        try {
            iEnd = editor.getCaretPosition();
            iStart = getPreviousWord(editor, iEnd);
        } catch (BadLocationException be) {
            // ignore
        }

        if (iStart < iEnd) {
            editor.select(iStart, iEnd);
        }
        int initialSelectionStart = editor.getSelectionStart();
        editor.replaceSelection(strText == null ? "" : strText);
        return initialSelectionStart;
    }

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

        int iStart = iOffset;
        for (; iStart > 0 && Character.isJavaIdentifierPart(text.charAt(iStart)); iStart--)
            ;
        if (iStart != iOffset) {
            iStart++;
        }
        return iStart;
    }

    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++)
            ;
        if (iEnd == iOffset && !Character.isWhitespace(text.charAt(iEnd))) {
            // the word is a single, non-identifier character
            iEnd++;
        }
        return iEnd;
    }

    public static void selectWordAtCaret(JTextComponent editor) {
        int iStart = 0;
        int iEnd = 0;
        try {
            int iCaretPos = editor.getCaretPosition();
            iStart = getWordStart(editor, iCaretPos);
            iEnd = getWordEnd(editor, iCaretPos);
        } catch (BadLocationException e) {
            try {
                iEnd = editor.getCaretPosition();
                iStart = getPreviousWord(editor, iEnd);
            } catch (BadLocationException be) {
                // ignore
            }
        }

        if (iStart < iEnd) {
            editor.select(iStart, iEnd);
        }
    }

    public static String getPartialWordBeforePos(JTextComponent editor, int iPos) {
        try {
            int iNonWhitespace = findNonWhitespacePositionBefore(editor.getText(), iPos - 1);
            int iStart = getWordStart(editor, iNonWhitespace);
            return editor.getText(iStart, iPos - iStart).trim();
        } catch (BadLocationException e) {
            return "";
        }
    }

    private static int getPreviousWord(JTextComponent editor, int iOffset) throws BadLocationException {
        String text = editor.getText();
        int iStart = getWordStart(editor, iOffset);
        for (iOffset = iStart - 1; iOffset >= 0 && Character.isWhitespace(text.charAt(iOffset)); iOffset--)
            ;
        if (iOffset < 0) {
            return iStart;
        }
        return getWordStart(editor, iOffset);
    }

    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;
    }

    /**
     * @return the first non-whitespace position in the given script before the given position
     */
    public static int findNonWhitespacePositionBefore(String script, int position) {
        if (position > script.length() - 1) {
            return position;
        }

        while (position > 0) {
            if (!Character.isWhitespace(script.charAt(position))) {
                break;
            }
            position--;
        }
        return position;
    }
}

Related

  1. getWordDimensionBeforeCaret(JTextComponent editor)
  2. getWordEnd(JTextComponent c, int offs)
  3. getWordEnd(JTextComponent editor, int iOffset)
  4. getWordStart(JTextComponent c, int offs)
  5. getWordWrappedTextDimension(JTextComponent textComponent, int width)
  6. searchInTxComp(JTextComponent src, String word)
  7. selectWordAtCaret(JTextComponent editor)