List of usage examples for javax.swing.text JTextComponent getSelectionEnd
@Transient public int getSelectionEnd()
From source file:Main.java
/** * Cuts via the fake clipboard.//from w ww . ja va2 s .co 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:Main.java
public void actionPerformed(ActionEvent e) { JTextComponent comp = getTextComponent(e); if (comp == null) return;//from w w w. ja va 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;// w w w . ja v a 2 s . com 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;//from ww 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;//from ww w . j ava2 s . c o m lastShownCompletion = 0; lastCaretPosition = -1; toSetIn = null; } }
From source file:net.sf.jabref.gui.autocompleter.AutoCompleteListener.java
@Override public void keyPressed(KeyEvent e) { if ((toSetIn != null) && (e.getKeyCode() == KeyEvent.VK_ENTER)) { JTextComponent comp = (JTextComponent) e.getSource(); // replace typed characters by characters from completion lastBeginning = lastCompletions.get(lastShownCompletion); int end = comp.getSelectionEnd(); comp.select(end, end);// w w w . ja va 2 s . com toSetIn = null; if (consumeEnterKey) { e.consume(); } } // Cycle through alternative completions when user presses PGUP/PGDN: else if ((e.getKeyCode() == KeyEvent.VK_PAGE_DOWN) && (toSetIn != null)) { cycle((JTextComponent) e.getSource(), 1); e.consume(); } else if ((e.getKeyCode() == KeyEvent.VK_PAGE_UP) && (toSetIn != null)) { cycle((JTextComponent) e.getSource(), -1); e.consume(); } // else if ((e.getKeyCode() == KeyEvent.VK_BACK_SPACE)) { // StringBuffer currentword = getCurrentWord((JTextComponent) e.getSource()); // // delete last char to obey semantics of back space // currentword.deleteCharAt(currentword.length()-1); // doCompletion(currentword, e); // } else if (e.getKeyChar() == KeyEvent.CHAR_UNDEFINED) { if (e.getKeyCode() == KeyEvent.VK_SHIFT) { // shift is OK, everything else leads to a reset LOGGER.debug("Special case: shift pressed. No action."); } else { resetAutoCompletion(); } } else { LOGGER.debug("Special case: defined character, but not caught above"); } }
From source file:net.sf.jabref.gui.AutoCompleteListener.java
@Override public void keyPressed(KeyEvent e) { if ((toSetIn != null) && (e.getKeyCode() == KeyEvent.VK_ENTER)) { JTextComponent comp = (JTextComponent) e.getSource(); // replace typed characters by characters from completion lastBeginning = lastCompletions[lastShownCompletion]; int end = comp.getSelectionEnd(); comp.select(end, end);/*from w ww . j a v a 2 s. c om*/ toSetIn = null; if (consumeEnterKey) { e.consume(); } } // Cycle through alternative completions when user presses PGUP/PGDN: else if ((e.getKeyCode() == KeyEvent.VK_PAGE_DOWN) && (toSetIn != null)) { cycle((JTextComponent) e.getSource(), 1); e.consume(); } else if ((e.getKeyCode() == KeyEvent.VK_PAGE_UP) && (toSetIn != null)) { cycle((JTextComponent) e.getSource(), -1); e.consume(); } // else if ((e.getKeyCode() == KeyEvent.VK_BACK_SPACE)) { // StringBuffer currentword = getCurrentWord((JTextComponent) e.getSource()); // // delete last char to obey semantics of back space // currentword.deleteCharAt(currentword.length()-1); // doCompletion(currentword, e); // } else if (e.getKeyChar() == KeyEvent.CHAR_UNDEFINED) { if (e.getKeyCode() != KeyEvent.VK_SHIFT) { // shift is OK, everyhting else leads to a reset resetAutoCompletion(); } else { LOGGER.debug("Special case: shift pressed. No action."); } } else { LOGGER.debug("Special case: defined character, but not caught above"); } }
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 .jav a 2 s.c o 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.autocompleter.AutoCompleteListener.java
private void cycle(JTextComponent comp, int increment) { assert (lastCompletions != null); assert (!lastCompletions.isEmpty()); lastShownCompletion += increment;//from w w w. ja v a 2 s .c om 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;/* w w w . j a v a2s . c om*/ 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+"'"); }