Search in sources :

Example 1 with Caret

use of javax.swing.text.Caret in project enclojure by EricThorsen.

the class ClojureCodeCompletion_Provider method getAllHippieMatches.

public static ArrayList<String> getAllHippieMatches(JTextComponent target) {
    ArrayList<String> ret = new ArrayList<String>();
    EditorUI editorUI = Utilities.getEditorUI(target);
    Caret caret = target.getCaret();
    int dotPos = caret.getDot();
    WordMatch m = editorUI.getWordMatch();
    if (m != null) {
        String s = null;
        String searchWord = "";
        int c = 0;
        do {
            c += 1;
            s = m.getMatchWord(dotPos, false);
            if (s != null && !ret.contains(s)) {
                ret.add(s);
                if (c == 1)
                    searchWord = m.getPreviousWord();
            }
        } while (s != null && m.isFound());
        if (searchWord == null)
            searchWord = "";
        do {
            s = m.getMatchWord(dotPos + searchWord.length() + 2, true);
            if (s != null && !ret.contains(s))
                if (!s.equalsIgnoreCase(searchWord) && !ret.contains(s))
                    ret.add(s);
        } while (s != null && m.isFound());
    }
    return ret;
}
Also used : WordMatch(org.netbeans.editor.WordMatch) EditorUI(org.netbeans.editor.EditorUI) ArrayList(java.util.ArrayList) Caret(javax.swing.text.Caret)

Example 2 with Caret

use of javax.swing.text.Caret in project omegat by omegat-org.

the class StaticUIUtils method makeCaretAlwaysVisible.

/**
 * Make caret visible even when the {@link JTextComponent} is not editable.
 */
public static FocusListener makeCaretAlwaysVisible(final JTextComponent comp) {
    FocusListener listener = new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            Caret caret = comp.getCaret();
            caret.setVisible(true);
            caret.setSelectionVisible(true);
        }
    };
    comp.addFocusListener(listener);
    return listener;
}
Also used : FocusAdapter(java.awt.event.FocusAdapter) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent) Caret(javax.swing.text.Caret) DefaultCaret(javax.swing.text.DefaultCaret)

Example 3 with Caret

use of javax.swing.text.Caret in project blue by kunstmusik.

the class RemoveSemiColonLineCommentAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent evt, final JTextComponent target) {
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled()) {
            target.getToolkit().beep();
            return;
        }
        final Caret caret = target.getCaret();
        final BaseDocument doc = (BaseDocument) target.getDocument();
        doc.runAtomicAsUser(() -> {
            try {
                int startPos;
                int endPos;
                if (Utilities.isSelectionShowing(caret)) {
                    startPos = Utilities.getRowStart(doc, target.getSelectionStart());
                    endPos = target.getSelectionEnd();
                    if (endPos > 0 && Utilities.getRowStart(doc, endPos) == endPos) {
                        endPos--;
                    }
                    endPos = Utilities.getRowEnd(doc, endPos);
                } else {
                    // selection not visible
                    startPos = Utilities.getRowStart(doc, caret.getDot());
                    endPos = Utilities.getRowEnd(doc, caret.getDot());
                }
                int lineCount = Utilities.getRowCount(doc, startPos, endPos);
                uncomment(doc, startPos, lineCount);
            } catch (BadLocationException e) {
                target.getToolkit().beep();
            }
        });
    }
}
Also used : BaseDocument(org.netbeans.editor.BaseDocument) Caret(javax.swing.text.Caret) BadLocationException(javax.swing.text.BadLocationException)

Example 4 with Caret

use of javax.swing.text.Caret in project jsql-injection by ron190.

the class DeleteNextCharAction method actionPerformed.

/**
 * The operation to perform when this action is triggered.
 */
@Override
public void actionPerformed(ActionEvent e) {
    JTextComponent target = this.getTextComponent(e);
    if ((target != null) && (target.isEditable())) {
        try {
            Document doc = target.getDocument();
            Caret caret = target.getCaret();
            int dot = caret.getDot();
            int mark = caret.getMark();
            if (dot != mark) {
                doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
            } else if (dot < doc.getLength()) {
                int delChars = 1;
                if (dot < doc.getLength() - 1) {
                    String dotChars = doc.getText(dot, 2);
                    char c0 = dotChars.charAt(0);
                    char c1 = dotChars.charAt(1);
                    if (c0 >= '\uD800' && c0 <= '\uDBFF' && c1 >= '\uDC00' && c1 <= '\uDFFF') {
                        delChars = 2;
                    }
                }
                doc.remove(dot, delChars);
            }
        } catch (BadLocationException ble) {
            LOGGER.error(ble, ble);
        }
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent) Document(javax.swing.text.Document) Caret(javax.swing.text.Caret) BadLocationException(javax.swing.text.BadLocationException)

Example 5 with Caret

use of javax.swing.text.Caret in project jsql-injection by ron190.

the class SilentDeleteTextAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    JTextComponent target = this.getTextComponent(e);
    if (Objects.nonNull(target) && target.isEditable()) {
        Caret caret = target.getCaret();
        int dot = caret.getDot();
        int mark = caret.getMark();
        if (DefaultEditorKit.deletePrevCharAction.equals(this.getValue(Action.NAME))) {
            // @see javax/swing/text/DefaultEditorKit.java DeletePrevCharAction
            if (dot == 0 && mark == 0) {
                return;
            }
        } else {
            // @see javax/swing/text/DefaultEditorKit.java DeleteNextCharAction
            Document doc = target.getDocument();
            if (dot == mark && doc.getLength() == dot) {
                return;
            }
        }
    }
    this.deleteAction.actionPerformed(e);
}
Also used : JTextComponent(javax.swing.text.JTextComponent) Document(javax.swing.text.Document) Caret(javax.swing.text.Caret)

Aggregations

Caret (javax.swing.text.Caret)16 JTextComponent (javax.swing.text.JTextComponent)8 DefaultCaret (javax.swing.text.DefaultCaret)7 BadLocationException (javax.swing.text.BadLocationException)5 Document (javax.swing.text.Document)5 BaseDocument (org.netbeans.editor.BaseDocument)2 Component (java.awt.Component)1 Point (java.awt.Point)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 FocusAdapter (java.awt.event.FocusAdapter)1 FocusEvent (java.awt.event.FocusEvent)1 FocusListener (java.awt.event.FocusListener)1 ArrayList (java.util.ArrayList)1 JComponent (javax.swing.JComponent)1 JMenu (javax.swing.JMenu)1 JMenuItem (javax.swing.JMenuItem)1 JPopupMenu (javax.swing.JPopupMenu)1 AbstractDocument (javax.swing.text.AbstractDocument)1 EditorUI (org.netbeans.editor.EditorUI)1