Java Code Examples for javax.swing.text.Caret#moveDot()

The following examples show how to use javax.swing.text.Caret#moveDot() . 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: GotoBookmarkAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: I18nManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Highlights found hasrdcoded string. */
private void highlightHCString() {
    HardCodedString hStr = hcString;
    
    if (hStr == null) {
        return;
    }
    
    // Highlight found hard coded string.
    Caret caret = caretWRef.get();
    
    if (caret != null) {
        caret.setDot(hStr.getStartPosition().getOffset());
        caret.moveDot(hStr.getEndPosition().getOffset());
    }
}
 
Example 3
Source File: ActionFactory.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        Caret caret = target.getCaret();
        try {
            int pos = Utilities.getRowFirstNonWhite((BaseDocument)target.getDocument(),
                                                    caret.getDot());
            if (pos >= 0) {
                boolean select = BaseKit.selectionFirstNonWhiteAction.equals(getValue(Action.NAME));
                if (select) {
                    caret.moveDot(pos);
                } else {
                    caret.setDot(pos);
                }
            }
        } catch (BadLocationException e) {
            target.getToolkit().beep();
        }
    }
}
 
Example 4
Source File: ActionFactory.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        Caret caret = target.getCaret();
        try {
            int pos = Utilities.getRowLastNonWhite((BaseDocument)target.getDocument(),
                                                   caret.getDot());
            if (pos >= 0) {
                boolean select = BaseKit.selectionLastNonWhiteAction.equals(getValue(Action.NAME));
                if (select) {
                    caret.moveDot(pos);
                } else {
                    caret.setDot(pos);
                }
            }
        } catch (BadLocationException e) {
            target.getToolkit().beep();
        }
    }
}
 
Example 5
Source File: CaretUndoEdit.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void restoreLegacyCaret(Caret caret) {
    int markOffsetAndBias = this.markOffsetAndBias;
    if (markOffsetAndBias == COMPLEX_POSITIONS_MARKER) { // complex positions at time of undo edit creation
        markOffsetAndBias = extraOffsets[1];
    }
    int markOffset = getOffset(markOffsetAndBias);
    int dotOffset = getOffset(dotOffsetAndBias);
    if (caret instanceof DefaultCaret) {
        DefaultCaret dCaret = (DefaultCaret) caret;
        dCaret.setDot(markOffset, getBias(markOffsetAndBias));
        dCaret.moveDot(dotOffset, getBias(dotOffsetAndBias));
    } else {
        caret.setDot(markOffset);
        caret.moveDot(dotOffset);
    }
}
 
Example 6
Source File: Test6462562.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
    Object v0 = getValue();

    Caret caret = getCaret();
    caret.setDot(pos);
    if (selectionLength > 0) {
        caret.moveDot(pos + selectionLength);
    }

    String desc = todo;
    if (todo == BACKSPACE) {
        backspace.actionPerformed(dummyEvent);
    } else if (todo == DELETE) {
        delete.actionPerformed(dummyEvent);
    } else {
        desc = "insert('" + todo + "')";
        insert.actionPerformed(new ActionEvent(this, 0, todo));
    }

    try {
        commitEdit();
    } catch (ParseException e) {
        e.printStackTrace();
        failed = true;
        return false;
    }

    Object v1 = getValue();
    if (! v1.equals(expectedResult)) {
        System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
                v0, pos, pos + selectionLength, desc);
        System.err.printf("   Result: '%s', expected: '%s'\n", v1, expectedResult);
        failed = true;
        return false;
    }
    return true;
}
 
Example 7
Source File: Test6462562.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
    Object v0 = getValue();

    Caret caret = getCaret();
    caret.setDot(pos);
    if (selectionLength > 0) {
        caret.moveDot(pos + selectionLength);
    }

    String desc = todo;
    if (todo == BACKSPACE) {
        backspace.actionPerformed(dummyEvent);
    } else if (todo == DELETE) {
        delete.actionPerformed(dummyEvent);
    } else {
        desc = "insert('" + todo + "')";
        insert.actionPerformed(new ActionEvent(this, 0, todo));
    }

    try {
        commitEdit();
    } catch (ParseException e) {
        e.printStackTrace();
        failed = true;
        return false;
    }

    Object v1 = getValue();
    if (! v1.equals(expectedResult)) {
        System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
                v0, pos, pos + selectionLength, desc);
        System.err.printf("   Result: '%s', expected: '%s'\n", v1, expectedResult);
        failed = true;
        return false;
    }
    return true;
}
 
