Example usage for javax.swing.text JTextComponent getSelectedText

List of usage examples for javax.swing.text JTextComponent getSelectedText

Introduction

In this page you can find the example usage for javax.swing.text JTextComponent getSelectedText.

Prototype

@BeanProperty(bound = false)
public String getSelectedText() 

Source Link

Document

Returns the selected text contained in this TextComponent.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JTextComponent c = new JTextArea();

    // Get text inside selection
    c.getSelectedText();
}

From source file:Main.java

/**
 * Cuts via the fake clipboard.//from   w  ww  .j  a  v a2  s  . c  o m
 */

static void fakeCut(JTextComponent tc) {
    String selection = tc.getSelectedText();
    if ((selection != null) && (selection.length() > 0)) {
        clipboard = selection;

        String text = tc.getText();
        int selectionStart = tc.getSelectionStart();
        int selectionEnd = tc.getSelectionEnd();

        tc.setText(text.substring(0, selectionStart) + text.substring(selectionEnd));
        tc.setCaretPosition(selectionStart);
    }
}

From source file:net.sf.jabref.gui.keyboard.EmacsKeyBindings.java

private static void doCopyOrCut(JTextComponent jtc, boolean copy) {
    if (jtc != null) {
        int caretPosition = jtc.getCaretPosition();
        String text = jtc.getSelectedText();
        if (text != null) {
            // user has manually marked a text without using CTRL+W
            // we obey that selection and copy it.
        } else if (SetMarkCommandAction.isMarked(jtc)) {
            int beginPos = caretPosition;
            int endPos = SetMarkCommandAction.getCaretPosition();
            if (beginPos > endPos) {
                int tmp = endPos;
                endPos = beginPos;//from w  w w .  j av  a2  s. co  m
                beginPos = tmp;
            }
            jtc.select(beginPos, endPos);
            SetMarkCommandAction.reset();
        }
        text = jtc.getSelectedText();
        if (text == null) {
            jtc.getToolkit().beep();
        } else {
            if (copy) {
                jtc.copy();
                // clear the selection
                jtc.select(caretPosition, caretPosition);
            } else {
                int newCaretPos = jtc.getSelectionStart();
                jtc.cut();
                // put the cursor to the beginning of the text to cut
                jtc.setCaretPosition(newCaretPos);
            }
            KillRing.getInstance().add(text);
        }
    }
}

From source file:com.junichi11.netbeans.php.enhancements.ui.actions.ConvertToAction.java

