Java JTextComponent getCurrentTextBlock(JTextComponent textComponent)

Here you can find the source of getCurrentTextBlock(JTextComponent textComponent)

Description

this method will get the current block of text that the caret is within.

License

Open Source License

Parameter

Parameter Description
textComponent a parameter

Declaration

public static String getCurrentTextBlock(JTextComponent textComponent) 

Method Source Code

//package com.java2s;
/*// www .  ja  va 2s .  co m
 * #%L
 * SwingAutoCompletion
 * %%
 * Copyright (C) 2013 - 2014 SwingAutoComplete
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.JTextComponent;

public class Main {
    /**
     * this method will get the current block of text that the caret is within. The block is determined by blank lines on top and bottom or beginning or ending of string.
     *
     * @param textComponent
     * @return
     */
    public static String getCurrentTextBlock(JTextComponent textComponent) {
        // Compile the pattern
        String patternStr = "(?m)^\\s*?$";
        Pattern pattern = Pattern.compile(patternStr);
        String text = textComponent.getText();
        Matcher matcher = pattern.matcher(text);
        int caretPosition = textComponent.getCaretPosition();

        // Read the paragraphs
        int prevBlankLineIndex = 0;
        while (matcher.find()) {
            if (matcher.start() >= caretPosition)
                return textComponent.getText().substring(prevBlankLineIndex, matcher.start());
            prevBlankLineIndex = matcher.start();
        }
        return text.substring(prevBlankLineIndex, text.length());
    }
}

Related

  1. enableJTextField(JTextComponent component, boolean enable, Color color)
  2. ensureCustomBackgroundStored(JTextComponent comp)
  3. ensureCustomBackgroundStored(JTextComponent comp)
  4. fillWith(String value, JTextComponent... fields)
  5. getAdjustedClickCount(JTextComponent comp, MouseEvent e)
  6. getNumberOfLines(final JTextComponent component)
  7. getParagraphElement(JTextComponent c, int offs)
  8. getPositionBelow(JTextComponent c, int offs, int x)
  9. getPreferredScrollableViewportSize(javax.swing.text.JTextComponent t, Dimension ans)