Search in sources :

Example 6 with Caret

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

the class FocusCaret method makeTextArea.

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

        @Override
        public void updateUI() {
            setCaret(null);
            super.updateUI();
            if (flag) {
                Caret oldCaret = getCaret();
                int blinkRate = oldCaret.getBlinkRate();
                // int blinkRate = UIManager.getInt("TextField.caretBlinkRate")
                Caret caret = new FocusCaret();
                caret.setBlinkRate(blinkRate);
                setCaret(caret);
                caret.setSelectionVisible(true);
            }
        }
    };
    textArea.setText("aaa\nbbbbbb\ncccccccccccc\n");
    textArea.selectAll();
    return new JScrollPane(textArea);
}
Also used : Caret(javax.swing.text.Caret) DefaultCaret(javax.swing.text.DefaultCaret)

Example 7 with Caret

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

the class SizeFilter method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    JTextComponent target = getTextComponent(e);
    if (Objects.nonNull(target) && target.isEditable()) {
        Caret caret = target.getCaret();
        int dot = caret.getDot();
        int mark = caret.getMark();
        if (DefaultEditorKit.deletePrevCharAction.equals(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;
            }
        }
    }
    deleteAction.actionPerformed(e);
}
Also used : JTextComponent(javax.swing.text.JTextComponent) AbstractDocument(javax.swing.text.AbstractDocument) Document(javax.swing.text.Document) Caret(javax.swing.text.Caret)

Example 8 with Caret

use of javax.swing.text.Caret in project n2a by frothga.

the class PanelRun method viewJob.

public void viewJob(boolean refresh) {
    if (displayThread != null) {
        synchronized (displayText) {
            displayThread.stop = true;
            displayThread = null;
        }
    }
    StringBuilder contents = new StringBuilder();
    contents.append("Status:");
    NodeJob jobNode = (NodeJob) displayNode;
    if (jobNode.complete < 0)
        contents.append(" Waiting");
    else if (jobNode.complete == 0)
        contents.append(" Started");
    else if (jobNode.complete > 0 && jobNode.complete < 1)
        contents.append(" " + Math.round(jobNode.complete * 100) + "%");
    else if (jobNode.complete == 1)
        contents.append(" Success");
    else if (jobNode.complete == 3)
        contents.append(" Killed (lingering)");
    else if (jobNode.complete == 4)
        contents.append(" Killed");
    else
        // complete==2, or any value not specified above
        contents.append(" Failed");
    contents.append("\n");
    if (jobNode.dateStarted != null)
        contents.append("  started:  " + jobNode.dateStarted + "\n");
    if (jobNode.dateFinished != null)
        contents.append("  finished: " + jobNode.dateFinished + "\n");
    contents.append("\n");
    MNode job = jobNode.getSource();
    if (// job can be null if it is deleted while we are preparing this status text.
    job != null) {
        appendMetadata(job, contents, "backend");
        appendMetadata(job, contents, "duration");
        appendMetadata(job, contents, "host");
        appendMetadata(job, contents, "pid");
        appendMetadata(job, contents, "seed");
        contents.append("\n");
    }
    // Walk the model and display all overridden parameters.
    if (job != null && jobNode.hasSnapshot()) {
        // Obtain top-level model and collated model
        MNode doc;
        MNode model;
        String key = job.get("$inherit");
        Path localJobDir = Host.getJobDir(Host.getLocalResourceDir(), job);
        Path snapshotPath = localJobDir.resolve("snapshot");
        if (Files.exists(snapshotPath)) {
            MNode snapshot = new MDoc(snapshotPath);
            doc = snapshot.child(key);
            model = MPart.fromSnapshot(key, snapshot);
        } else {
            doc = AppData.models.childOrEmpty(key);
            model = new MDoc(localJobDir.resolve("model"), key);
        }
        doc.visit(new Visitor() {

            public boolean visit(MNode node) {
                List<String> keyList = Arrays.asList(node.keyPath(doc));
                List<String> paramPath = new ArrayList<String>(keyList);
                paramPath.add("$metadata");
                paramPath.add("param");
                Object[] paramArray = paramPath.toArray();
                // node is not a parameter
                if (!model.getFlag(paramArray))
                    return true;
                // watchable items aren't of interest for this summary
                if (model.get(paramArray).equals("watch"))
                    return true;
                String[] keyPath = keyList.toArray(new String[keyList.size()]);
                String key = keyPath[0];
                for (int i = 1; i < keyPath.length; i++) key += "." + keyPath[i];
                ParsedValue pv = new ParsedValue(model.get(keyPath));
                contents.append(key + " =" + pv.combiner + " " + pv.expression + "\n");
                if (// Could be multi-valued
                pv.expression.isEmpty()) {
                    for (MNode v : model.childOrEmpty(keyPath)) {
                        key = v.key();
                        if (key.contains("@"))
                            contents.append("\t" + v.get() + "\t" + key + "\n");
                    }
                }
                return true;
            }
        });
    }
    synchronized (displayText) {
        if (refresh) {
            Caret c = displayText.getCaret();
            int dot = c.getDot();
            int mark = c.getMark();
            Point magic = c.getMagicCaretPosition();
            if (magic == null)
                magic = new Point();
            Rectangle visible = displayPane.getViewport().getViewRect();
            if (// User has scrolled away from caret.
            !visible.contains(magic)) {
                // Scroll takes precedence over caret, so move caret back into visible area.
                Font f = displayText.getFont();
                FontMetrics fm = displayText.getFontMetrics(f);
                int h = fm.getHeight();
                int w = fm.getMaxAdvance();
                if (w < 0)
                    w = h / 2;
                h += h / 2;
                w += w / 2;
                magic.x = Math.max(magic.x, visible.x == 0 ? 0 : visible.x + w);
                magic.x = Math.min(magic.x, visible.x + visible.width - w);
                magic.y = Math.max(magic.y, visible.y == 0 ? 0 : visible.y + h);
                magic.y = Math.min(magic.y, visible.y + visible.height - h);
                dot = mark = displayText.viewToModel2D(magic);
            }
            displayText.setText(contents.toString());
            c.setDot(mark);
            if (dot != mark)
                c.moveDot(dot);
        } else {
            displayText.setText(contents.toString());
            displayText.setCaretPosition(0);
        }
    }
    displayChart.buttonBar.setVisible(false);
    if (displayPane.getViewport().getView() != displayText)
        displayPane.setViewportView(displayText);
    displayPane.repaint();
}
Also used : Path(java.nio.file.Path) TreePath(javax.swing.tree.TreePath) Visitor(gov.sandia.n2a.db.MNode.Visitor) Rectangle(java.awt.Rectangle) Point(java.awt.Point) MNode(gov.sandia.n2a.db.MNode) Point(java.awt.Point) Font(java.awt.Font) MDoc(gov.sandia.n2a.db.MDoc) ParsedValue(gov.sandia.n2a.eqset.Variable.ParsedValue) FontMetrics(java.awt.FontMetrics) List(java.util.List) ArrayList(java.util.ArrayList) Caret(javax.swing.text.Caret)

Example 9 with Caret

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

the class MatchesTextArea 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, activeMatch);
        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 10 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)

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