Java JTextComponent Word getWordEnd(JTextComponent c, int offs)

Here you can find the source of getWordEnd(JTextComponent c, int offs)

Description

Determines the end of a word for the given model location.

License

Open Source License

Parameter

Parameter Description
c a parameter
offs a parameter

Exception

Parameter Description
BadLocationException an exception

Declaration

public static int getWordEnd(JTextComponent c, int offs) throws BadLocationException 

Method Source Code

//package com.java2s;
/**************************************************************************
 OmegaT - Computer Assisted Translation (CAT) tool 
  with fuzzy matching, translation memory, keyword search, 
  glossaries, and translation leveraging into updated projects.
    /*from  ww  w.  ja v a2  s .  c  om*/
 Copyright (C) 2008 Alex Buloichik
       2012 Didier Briel
       2015 Aaron Madlon-Kay
       Home page: http://www.omegat.org/
       Support center: http://groups.yahoo.com/group/OmegaT/
    
 This file is part of OmegaT.
    
 OmegaT is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 OmegaT is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************/

import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.Utilities;

public class Main {
    /**
     * Determines the end of a word for the given model location. This method
     * skips direction char.
     * 
     * TODO: change to use document's locale
     * 
     * @param c
     * @param offs
     * @return
     * @throws BadLocationException
     */
    public static int getWordEnd(JTextComponent c, int offs) throws BadLocationException {
        int result = Utilities.getWordEnd(c, offs);
        if (result > 0) {
            char ch = c.getDocument().getText(result - 1, 1).charAt(0);
            if (isDirectionChar(ch)) {
                result--;
            }
        }
        return result;
    }

    /**
     * Check if char is direction char(u202A,u202B,u202C).
     * 
     * @param ch
     *            char to check
     * @return true if it's direction char
     */
    private static boolean isDirectionChar(final char ch) {
        return ch == '\u202A' || ch == '\u202B' || ch == '\u202C' || ch == '\u200E' || ch == '\u200F';
    }
}

Related

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