Example 8
Source File: Test6462562.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
    Object v0 = getValue();

    Caret caret = getCaret();
    caret.setDot(pos);
    if (selectionLength > 0) {
        caret.moveDot(pos + selectionLength);
    }

    String desc = todo;
    if (todo == BACKSPACE) {
        backspace.actionPerformed(dummyEvent);
    } else if (todo == DELETE) {
        delete.actionPerformed(dummyEvent);
    } else {
        desc = "insert('" + todo + "')";
        insert.actionPerformed(new ActionEvent(this, 0, todo));
    }

    try {
        commitEdit();
    } catch (ParseException e) {
        e.printStackTrace();
        failed = true;
        return false;
    }

    Object v1 = getValue();
    if (! v1.equals(expectedResult)) {
        System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
                v0, pos, pos + selectionLength, desc);
        System.err.printf("   Result: '%s', expected: '%s'\n", v1, expectedResult);
        failed = true;
        return false;
    }
    return true;
}
 
Example 9
Source File: Test6462562.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
    Object v0 = getValue();

    Caret caret = getCaret();
    caret.setDot(pos);
    if (selectionLength > 0) {
        caret.moveDot(pos + selectionLength);
    }

    String desc = todo;
    if (todo == BACKSPACE) {
        backspace.actionPerformed(dummyEvent);
    } else if (todo == DELETE) {
        delete.actionPerformed(dummyEvent);
    } else {
        desc = "insert('" + todo + "')";
        insert.actionPerformed(new ActionEvent(this, 0, todo));
    }

    try {
        commitEdit();
    } catch (ParseException e) {
        e.printStackTrace();
        failed = true;
        return false;
    }

    Object v1 = getValue();
    if (! v1.equals(expectedResult)) {
        System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
                v0, pos, pos + selectionLength, desc);
        System.err.printf("   Result: '%s', expected: '%s'\n", v1, expectedResult);
        failed = true;
        return false;
    }
    return true;
}
 
Example 10
Source File: SelectCodeElementAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void select(SelectionInfo selectionInfo) {
    Caret caret = target.getCaret();
    markIgnoreNextCaretUpdate();
    caret.setDot(selectionInfo.getStartOffset());
    markIgnoreNextCaretUpdate();
    caret.moveDot(selectionInfo.getEndOffset());
}
 
Example 11
Source File: Test6462562.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
    Object v0 = getValue();

    Caret caret = getCaret();
    caret.setDot(pos);
    if (selectionLength > 0) {
        caret.moveDot(pos + selectionLength);
    }

    String desc = todo;
    if (todo == BACKSPACE) {
        backspace.actionPerformed(dummyEvent);
    } else if (todo == DELETE) {
        delete.actionPerformed(dummyEvent);
    } else {
        desc = "insert('" + todo + "')";
        insert.actionPerformed(new ActionEvent(this, 0, todo));
    }

    try {
        commitEdit();
    } catch (ParseException e) {
        e.printStackTrace();
        failed = true;
        return false;
    }

    Object v1 = getValue();
    if (! v1.equals(expectedResult)) {
        System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
                v0, pos, pos + selectionLength, desc);
        System.err.printf("   Result: '%s', expected: '%s'\n", v1, expectedResult);
        failed = true;
        return false;
    }
    return true;
}
 
Example 12
Source File: AddCaretSelectNextAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        Caret caret = target.getCaret();
        if (Utilities.isSelectionShowing(caret)) {
            EditorFindSupport findSupport = EditorFindSupport.getInstance();
            HashMap<String, Object> props = new HashMap<>(findSupport.createDefaultFindProperties());
            String searchWord = target.getSelectedText();
            int n = searchWord.indexOf('\n');
            if (n >= 0) {
                searchWord = searchWord.substring(0, n);
            }
            props.put(EditorFindSupport.FIND_WHAT, searchWord);
            props.put(EditorFindSupport.ADD_MULTICARET, Boolean.TRUE);
            EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(target);
            if (eui.getComponent().getClientProperty("AsTextField") == null) { //NOI18N
                findSupport.setFocusedTextComponent(eui.getComponent());
            }
            findSupport.putFindProperties(props);
            findSupport.find(null, false);
            props.put(EditorFindSupport.ADD_MULTICARET, Boolean.FALSE);
            findSupport.putFindProperties(props);
        } else {
            try {
                int[] identifierBlock = Utilities.getIdentifierBlock((BaseDocument) target.getDocument(), caret.getDot());
                if (identifierBlock != null) {
                    caret.setDot(identifierBlock[0]);
                    caret.moveDot(identifierBlock[1]);
                }
            } catch (BadLocationException e) {
                LOGGER.log(Level.WARNING, null, e);
            }
        }
    }
}
 
