Search in sources :

Example 11 with Caret

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

the class AddSemiColonLineCommentAction 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 (org.netbeans.editor.Utilities.isSelectionShowing(caret)) {
                    startPos = org.netbeans.editor.Utilities.getRowStart(doc, target.getSelectionStart());
                    endPos = target.getSelectionEnd();
                    if (endPos > 0 && org.netbeans.editor.Utilities.getRowStart(doc, endPos) == endPos) {
                        endPos--;
                    }
                    endPos = org.netbeans.editor.Utilities.getRowEnd(doc, endPos);
                } else {
                    // selection not visible
                    startPos = org.netbeans.editor.Utilities.getRowStart(doc, caret.getDot());
                    endPos = org.netbeans.editor.Utilities.getRowEnd(doc, caret.getDot());
                }
                int lineCount = org.netbeans.editor.Utilities.getRowCount(doc, startPos, endPos);
                comment(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 12 with Caret

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

the class DeletePrevCharAction method actionPerformed.

/**
 * The operation to perform when this action is triggered.
 *
 * @param e the action event
 */
@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 > 0) {
                int delChars = 1;
                if (dot > 1) {
                    String dotChars = doc.getText(dot - 2, 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, 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 13 with Caret

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

the class GlossaryTextArea method processKeyEvent.

@Override
protected void processKeyEvent(KeyEvent e) {
    KeyStroke s = KeyStroke.getKeyStrokeForEvent(e);
    if (s.equals(PropertiesShortcuts.getEditorShortcuts().getKeyStroke("editorContextMenu"))) {
        JPopupMenu popup = new JPopupMenu();
        populateContextMenu(popup);
        Caret caret = getCaret();
        Point p = caret == null ? getMousePosition() : caret.getMagicCaretPosition();
        popup.show(this, (int) p.getX(), (int) p.getY());
        e.consume();
    }
    super.processKeyEvent(e);
}
Also used : KeyStroke(javax.swing.KeyStroke) Point(java.awt.Point) JPopupMenu(javax.swing.JPopupMenu) Caret(javax.swing.text.Caret)

Example 14 with Caret

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

the class MultipleTransPane method processKeyEvent.

@Override
protected void processKeyEvent(KeyEvent e) {
    KeyStroke s = KeyStroke.getKeyStrokeForEvent(e);
    if (s.equals(PropertiesShortcuts.getEditorShortcuts().getKeyStroke("editorContextMenu"))) {
        JPopupMenu popup = new JPopupMenu();
        Caret caret = getCaret();
        Point p = caret == null ? getMousePosition() : caret.getMagicCaretPosition();
        populateContextMenu(popup, Java8Compat.viewToModel(MultipleTransPane.this, p));
        popup.show(this, (int) p.getX(), (int) p.getY());
        e.consume();
    }
    super.processKeyEvent(e);
}
Also used : KeyStroke(javax.swing.KeyStroke) Point(java.awt.Point) JPopupMenu(javax.swing.JPopupMenu) Caret(javax.swing.text.Caret)

Example 15 with Caret

use of javax.swing.text.Caret in project java-swing-tips by aterai.

the class FocusOwnerCaret method makeTextArea.

private static Component makeTextArea(boolean flg) {
    JTextArea textArea = new JTextArea() {

        @Override
        public void updateUI() {
            setCaret(null);
            super.updateUI();
            if (flg) {
                Caret oldCaret = getCaret();
                int blinkRate = oldCaret.getBlinkRate();
                Caret caret = new FocusOwnerCaret();
                caret.setBlinkRate(blinkRate);
                setCaret(caret);
                caret.setSelectionVisible(true);
            }
        }
    };
    textArea.setText("FocusOwnerCaret: " + flg + "\n111\n22222\n33333333\n");
    textArea.selectAll();
    return new JScrollPane(textArea);
}
Also used : Caret(javax.swing.text.Caret) DefaultCaret(javax.swing.text.DefaultCaret)

Aggregations

Caret (javax.swing.text.Caret)21 DefaultCaret (javax.swing.text.DefaultCaret)8 JTextComponent (javax.swing.text.JTextComponent)8 Point (java.awt.Point)6 BadLocationException (javax.swing.text.BadLocationException)5 Document (javax.swing.text.Document)5 JPopupMenu (javax.swing.JPopupMenu)4 KeyStroke (javax.swing.KeyStroke)3 FocusEvent (java.awt.event.FocusEvent)2 FocusListener (java.awt.event.FocusListener)2 ArrayList (java.util.ArrayList)2 BaseDocument (org.netbeans.editor.BaseDocument)2 MDoc (gov.sandia.n2a.db.MDoc)1 MNode (gov.sandia.n2a.db.MNode)1 Visitor (gov.sandia.n2a.db.MNode.Visitor)1 ParsedValue (gov.sandia.n2a.eqset.Variable.ParsedValue)1 Component (java.awt.Component)1 Font (java.awt.Font)1 FontMetrics (java.awt.FontMetrics)1 Rectangle (java.awt.Rectangle)1