Search in sources :

Example 16 with Caret

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

the class TextAreaHandler method fire.

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;
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent) Caret(javax.swing.text.Caret)

Example 17 with Caret

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

the class LineCursorTextArea method updateUI.

// public LineCursorTextArea(String text, int rows, int columns) {
// super(text, rows, columns);
// }
@Override
public void updateUI() {
    super.updateUI();
    Caret caret = new DefaultCaret() {

        // [UnsynchronizedOverridesSynchronized]
        // Unsynchronized method damage overrides synchronized method in DefaultCaret
        @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
        @Override
        protected synchronized void damage(Rectangle r) {
            if (Objects.nonNull(r)) {
                JTextComponent c = getComponent();
                x = 0;
                y = r.y;
                width = c.getSize().width;
                height = r.height;
                c.repaint();
            }
        }
    };
    // caret.setBlinkRate(getCaret().getBlinkRate());
    caret.setBlinkRate(UIManager.getInt("TextArea.caretBlinkRate"));
    setCaret(caret);
}
Also used : DefaultCaret(javax.swing.text.DefaultCaret) JTextComponent(javax.swing.text.JTextComponent) Caret(javax.swing.text.Caret) DefaultCaret(javax.swing.text.DefaultCaret)

Example 18 with Caret

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

the class LineCursorTextArea method paintComponent.

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Caret c = getCaret();
    if (c instanceof DefaultCaret) {
        Graphics2D g2 = (Graphics2D) g.create();
        Rectangle r = SwingUtilities.calculateInnerArea(this, null);
        DefaultCaret caret = (DefaultCaret) c;
        int y = caret.y + caret.height - 1;
        g2.setPaint(LINE_COLOR);
        g2.drawLine(r.x, y, (int) r.getMaxX(), y);
        g2.dispose();
    }
}
Also used : DefaultCaret(javax.swing.text.DefaultCaret) Caret(javax.swing.text.Caret) DefaultCaret(javax.swing.text.DefaultCaret)

Example 19 with Caret

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

the class HighlightCursorTextArea method updateUI.

@Override
public void updateUI() {
    super.updateUI();
    setOpaque(false);
    Caret caret = new DefaultCaret() {

        // [UnsynchronizedOverridesSynchronized]
        // Unsynchronized method damage overrides synchronized method in DefaultCaret
        @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
        @Override
        protected synchronized void damage(Rectangle r) {
            if (Objects.nonNull(r)) {
                JTextComponent c = getComponent();
                x = 0;
                y = r.y;
                width = c.getSize().width;
                height = r.height;
                c.repaint();
            }
        }
    };
    // caret.setBlinkRate(getCaret().getBlinkRate());
    caret.setBlinkRate(UIManager.getInt("TextArea.caretBlinkRate"));
    setCaret(caret);
}
Also used : DefaultCaret(javax.swing.text.DefaultCaret) JTextComponent(javax.swing.text.JTextComponent) Caret(javax.swing.text.Caret) DefaultCaret(javax.swing.text.DefaultCaret)

Example 20 with Caret

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

the class HighlightCursorTextArea method paintComponent.

@Override
protected void paintComponent(Graphics g) {
    Caret c = getCaret();
    if (c instanceof DefaultCaret) {
        Graphics2D g2 = (Graphics2D) g.create();
        DefaultCaret caret = (DefaultCaret) c;
        Rectangle r = SwingUtilities.calculateInnerArea(this, rect);
        r.y = caret.y;
        r.height = caret.height;
        g2.setPaint(LINE_COLOR);
        g2.fill(r);
        g2.dispose();
    }
    super.paintComponent(g);
}
Also used : DefaultCaret(javax.swing.text.DefaultCaret) 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