Example 13
Source File: Test6462562.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
    Object v0 = getValue();

    Caret caret = getCaret();
    caret.setDot(pos);
    if (selectionLength > 0) {
        caret.moveDot(pos + selectionLength);
    }

    String desc = todo;
    if (todo == BACKSPACE) {
        backspace.actionPerformed(dummyEvent);
    } else if (todo == DELETE) {
        delete.actionPerformed(dummyEvent);
    } else {
        desc = "insert('" + todo + "')";
        insert.actionPerformed(new ActionEvent(this, 0, todo));
    }

    try {
        commitEdit();
    } catch (ParseException e) {
        e.printStackTrace();
        failed = true;
        return false;
    }

    Object v1 = getValue();
    if (! v1.equals(expectedResult)) {
        System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
                v0, pos, pos + selectionLength, desc);
        System.err.printf("   Result: '%s', expected: '%s'\n", v1, expectedResult);
        failed = true;
        return false;
    }
    return true;
}
 
Example 14
Source File: EditorUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Set the position of the caret and scroll the extent if necessary.
 * @param offset position where the caret should be placed
 * @param scrollRect rectangle that should become visible. It can be null
 *   when no scrolling should be done.
 * @param scrollPolicy policy to be used when scrolling.
 * @deprecated
 */
public void caretMoveDot(int offset, Rectangle scrollRect, int scrollPolicy) {
    if (component != null) {
        Caret caret = component.getCaret();
        if (caret instanceof BaseCaret) {
            ((BaseCaret)caret).moveDot(offset, scrollRect, scrollPolicy);
        } else {
            caret.moveDot(offset);
        }
    }
}
 
Example 15
Source File: Test6462562.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
    Object v0 = getValue();

    Caret caret = getCaret();
    caret.setDot(pos);
    if (selectionLength > 0) {
        caret.moveDot(pos + selectionLength);
    }

    String desc = todo;
    if (todo == BACKSPACE) {
        backspace.actionPerformed(dummyEvent);
    } else if (todo == DELETE) {
        delete.actionPerformed(dummyEvent);
    } else {
        desc = "insert('" + todo + "')";
        insert.actionPerformed(new ActionEvent(this, 0, todo));
    }

    try {
        commitEdit();
    } catch (ParseException e) {
        e.printStackTrace();
        failed = true;
        return false;
    }

    Object v1 = getValue();
    if (! v1.equals(expectedResult)) {
        System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
                v0, pos, pos + selectionLength, desc);
        System.err.printf("   Result: '%s', expected: '%s'\n", v1, expectedResult);
        failed = true;
        return false;
    }
    return true;
}
 
Example 16
Source File: Test6462562.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
    Object v0 = getValue();

    Caret caret = getCaret();
    caret.setDot(pos);
    if (selectionLength > 0) {
        caret.moveDot(pos + selectionLength);
    }

    String desc = todo;
    if (todo == BACKSPACE) {
        backspace.actionPerformed(dummyEvent);
    } else if (todo == DELETE) {
        delete.actionPerformed(dummyEvent);
    } else {
        desc = "insert('" + todo + "')";
        insert.actionPerformed(new ActionEvent(this, 0, todo));
    }

    try {
        commitEdit();
    } catch (ParseException e) {
        e.printStackTrace();
        failed = true;
        return false;
    }

    Object v1 = getValue();
    if (! v1.equals(expectedResult)) {
        System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
                v0, pos, pos + selectionLength, desc);
        System.err.printf("   Result: '%s', expected: '%s'\n", v1, expectedResult);
        failed = true;
        return false;
    }
    return true;
}
 