@Override
public void actionPerformed(ActionEvent ev) {
    try {/* ww  w.  j  a  v  a2  s.co m*/
        JTextComponent editor = EditorRegistry.lastFocusedComponent();
        if (editor == null) {
            return;
        }
        final StyledDocument document = context.openDocument();
        if (editor.getDocument() != document) {
            return;
        }

        final String selectedText = editor.getSelectedText();
        if (selectedText == null || selectedText.isEmpty()) {
            return;
        }

        final String convertedString = convert(selectedText);
        if (selectedText.equals(convertedString)) {
            return;
        }

        final int selectionStartPosition = editor.getSelectionStart();
        NbDocument.runAtomic(document, new Runnable() {

            @Override
            public void run() {
                try {
                    document.remove(selectionStartPosition, selectedText.length());
                    document.insertString(selectionStartPosition, convertedString, null);
                } catch (BadLocationException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        });
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:net.sf.jabref.gui.AutoCompleteListener.java

/**
 * If user cancels autocompletion by/*from   w  w  w.  jav a  2 s.  co  m*/
 *   a) entering another letter than the completed word (and there is no other auto completion)
 *   b) space
 * the casing of the letters has to be kept
 * 
 * Global variable "lastBeginning" keeps track of typed letters.
 * We rely on this variable to reconstruct the text 
 * 
 * @param wordSeperatorTyped indicates whether the user has typed a white space character or a
 */
private void setUnmodifiedTypedLetters(JTextComponent comp, boolean lastBeginningContainsTypedCharacter,
        boolean wordSeperatorTyped) {
    if (lastBeginning == null) {
        LOGGER.debug("No last beginning found");
        // There was no previous input (if the user typed a word, where no autocompletion is available)
        // Thus, there is nothing to replace
        return;
    }
    LOGGER.debug("lastBeginning: >" + lastBeginning + '<');
    if (comp.getSelectedText() == null) {
        // if there is no selection
        // the user has typed the complete word, but possibly with a different casing
        // we need a replacement
        if (wordSeperatorTyped) {
            LOGGER.debug("Replacing complete word");
        } else {
            // if user did not press a white space character (space, ...),
            // then we do not do anything
            return;
        }
    } else {
        LOGGER.debug("Selected text " + comp.getSelectedText() + " will be removed");
        // remove completion suggestion
        comp.replaceSelection("");
    }

    lastCaretPosition = comp.getCaretPosition();

    int endIndex = lastCaretPosition - lastBeginning.length();
    if (lastBeginningContainsTypedCharacter) {
        // the current letter is NOT contained in comp.getText(), but in lastBeginning
        // thus lastBeginning.length() is one too large
        endIndex++;
    }
    String text = comp.getText();
    comp.setText(text.substring(0, endIndex).concat(lastBeginning).concat(text.substring(lastCaretPosition)));
    if (lastBeginningContainsTypedCharacter) {
        // the current letter is NOT contained in comp.getText()
        // Thus, cursor position also did not get updated
        lastCaretPosition++;
    }
    comp.setCaretPosition(lastCaretPosition);
    lastBeginning = null;
}

From source file:net.java.sip.communicator.impl.gui.main.chat.ChatPanel.java

/**
 * Copies either the selected write area content or the selected
 * conversation panel content to the clipboard.
 *///www  . j  a va2 s  .  c  o  m
public void copy() {
    JTextComponent textPane = this.conversationPanel.getChatTextPane();

    if (textPane.getSelectedText() == null)
        textPane = this.writeMessagePanel.getEditorPane();

    textPane.copy();
}

From source file:org.executequery.gui.text.TextUtilities.java

public static void changeSelectionToCamelCase(JTextComponent textComponent) {

    String selectedText = textComponent.getSelectedText();
    if (StringUtils.isBlank(selectedText)) {

        return;/*from   ww w.  j  a  v a2s  . c om*/
    }

    String breakChars = "-_ \t";

    boolean nextIsUpper = false;
    StringBuilder sb = new StringBuilder();
    char[] chars = selectedText.toCharArray();
    for (int i = 0; i < chars.length; i++) {

        char _char = chars[i];
        if (breakChars.indexOf(_char) != -1) {

            if (i > 0) {

                nextIsUpper = true;
            }

        } else {

            if (nextIsUpper) {

                sb.append(Character.toUpperCase(_char));
                nextIsUpper = false;

            } else {

                sb.append(Character.toLowerCase(_char));
            }
        }
    }

    textComponent.replaceSelection(sb.toString());
}

From source file:org.executequery.gui.text.TextUtilities.java

public static void changeSelectionToUnderscore(JTextComponent textComponent) {

    String selectedText = textComponent.getSelectedText();
    if (StringUtils.isBlank(selectedText)) {

        return;//from  www. j a v a 2  s  . co  m
    }

    String breakChars = "-_ \t";

    boolean lastCharWasUnderscore = false;
    StringBuilder sb = new StringBuilder();
    char[] chars = selectedText.toCharArray();
    for (int i = 0; i < chars.length; i++) {

        char _char = chars[i];
        if (breakChars.indexOf(_char) != -1) {

            sb.append("_");
            lastCharWasUnderscore = true;

        } else if (Character.isUpperCase(_char)) {

            if (!lastCharWasUnderscore) {

                sb.append("_");
            }
            sb.append(_char);
            lastCharWasUnderscore = false;

        } else {

            sb.append(_char);
            lastCharWasUnderscore = false;
        }

    }

    textComponent.replaceSelection(sb.toString().toLowerCase());
}

From source file:org.executequery.gui.text.TextUtilities.java

public static void changeSelectionCase(JTextComponent textComponent, boolean upper) {

    String selectedText = textComponent.getSelectedText();
    if (StringUtils.isBlank(selectedText)) {

        return;//from w  ww  . j a  v  a2  s .  c o m
    }

    if (upper) {
        selectedText = selectedText.toUpperCase();

    } else {

        selectedText = selectedText.toLowerCase();
    }
    textComponent.replaceSelection(selectedText);

}