Java Code Examples for javax.swing.text.Caret#getDot()
The following examples show how to use
javax.swing.text.Caret#getDot() .
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: SQLHistoryPanel.java From netbeans with Apache License 2.0 | 6 votes |
private int insertIntoDocument(String s, JEditorPane target) throws BadLocationException { Document doc = target.getDocument(); if (s == null) { s = ""; } if (doc == null) { return -1; } int start = -1; try { Caret caret = target.getCaret(); int p0 = Math.min(caret.getDot(), caret.getMark()); int p1 = Math.max(caret.getDot(), caret.getMark()); doc.remove(p0, p1 - p0); start = caret.getDot(); doc.insertString(start, s + ";\n", null); // NOI18N } catch (BadLocationException ble) { LOGGER.log(Level.WARNING, org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_InsertAtLocationError") + ble); } return start; }
Example 2
Source File: JSFPaletteUtilities.java From netbeans with Apache License 2.0 | 6 votes |
private static int insert(String s, JTextComponent target, Document doc) throws BadLocationException { int start = -1; try { //at first, find selected text range Caret caret = target.getCaret(); int p0 = Math.min(caret.getDot(), caret.getMark()); int p1 = Math.max(caret.getDot(), caret.getMark()); doc.remove(p0, p1 - p0); //replace selected text by the inserted one start = caret.getDot(); doc.insertString(start, s, null); } catch (BadLocationException ble) { } return start; }
Example 3
Source File: SelectCodeElementAction.java From netbeans with Apache License 2.0 | 6 votes |
private int computeSelIndex(boolean inner) { Caret caret = target.getCaret(); if (selectionInfos != null && caret != null && caret.getDot() != caret.getMark()) { int dot = caret.getDot(); int mark = caret.getMark(); int start = Math.min(dot,mark); //int end = Math.max(dot,mark); for (int i = 0; i < selectionInfos.length; i++) { if (selectionInfos[i].getStartOffset() == start) { // TODO - check end offset too return i; } } // No exact match - look at the editor selection and find the range // that most closely surround the selection (if inner is true, go // for the inner one, otherwise the outer) for (int i = selectionInfos.length-2; i >= 0; i--) { if (selectionInfos[i].getStartOffset() > start && selectionInfos[i+1].getStartOffset() < start) { return inner ? i : i-1; } } } return selIndex; }
Example 4
Source File: GotoBookmarkAction.java From netbeans with Apache License 2.0 | 6 votes |
@Override public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { Caret caret = target.getCaret(); BookmarkList bookmarkList = BookmarkList.get(target.getDocument()); int dotOffset = caret.getDot(); Bookmark bookmark = gotoNext ? bookmarkList.getNextBookmark(dotOffset, true) // next (wrap) : bookmarkList.getPreviousBookmark(dotOffset, true); // previous (wrap) if (bookmark != null) { if (select) { caret.moveDot(bookmark.getOffset()); } else { caret.setDot(bookmark.getOffset()); } } } }
Example 5
Source File: ElementResultItem.java From netbeans with Apache License 2.0 | 6 votes |
/** * If called with <code>SHIFT_MASK</code> modified it createa a start tag and * end tag pair and place caret between them. */ public boolean substituteText( JTextComponent c, int offset, int len, int modifiers ){ String replacementText = getReplacementText(modifiers); replaceText(c, replacementText, offset, len); boolean shift = (modifiers & java.awt.event.InputEvent.SHIFT_MASK) != 0; if (shift && startElement) { Caret caret = c.getCaret(); // it is at the end of replacement int dot = caret.getDot(); int rlen = replacementText.length(); if (empty) { caret.setDot((dot - rlen) + replacementText.indexOf('/')); } } return false; }
Example 6
Source File: MainPanel.java From java-swing-tips with MIT License | 6 votes |
private void fire(Object c) { if (c instanceof JTextComponent) { JTextComponent tc = (JTextComponent) c; Caret caret = tc.getCaret(); int d = caret.getDot(); int m = caret.getMark(); // LOGGER.info(() -> String.format("%s / %s", m, d)); if (d != m && (dot != d || mark != m)) { Optional.ofNullable(tc.getSelectedText()).ifPresent(str -> { LOGGER.info(() -> str); // StringSelection data = new StringSelection(str); // Toolkit.getDefaultToolkit().getSystemClipboard().setContents(data, data); tc.copy(); }); } dot = d; mark = m; } }
Example 7
Source File: EditorCaret.java From netbeans with Apache License 2.0 | 5 votes |
/** * Determines if the following are true: * <ul> * <li>the press event is located over a selection * <li>the dragEnabled property is true * <li>A TranferHandler is installed * </ul> * <p> * This is implemented to check for a TransferHandler. * Subclasses should perform the remaining conditions. */ private boolean isDragPossible(MouseEvent e) { Object src = e.getSource(); if (src instanceof JComponent) { JComponent comp = (JComponent) src; boolean possible = (comp == null) ? false : (comp.getTransferHandler() != null); if (possible && comp instanceof JTextComponent) { JTextComponent c = (JTextComponent) comp; if (c.getDragEnabled()) { Caret caret = c.getCaret(); int dot = caret.getDot(); int mark = caret.getMark(); if (dot != mark) { Point p = new Point(e.getX(), e.getY()); int pos = c.viewToModel(p); int p0 = Math.min(dot, mark); int p1 = Math.max(dot, mark); if ((pos >= p0) && (pos < p1)) { return true; } } } } } return false; }
Example 8
Source File: GspKit.java From netbeans with Apache License 2.0 | 5 votes |
@Override public void actionPerformed(ActionEvent evt, JTextComponent target) { Caret caret = target.getCaret(); BaseDocument doc = (BaseDocument)target.getDocument(); int dotPos = caret.getDot(); if (handleDeletion(doc, dotPos)) { return; } super.actionPerformed(evt, target); }
Example 9
Source File: GotoAction.java From netbeans with Apache License 2.0 | 5 votes |
@Override public void actionPerformed(ActionEvent evt, JTextComponent target) { String actionName = actionName(); if (EditorActionNames.gotoDeclaration.equals(actionName)) { resetCaretMagicPosition(target); if (target != null) { if (hyperlinkGoTo(target)) { return; } BaseDocument doc = Utilities.getDocument(target); if (doc != null) { try { Caret caret = target.getCaret(); int dotPos = caret.getDot(); int[] idBlk = Utilities.getIdentifierBlock(doc, dotPos); ExtSyntaxSupport extSup = (ExtSyntaxSupport) doc.getSyntaxSupport(); if (idBlk != null) { int decPos = extSup.findDeclarationPosition(doc.getText(idBlk), idBlk[1]); if (decPos >= 0) { caret.setDot(decPos); } } } catch (BadLocationException e) { } } } } }
Example 10
Source File: GroovyTypedTextInterceptor.java From netbeans with Apache License 2.0 | 5 votes |
private void reindent(BaseDocument doc, int offset, TokenId id, Caret caret) throws BadLocationException { TokenSequence<GroovyTokenId> ts = LexUtilities.getGroovyTokenSequence(doc, offset); if (ts != null) { ts.move(offset); if (!ts.moveNext() && !ts.movePrevious()) { return; } Token<GroovyTokenId> token = ts.token(); if ((token.id() == id)) { final int rowFirstNonWhite = Utilities.getRowFirstNonWhite(doc, offset); // Ensure that this token is at the beginning of the line if (ts.offset() > rowFirstNonWhite) { return; } OffsetRange begin = OffsetRange.NONE; if (id == GroovyTokenId.RBRACE) { begin = LexUtilities.findBwd(doc, ts, GroovyTokenId.LBRACE, GroovyTokenId.RBRACE); } else if (id == GroovyTokenId.RBRACKET) { begin = LexUtilities.findBwd(doc, ts, GroovyTokenId.LBRACKET, GroovyTokenId.RBRACKET); } if (begin != OffsetRange.NONE) { int beginOffset = begin.getStart(); int indent = GsfUtilities.getLineIndent(doc, beginOffset); previousAdjustmentIndent = GsfUtilities.getLineIndent(doc, offset); GsfUtilities.setLineIndentation(doc, offset, indent); previousAdjustmentOffset = caret.getDot(); } } } }
Example 11
Source File: QuietEditorPane.java From netbeans with Apache License 2.0 | 5 votes |
/** Overrides superclass method, to keep old caret position. * While is reinstallation of UI in progress, there * is a gap between the uninstallUI * and intstallUI when caret set to <code>null</code>. */ @Override public void setCaret(Caret caret) { if (caret == null) { Caret oldCaret = getCaret(); if (oldCaret != null) { lastPosition = oldCaret.getDot(); } } super.setCaret(caret); }
Example 12
Source File: EditorContextImpl.java From netbeans with Apache License 2.0 | 5 votes |
/** * Returns number of line currently selected in editor or <code>-1</code>. * * @return number of line currently selected in editor or <code>-1</code> */ public int getCurrentOffset () { JEditorPane ep = contextDispatcher.getCurrentEditor(); if (ep == null) { return -1; } Caret caret = ep.getCaret (); if (caret == null) { return -1; } return caret.getDot(); }
Example 13
Source File: WatchPanel.java From netbeans with Apache License 2.0 | 5 votes |
private static int getRecentColumn() { JEditorPane mostRecentEditor = EditorContextDispatcher.getDefault().getMostRecentEditor(); if (mostRecentEditor != null) { Caret caret = mostRecentEditor.getCaret(); if (caret != null) { int offset = caret.getDot(); try { int rs = javax.swing.text.Utilities.getRowStart(mostRecentEditor, offset); return offset - rs; } catch (BadLocationException blex) {} } } return 0; }
Example 14
Source File: AnimationAutoCompletion.java From 3Dscript with BSD 2-Clause "Simplified" License | 5 votes |
/** * Inserts a completion. Any time a code completion event occurs, the actual * text insertion happens through this method. * * @param c A completion to insert. This cannot be <code>null</code>. * @param typedParamListStartChar Whether the parameterized completion start * character was typed (typically <code>'('</code>). */ @Override protected void insertCompletion(Completion c, boolean typedParamListStartChar) { JTextComponent textComp = getTextComponent(); String alreadyEntered = c.getAlreadyEntered(textComp); hidePopupWindow(); Caret caret = textComp.getCaret(); int dot = caret.getDot(); int len = alreadyEntered.length(); int start = dot - len; String replacement = getReplacementText(c, textComp.getDocument(), start, len); // Bene if(replacement != null && replacement.startsWith("(none)")) return; caret.setDot(start); caret.moveDot(dot); textComp.replaceSelection(replacement); if (isParameterAssistanceEnabled() && (c instanceof ParameterizedCompletion)) { ParameterizedCompletion pc = (ParameterizedCompletion) c; startParameterizedCompletionAssistance(pc, typedParamListStartChar); } }
Example 15
Source File: FindSelectionAction.java From netbeans with Apache License 2.0 | 4 votes |
@Override @SuppressWarnings("unchecked") public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { EditorFindSupport findSupport = EditorFindSupport.getInstance(); Caret caret = target.getCaret(); int dotPos = caret.getDot(); HashMap<String, Object> props = new HashMap<>(findSupport.createDefaultFindProperties()); String searchWord = null; boolean revert = false; Boolean originalValue = null; Map<String, Object> revertMap = (Map<String, Object>) props.get(EditorFindSupport.REVERT_MAP); Boolean revertValue = revertMap != null ? (Boolean) revertMap.get(EditorFindSupport.FIND_WHOLE_WORDS) : null; if (Utilities.isSelectionShowing(caret)) { // valid selection searchWord = target.getSelectedText(); originalValue = (Boolean) props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE); if (Boolean.FALSE.equals(revertValue)) { revertMap.remove(EditorFindSupport.FIND_WHOLE_WORDS); } else { revert = !Boolean.FALSE.equals(originalValue); } } else { // no selection, get current word try { searchWord = Utilities.getIdentifier((BaseDocument) target.getDocument(), dotPos); originalValue = (Boolean) props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.TRUE); if (Boolean.TRUE.equals(revertValue)) { revertMap.remove(EditorFindSupport.FIND_WHOLE_WORDS); } else { revert = !Boolean.TRUE.equals(originalValue); } } catch (BadLocationException e) { LOGGER.log(Level.WARNING, null, e); } } if (searchWord != null) { int n = searchWord.indexOf('\n'); if (n >= 0) { searchWord = searchWord.substring(0, n); } props.put(EditorFindSupport.FIND_WHAT, searchWord); if (revert) { revertMap = new HashMap<>(); revertMap.put(EditorFindSupport.FIND_WHOLE_WORDS, originalValue != null ? originalValue : Boolean.FALSE); props.put(EditorFindSupport.REVERT_MAP, revertMap); } props.put(EditorFindSupport.FIND_BLOCK_SEARCH, Boolean.FALSE); props.put(EditorFindSupport.FIND_BLOCK_SEARCH_START, null); props.put(EditorFindSupport.FIND_BLOCK_SEARCH_END, null); EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(target); if (eui.getComponent().getClientProperty("AsTextField") == null) { //NOI18N findSupport.setFocusedTextComponent(eui.getComponent()); } findSupport.putFindProperties(props); if (findSupport.find(null, false)) { findSupport.addToHistory(new EditorFindSupport.SPW((String) props.get(EditorFindSupport.FIND_WHAT), (Boolean) props.get(EditorFindSupport.FIND_WHOLE_WORDS), (Boolean) props.get(EditorFindSupport.FIND_MATCH_CASE), (Boolean) props.get(EditorFindSupport.FIND_REG_EXP))); } } } }
Example 16
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 17
Source File: JmePaletteUtilities.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 4 votes |
private static int insert(String s, JTextComponent target, Document doc) throws BadLocationException { int start = -1; try { //firstly, find selected text range: Caret caret = target.getCaret(); int p0 = Math.min(caret.getDot(), caret.getMark()); int p1 = Math.max(caret.getDot(), caret.getMark()); doc.remove(p0, p1 - p0); //then, replace selected text range with the inserted one: start = caret.getDot(); doc.insertString(start, s, null); } catch (BadLocationException ble) {} return start; }
Example 18
Source File: MasterMatcher.java From netbeans with Apache License 2.0 | 4 votes |
private static void navigateAreas( int [] origin, int [] matches, int caretOffset, Object caretBias, Caret caret, boolean select ) { if (matches != null && matches.length >= 2) { int newDotBackwardIdx = -1; int newDotForwardIdx = -1; for(int i = 0; i < matches.length / 2; i++) { if (matches[i * 2] <= origin[0] && (newDotBackwardIdx == -1 || matches[i * 2] > matches[newDotBackwardIdx * 2]) ) { newDotBackwardIdx = i; } if (matches[i * 2] >= origin[1] && (newDotForwardIdx == -1 || matches[i * 2] < matches[newDotForwardIdx * 2]) ) { newDotForwardIdx = i; } } if (newDotBackwardIdx != -1) { if (select) { int set, move; if (caretOffset < origin[1]) { set = origin[0]; move = matches[2 * newDotBackwardIdx + 1]; } else { set = origin[1]; move = matches[2 * newDotBackwardIdx]; } if (caret.getDot() == caret.getMark()) { // || (move <= caret.getMark() && caret.getMark() <= set) caret.setDot(set); } caret.moveDot(move); } else { if (B_BACKWARD.equalsIgnoreCase(caretBias.toString())) { caret.setDot(matches[2 * newDotBackwardIdx + 1]); } else { caret.setDot(matches[2 * newDotBackwardIdx]); } } } else if (newDotForwardIdx != -1) { if (select) { int set, move; if (caretOffset > origin[0]) { set = origin[1]; move = matches[2 * newDotForwardIdx]; } else { set = origin[0]; move = matches[2 * newDotForwardIdx + 1]; } if (caret.getDot() == caret.getMark()) { // || (set <= caret.getMark() && caret.getMark() <= move) caret.setDot(set); } caret.moveDot(move); } else { if (B_BACKWARD.equalsIgnoreCase(caretBias.toString())) { caret.setDot(matches[2 * newDotForwardIdx + 1]); } else { caret.setDot(matches[2 * newDotForwardIdx]); } } } } }
Example 19
Source File: PhpTypedTextInterceptor.java From netbeans with Apache License 2.0 | 4 votes |
private void reindent(BaseDocument doc, int offset, TokenId id, Caret caret) throws BadLocationException { TokenSequence<? extends PHPTokenId> ts = LexUtilities.getPHPTokenSequence(doc, offset); if (ts != null) { ts.move(offset); if (!ts.moveNext() && !ts.movePrevious()) { return; } Token<? extends PHPTokenId> token = ts.token(); if ((token.id() == id)) { final int rowFirstNonWhite = Utilities.getRowFirstNonWhite(doc, offset); if (id == PHPTokenId.PHP_CURLY_OPEN && ts.offset() == rowFirstNonWhite && ts.movePrevious()) { // The curly is at the first nonwhite char at the line. // Do we need to indent the { according previous line? int previousExprestion = LexUtilities.findStartTokenOfExpression(ts); int previousIndent = Utilities.getRowIndent(doc, previousExprestion); int currentIndent = Utilities.getRowIndent(doc, offset); int newIndent = IndentUtils.countIndent(doc, offset, previousIndent); if (newIndent != currentIndent) { GsfUtilities.setLineIndentation(doc, offset, Math.max(newIndent, 0)); } } else if (id == PHPTokenId.WHITESPACE || (id == PHPTokenId.PHP_TOKEN && token.text().charAt(0) == ':')) { // ":" handles "default:" if (id == PHPTokenId.WHITESPACE) { LexUtilities.findPreviousToken(ts, Arrays.asList(PHPTokenId.PHP_CASE)); } else { LexUtilities.findPreviousToken(ts, Arrays.asList(PHPTokenId.PHP_DEFAULT)); } if (ts.offset() >= rowFirstNonWhite) { //previous "case" or "default" on one line with typed char LexUtilities.findPreviousToken(ts, Arrays.asList(PHPTokenId.PHP_SWITCH)); Token<? extends PHPTokenId> firstCaseInSwitch = LexUtilities.findNextToken(ts, Arrays.asList(PHPTokenId.PHP_CASE)); if (firstCaseInSwitch != null && firstCaseInSwitch.id() == PHPTokenId.PHP_CASE) { int indentOfFirstCase = GsfUtilities.getLineIndent(doc, ts.offset()); GsfUtilities.setLineIndentation(doc, offset, indentOfFirstCase); } } } else if (id == PHPTokenId.PHP_CURLY_CLOSE) { OffsetRange begin = LexUtilities.findBwd(doc, ts, PHPTokenId.PHP_CURLY_OPEN, '{', PHPTokenId.PHP_CURLY_CLOSE, '}'); if (begin != OffsetRange.NONE) { int beginOffset = begin.getStart(); int indent = GsfUtilities.getLineIndent(doc, beginOffset); previousAdjustmentIndent = GsfUtilities.getLineIndent(doc, offset); GsfUtilities.setLineIndentation(doc, offset, indent); previousAdjustmentOffset = caret.getDot(); } } } } }
Example 20
Source File: JsTypedTextInterceptor.java From netbeans with Apache License 2.0 | 3 votes |
/** * A hook to be called after closing bracket ) or ] was inserted into * the document. The method checks if the bracket should stay there * or be removed and some exisitng bracket just skipped. * * @param doc the document * @param dotPos position of the inserted bracket * @param caret caret * @param bracket the bracket character ']' or ')' */ private void skipClosingBracket(BaseDocument doc, Caret caret, char bracket, TokenId bracketId) throws BadLocationException { int caretOffset = caret.getDot(); if (isSkipClosingBracket(doc, caretOffset, bracketId)) { doc.remove(caretOffset - 1, 1); caret.setDot(caretOffset); // skip closing bracket } }