Example 17
Source File: Test6462562.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
    Object v0 = getValue();

    Caret caret = getCaret();
    caret.setDot(pos);
    if (selectionLength > 0) {
        caret.moveDot(pos + selectionLength);
    }

    String desc = todo;
    if (todo == BACKSPACE) {
        backspace.actionPerformed(dummyEvent);
    } else if (todo == DELETE) {
        delete.actionPerformed(dummyEvent);
    } else {
        desc = "insert('" + todo + "')";
        insert.actionPerformed(new ActionEvent(this, 0, todo));
    }

    try {
        commitEdit();
    } catch (ParseException e) {
        e.printStackTrace();
        failed = true;
        return false;
    }

    Object v1 = getValue();
    if (! v1.equals(expectedResult)) {
        System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
                v0, pos, pos + selectionLength, desc);
        System.err.printf("   Result: '%s', expected: '%s'\n", v1, expectedResult);
        failed = true;
        return false;
    }
    return true;
}
 
Example 18
Source File: Test6462562.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
    Object v0 = getValue();

    Caret caret = getCaret();
    caret.setDot(pos);
    if (selectionLength > 0) {
        caret.moveDot(pos + selectionLength);
    }

    String desc = todo;
    if (todo == BACKSPACE) {
        backspace.actionPerformed(dummyEvent);
    } else if (todo == DELETE) {
        delete.actionPerformed(dummyEvent);
    } else {
        desc = "insert('" + todo + "')";
        insert.actionPerformed(new ActionEvent(this, 0, todo));
    }

    try {
        commitEdit();
    } catch (ParseException e) {
        e.printStackTrace();
        failed = true;
        return false;
    }

    Object v1 = getValue();
    if (! v1.equals(expectedResult)) {
        System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
                v0, pos, pos + selectionLength, desc);
        System.err.printf("   Result: '%s', expected: '%s'\n", v1, expectedResult);
        failed = true;
        return false;
    }
    return true;
}
 
Example 19
Source File: OverrideEditorActions.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        Caret caret = target.getCaret();
        BaseDocument doc = (BaseDocument)target.getDocument();
        try {
            int dot = caret.getDot();
            // #232675: if bounds are defined, use them rather than line start/end
            Object o = target.getClientProperty(PROP_NAVIGATE_BOUNDARIES);
            PositionRegion bounds = null;
            int lineStartPos = Utilities.getRowStart(target, dot);
            
            if (o instanceof PositionRegion) {
                bounds = (PositionRegion)o;
                int start = bounds.getStartOffset();
                int end = bounds.getEndOffset();
                int boundLineStart = Utilities.getRowStart(target, start);
                // refinement: only use the boundaries if the caret is at the same line
                // as boundary start; otherwise ignore the boundary and use document lines.
                if (boundLineStart == lineStartPos && dot > start && dot <= end) {
                    // move to the region start
                    dot = start;
                } else {
                    bounds = null;
                }
            }
            
            if (bounds == null) {
                if (homeKeyColumnOne) { // to first column
                    dot = lineStartPos;
                } else { // either to line start or text start
                    int textStartPos = Utilities.getRowFirstNonWhite(doc, lineStartPos);
                    if (textStartPos < 0) { // no text on the line
                        textStartPos = Utilities.getRowEnd(target, lineStartPos);
                    }
                    if (dot == lineStartPos) { // go to the text start pos
                        dot = textStartPos;
                    } else if (dot <= textStartPos) {
                        dot = lineStartPos;
                    } else {
                        dot = textStartPos;
                    }
                }
            }
            // For partial view hierarchy check bounds
            dot = Math.max(dot, target.getUI().getRootView(target).getStartOffset());
            String actionName = (String) getValue(Action.NAME);
            boolean select = selectionBeginLineAction.equals(actionName)
                    || selectionLineFirstColumnAction.equals(actionName);
            
            // If possible scroll the view to its begining horizontally
            // to ease user's orientation in the code.
            Rectangle r = target.modelToView(dot);
            Rectangle visRect = target.getVisibleRect();
            if (r.getMaxX() < visRect.getWidth()) {
                r.x = 0;
                target.scrollRectToVisible(r);
            }
            target.putClientProperty("navigational.action", SwingConstants.WEST);
            if (select) {
                caret.moveDot(dot);
            } else {
                caret.setDot(dot);
            }
        } catch (BadLocationException e) {
            target.getToolkit().beep();
        }
    }
}
 
Example 20
Source File: MasterMatcher.java    From netbeans with Apache License 2.0 4 votes vote down vote up
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]);
                }
            }
        }
    }
}