Java Code Examples for org.netbeans.editor.Utilities#isSelectionShowing()

The following examples show how to use org.netbeans.editor.Utilities#isSelectionShowing() . 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: ToggleBlockCommentAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    final AtomicBoolean processedHere = new AtomicBoolean(false);
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled() || !(target.getDocument() instanceof BaseDocument)) {
            target.getToolkit().beep();
            return;
        }
        final int caretOffset = Utilities.isSelectionShowing(target) ? target.getSelectionStart() : target.getCaretPosition();
        final BaseDocument doc = (BaseDocument) target.getDocument();
        doc.runAtomic(new Runnable() {

            @Override
            public void run() {
                performCustomAction(doc, caretOffset, processedHere);
            }
        });
        if (!processedHere.get()) {
            performDefaultAction(evt, target);
        }
    }
}
 
Example 2
Source File: ToggleBlockCommentAction.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) {
        if (!target.isEditable() || !target.isEnabled() || !(target.getDocument() instanceof BaseDocument)) {
            target.getToolkit().beep();
            return;
        }
        final int caretOffset = Utilities.isSelectionShowing(target) ? target.getSelectionStart() : target.getCaretPosition();
        final BaseDocument baseDocument = (BaseDocument) target.getDocument();
        final AtomicBoolean processedByLatte = new AtomicBoolean(false);
        baseDocument.runAtomic(new Runnable() {

            @Override
            public void run() {
                performLatteAction(baseDocument, caretOffset, processedByLatte);
            }
        });
        if (!processedByLatte.get()) {
            performDefaultAction(evt, target);
        }
    }
}
 
Example 3
Source File: ToggleBlockCommentAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static int[] getPositions(TokenSequence<TplTopTokenId> ts, boolean commentIt, int caretOffset,
        JTextComponent target, TplMetaData tplMetaData) throws BadLocationException {
    int[] positions = new int[2];
    if (commentIt) {
        if (Utilities.isSelectionShowing(target)) {
            positions[0] = caretOffset;
            positions[1] = target.getSelectionEnd();
        } else {
            positions[0] = Utilities.getRowStart((BaseDocument) target.getDocument(), caretOffset);
            positions[1] = Utilities.getRowEnd((BaseDocument) target.getDocument(), caretOffset);
        }
    } else {
        while (ts.movePrevious() && ts.token().id() != TplTopTokenId.T_SMARTY_OPEN_DELIMITER) {
            //NOOP - just find start
        }
        positions[0] = ts.offset();
        while (ts.moveNext() && ts.token().id() != TplTopTokenId.T_SMARTY_CLOSE_DELIMITER) {
            //NOOP - just find end
        }
        positions[1] = ts.offset() - tplMetaData.getCloseDelimiter().length() - 2;
    }
    return positions;
}
 
Example 4
Source File: ToolTipSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public @Override void mouseReleased(MouseEvent evt) {
    lastMouseEvent = evt;
    setToolTipVisible(false);

    // Check that if a selection becomes visible by dragging a mouse
    // the tooltip evaluation should be posted.
    EditorUI ui = extEditorUI;
    if (ui != null) {
        JTextComponent component = ui.getComponent();
        if (enabled && component != null && Utilities.isSelectionShowing(component)) {
            enterTimer.restart();
        }
    }
}
 
Example 5
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 6
Source File: ToggleBlockCommentAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static Positions create(JTextComponent target) {
    boolean isSelection = Utilities.isSelectionShowing(target);
    int start = isSelection ? target.getSelectionStart() : target.getCaretPosition();
    int end = isSelection ? target.getSelectionEnd() : target.getCaretPosition();
    return new Positions(start, end, isSelection);
}
 
Example 7
Source File: FindSelectionAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@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 8
Source File: AddCaretSelectAllAction.java    From netbeans with Apache License 2.0 4 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)) {
            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);
            }
        }

        EditorFindSupport findSupport = EditorFindSupport.getInstance();
        HashMap<String, Object> props = new HashMap<>(findSupport.createDefaultFindProperties());
        String searchWord = target.getSelectedText();
        if (searchWord != null) {
            int n = searchWord.indexOf('\n');
            if (n >= 0) {
                searchWord = searchWord.substring(0, n);
            }
            props.put(EditorFindSupport.FIND_WHAT, searchWord);
            Document doc = target.getDocument();
            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);

            if (caret instanceof EditorCaret) {
                EditorCaret editorCaret = (EditorCaret) caret;
                try {
                    int[] blocks = findSupport.getBlocks(new int[]{-1, -1}, doc, 0, doc.getLength());
                    if (blocks[0] >= 0 && blocks.length % 2 == 0) {
                        List<Position> newCarets = new ArrayList<>();

                        for (int i = 0; i < blocks.length; i += 2) {
                            int start = blocks[i];
                            int end = blocks[i + 1];
                            if (start == -1 || end == -1) {
                                break;
                            }
                            Position startPos = doc.createPosition(start);
                            Position endPos = doc.createPosition(end);
                            newCarets.add(endPos);
                            newCarets.add(startPos);
                        }

                        editorCaret.replaceCarets(newCarets, null); // TODO handle biases
                    }
                } catch (BadLocationException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
    }
}