Java Code Examples for javax.swing.text.Caret#isSelectionVisible()
The following examples show how to use
javax.swing.text.Caret#isSelectionVisible() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EditorFindSupport.java From netbeans with Apache License 2.0 | 4 votes |
boolean replaceImpl(Map<String, Object> props, boolean oppositeDir, JTextComponent c) throws BadLocationException { props = getValidFindProperties(props); boolean back = Boolean.TRUE.equals(props.get(FIND_BACKWARD_SEARCH)); if (oppositeDir) { back = !back; } boolean blockSearch = Boolean.TRUE.equals(props.get(FIND_BLOCK_SEARCH)); Position blockSearchStartPos = (Position) props.get(FIND_BLOCK_SEARCH_START); int blockSearchStartOffset = (blockSearchStartPos != null) ? blockSearchStartPos.getOffset() : -1; if (c != null) { String s = (String)props.get(FIND_REPLACE_WITH); Caret caret = c.getCaret(); if (caret.isSelectionVisible() && caret.getDot() != caret.getMark()){ Object dp = props.get(FIND_BACKWARD_SEARCH); boolean direction = (dp != null) ? ((Boolean)dp).booleanValue() : false; int dotPos = (oppositeDir ^ direction ? c.getSelectionEnd() : c.getSelectionStart()); c.setCaretPosition(dotPos); } FindReplaceResult result = findReplaceImpl(s, props, oppositeDir, c); if (result!=null){ s = result.getReplacedString(); } else { return false; } Document doc = c.getDocument(); int startOffset = c.getSelectionStart(); int len = c.getSelectionEnd() - startOffset; DocUtils.atomicLock(doc); try { if (len > 0) { doc.remove(startOffset, len); } if (s != null && s.length() > 0) { try { NavigationHistory.getEdits().markWaypoint(c, startOffset, false, true); } catch (BadLocationException e) { LOG.log(Level.WARNING, "Can't add position to the history of edits.", e); //NOI18N } doc.insertString(startOffset, s, null); if (startOffset == blockSearchStartOffset) { // Replaced at begining of block blockSearchStartPos = doc.createPosition(startOffset); props.put(EditorFindSupport.FIND_BLOCK_SEARCH_START, blockSearchStartPos); } } } finally { DocUtils.atomicUnlock(doc); if (blockSearch){ setBlockSearchHighlight(blockSearchStartOffset, getBlockEndOffset()); } } // adjust caret pos after replace operation int adjustedCaretPos = (back || s == null) ? startOffset : startOffset + s.length(); caret.setDot(adjustedCaretPos); } return true; }
Example 2
Source File: XMLKit.java From netbeans with Apache License 2.0 | 4 votes |
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target == null) return; if (!target.isEditable() || !target.isEnabled()) { problem(null); return; } Caret caret = target.getCaret(); BaseDocument doc = (BaseDocument)target.getDocument(); try { if (caret.isSelectionVisible()) { int startPos = Utilities.getRowStart(doc, target.getSelectionStart()); int endPos = target.getSelectionEnd(); doc.atomicLock(); try { if (endPos > 0 && Utilities.getRowStart(doc, endPos) == endPos) { endPos--; } int pos = startPos; int lineCnt = Utilities.getRowCount(doc, startPos, endPos); for (;lineCnt > 0; lineCnt--) { doc.insertString(pos, commentStartString, null); doc.insertString(Utilities.getRowEnd(doc,pos), commentEndString, null); pos = Utilities.getRowStart(doc, pos, +1); } } finally { doc.atomicUnlock(); } } else { // selection not visible doc.insertString(Utilities.getRowStart(doc, target.getSelectionStart()), commentStartString, null); doc.insertString(Utilities.getRowEnd(doc, target.getSelectionStart()), commentEndString, null); } } catch (BadLocationException e) { problem(null); } }
Example 3
Source File: Utilities.java From netbeans with Apache License 2.0 | 2 votes |
/** * Check whether caret's selection is visible and there is at least * one selected character showing. * * @param caret non-null caret. * @return true if selection is visible and there is at least one selected character. */ public static boolean isSelectionShowing(Caret caret) { return caret.isSelectionVisible() && caret.getDot() != caret.getMark(); }