List of usage examples for javax.swing.text JTextComponent select
public void select(int selectionStart, int selectionEnd)
From source file:net.sf.jabref.gui.AutoCompleteListener.java
/** * Start a new completion attempt/*from w w w. ja v a2 s . com*/ * (instead of treating a continuation of an existing word or an interrupt of the current word) */ private void startCompletion(StringBuffer currentword, KeyEvent e) { JTextComponent comp = (JTextComponent) e.getSource(); String[] completed = findCompletions(currentword.toString(), comp); String prefix = completer.getPrefix(); String cWord = (prefix != null) && (!prefix.isEmpty()) ? currentword.toString().substring(prefix.length()) : currentword.toString(); LOGGER.debug("StartCompletion currentword: >" + currentword + "'<' prefix: >" + prefix + "'<' cword: >" + cWord + '<'); int no = 0; // We use the first word in the array of completions. if ((completed != null) && (completed.length > 0)) { lastShownCompletion = 0; lastCompletions = completed; String sno = completed[no]; // these two lines obey the user's input //toSetIn = Character.toString(ch); //toSetIn = toSetIn.concat(sno.substring(cWord.length())); // BUT we obey the completion toSetIn = sno.substring(cWord.length() - 1); LOGGER.debug("toSetIn: >" + toSetIn + '<'); StringBuilder alltext = new StringBuilder(comp.getText()); int cp = comp.getCaretPosition(); alltext.insert(cp, toSetIn); comp.setText(alltext.toString()); comp.setCaretPosition(cp); comp.select(cp + 1, (cp + 1 + sno.length()) - cWord.length()); e.consume(); lastCaretPosition = comp.getCaretPosition(); char ch = e.getKeyChar(); LOGGER.debug("Appending >" + ch + '<'); if (cWord.length() <= 1) { lastBeginning = Character.toString(ch); } else { lastBeginning = cWord.substring(0, cWord.length() - 1).concat(Character.toString(ch)); } } }
From source file:com.hexidec.ekit.EkitCore.java
/** * Method for finding (and optionally replacing) a string in the text *///from w w w.j a va 2 s . c om private int findText(String findTerm, String replaceTerm, boolean bCaseSenstive, int iOffset) { JTextComponent jtpFindSource; if (sourceWindowActive || jtpSource.hasFocus()) { jtpFindSource = (JTextComponent) jtpSource; } else { jtpFindSource = (JTextComponent) jtpMain; } int searchPlace = -1; try { Document baseDocument = jtpFindSource.getDocument(); searchPlace = (bCaseSenstive ? baseDocument.getText(0, baseDocument.getLength()).indexOf(findTerm, iOffset) : baseDocument.getText(0, baseDocument.getLength()).toLowerCase() .indexOf(findTerm.toLowerCase(), iOffset)); if (searchPlace > -1) { if (replaceTerm != null) { AttributeSet attribs = null; if (baseDocument instanceof HTMLDocument) { Element element = ((HTMLDocument) baseDocument).getCharacterElement(searchPlace); attribs = element.getAttributes(); } baseDocument.remove(searchPlace, findTerm.length()); baseDocument.insertString(searchPlace, replaceTerm, attribs); jtpFindSource.setCaretPosition(searchPlace + replaceTerm.length()); jtpFindSource.requestFocus(); jtpFindSource.select(searchPlace, searchPlace + replaceTerm.length()); } else { jtpFindSource.setCaretPosition(searchPlace + findTerm.length()); jtpFindSource.requestFocus(); jtpFindSource.select(searchPlace, searchPlace + findTerm.length()); } } } catch (BadLocationException ble) { logException("BadLocationException in actionPerformed method", ble); new SimpleInfoDialog(this.getWindow(), Translatrix.getTranslationString("Error"), true, Translatrix.getTranslationString("ErrorBadLocationException"), SimpleInfoDialog.ERROR); } return searchPlace; }
From source file:org.parosproxy.paros.view.FindDialog.java
private void find() { JTextComponent txtComp = lastInvoker; if (txtComp == null) { JFrame parent = (JFrame) (this.getParent()); Component c = parent.getMostRecentFocusOwner(); if (c instanceof JTextComponent) { txtComp = (JTextComponent) c; }//w w w . j av a 2 s .c om } // ZAP: Check if a JTextComponent was really found. if (txtComp == null) { return; } try { String findText = txtFind.getText().toLowerCase(); String txt = txtComp.getText().toLowerCase(); int startPos = txt.indexOf(findText, txtComp.getCaretPosition()); // Enable Wrap Search if (startPos <= 0) { txtComp.setCaretPosition(0); startPos = txt.indexOf(findText, txtComp.getCaretPosition()); } int length = findText.length(); if (startPos > -1) { txtComp.select(startPos, startPos + length); txtComp.requestFocusInWindow(); txtFind.requestFocusInWindow(); } else { Toolkit.getDefaultToolkit().beep(); } } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } }
From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxUtil.java
/** * Return the lines that span the selection (split as an array of Strings) * if there is no selection then current line is returned. * //from ww w. j ava 2s . c o m * Note that the strings returned will not contain the terminating line feeds. * * The text component will then have the full lines set as selection. * * @param target * @return String[] of lines spanning selection / or Dot */ public static String[] getSelectedLines(JTextComponent target) { String[] lines = null; try { PlainDocument document = (PlainDocument) target.getDocument(); int start = document.getParagraphElement(target.getSelectionStart()).getStartOffset(); int end; if (target.getSelectionStart() == target.getSelectionEnd()) { end = document.getParagraphElement(target.getSelectionEnd()).getEndOffset(); } else { // if more than one line is selected, we need to subtract one from the end // so that we do not select the line with the caret and no selection in it end = document.getParagraphElement(target.getSelectionEnd() - 1).getEndOffset(); } target.select(start, end); lines = document.getText(start, end - start).split("\n"); target.select(start, end); } catch (BadLocationException e) { LOG.error(e.getMessage(), e); lines = EMPTY_STRING_ARRAY; } return lines; }