Example usage for javax.swing.text JTextComponent getSelectionStart

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

Introduction

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

Prototype

@Transient
public int getSelectionStart() 

Source Link

Document

Returns the selected text's start position.

Usage

From source file:Main.java

/**
 * Cuts via the fake clipboard.// ww  w .ja  va  2  s  .com
 */

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:Main.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;/*  w w w  . j ava  2 s  .c om*/
    Document doc = comp.getDocument();
    int start = comp.getSelectionStart();
    int end = comp.getSelectionEnd();
    try {
        int left = Utilities.getWordStart(comp, start);
        int right = Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right - left);
        doc.remove(left, right - left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start);
        comp.setSelectionEnd(end);
    } catch (Exception ble) {
        return;
    }
}

From source file:KeymapExample.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;//from w  ww.  j  a v a2 s . c  o  m
    Document doc = comp.getDocument();
    int start = comp.getSelectionStart();
    int end = comp.getSelectionEnd();
    try {
        int left = javax.swing.text.Utilities.getWordStart(comp, start);
        int right = javax.swing.text.Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right - left);
        doc.remove(left, right - left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start);
        comp.setSelectionEnd(end);
    } catch (BadLocationException ble) {
        return;
    }
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;//  w  w  w  . j  a va 2 s  .c o  m
    Document doc = comp.getDocument();
    int start = comp.getSelectionStart();
    int end = comp.getSelectionEnd();
    try {
        int left = Utilities.getWordStart(comp, start);
        int right = Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right - left);
        doc.remove(left, right - left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start);
        comp.setSelectionEnd(end);
    } catch (BadLocationException ble) {
        return;
    }
}

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

public void clearCurrentSuggestion(JTextComponent comp) {
    if (toSetIn != null) {
        int selStart = comp.getSelectionStart();
        String text = comp.getText();
        comp.setText(text.substring(0, selStart) + text.substring(comp.getSelectionEnd()));
        comp.setCaretPosition(selStart);
        lastCompletions = null;//www . j a  v  a 2  s .c om
        lastShownCompletion = 0;
        lastCaretPosition = -1;
        toSetIn = null;
    }
}

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

@Override
public void actionPerformed(ActionEvent ev) {
    try {//from w w w .  ja  v a2  s. c  o  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:com.hexidec.ekit.component.RelativeImageView.java

/**
  * Determines whether the image is selected, and if it's the only thing selected.
  * @return  0 if not selected, 1 if selected, 2 if exclusively selected.
  * "Exclusive" selection is only returned when editable.
  *///from  ww  w  . java2s  .co m
protected int getSelectionState() {
    int p0 = fElement.getStartOffset();
    int p1 = fElement.getEndOffset();
    if (fContainer instanceof JTextComponent) {
        JTextComponent textComp = (JTextComponent) fContainer;
        int start = textComp.getSelectionStart();
        int end = textComp.getSelectionEnd();
        if ((start <= p0) && (end >= p1)) {
            if ((start == p0) && (end == p1) && isEditable()) {
                return 2;
            } else {
                return 1;
            }
        }
    }
    return 0;
}

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;/*  www. j  av  a2s .  com*/
                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:net.sf.jabref.gui.autocompleter.AutoCompleteListener.java

private void cycle(JTextComponent comp, int increment) {
    assert (lastCompletions != null);
    assert (!lastCompletions.isEmpty());
    lastShownCompletion += increment;/*ww  w.j  a va2  s . co  m*/
    if (lastShownCompletion >= lastCompletions.size()) {
        lastShownCompletion = 0;
    } else if (lastShownCompletion < 0) {
        lastShownCompletion = lastCompletions.size() - 1;
    }
    String sno = lastCompletions.get(lastShownCompletion);
    toSetIn = sno.substring(lastBeginning.length() - 1);

    StringBuilder alltext = new StringBuilder(comp.getText());

    int oldSelectionStart = comp.getSelectionStart();
    int oldSelectionEnd = comp.getSelectionEnd();

    // replace prefix with new prefix
    int startPos = comp.getSelectionStart() - lastBeginning.length();
    alltext.delete(startPos, oldSelectionStart);
    alltext.insert(startPos, sno.subSequence(0, lastBeginning.length()));

    // replace suffix with new suffix
    alltext.delete(oldSelectionStart, oldSelectionEnd);
    //int cp = oldSelectionEnd - deletedChars;
    alltext.insert(oldSelectionStart, toSetIn.substring(1));

    LOGGER.debug(alltext.toString());
    comp.setText(alltext.toString());
    //comp.setCaretPosition(cp+toSetIn.length()-1);
    comp.select(oldSelectionStart, (oldSelectionStart + toSetIn.length()) - 1);
    lastCaretPosition = comp.getCaretPosition();
    LOGGER.debug("ToSetIn: '" + toSetIn + "'");
}

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

private void cycle(JTextComponent comp, int increment) {
    assert (lastCompletions != null);
    assert (lastCompletions.length > 0);
    lastShownCompletion += increment;//from  w  w w  .j ava2s .  co  m
    if (lastShownCompletion >= lastCompletions.length) {
        lastShownCompletion = 0;
    } else if (lastShownCompletion < 0) {
        lastShownCompletion = lastCompletions.length - 1;
    }
    String sno = lastCompletions[lastShownCompletion];
    toSetIn = sno.substring(lastBeginning.length() - 1);

    StringBuilder alltext = new StringBuilder(comp.getText());

    int oldSelectionStart = comp.getSelectionStart();
    int oldSelectionEnd = comp.getSelectionEnd();

    // replace prefix with new prefix
    int startPos = comp.getSelectionStart() - lastBeginning.length();
    alltext.delete(startPos, oldSelectionStart);
    alltext.insert(startPos, sno.subSequence(0, lastBeginning.length()));

    // replace suffix with new suffix
    alltext.delete(oldSelectionStart, oldSelectionEnd);
    //int cp = oldSelectionEnd - deletedChars;
    alltext.insert(oldSelectionStart, toSetIn.substring(1));

    //Util.pr(alltext.toString());
    comp.setText(alltext.toString());
    //comp.setCaretPosition(cp+toSetIn.length()-1);
    comp.select(oldSelectionStart, (oldSelectionStart + toSetIn.length()) - 1);
    lastCaretPosition = comp.getCaretPosition();
    //System.out.println("ToSetIn: '"+toSetIn+"'");
}