Example usage for javax.swing.text DefaultCaret getUpdatePolicy

List of usage examples for javax.swing.text DefaultCaret getUpdatePolicy

Introduction

In this page you can find the example usage for javax.swing.text DefaultCaret getUpdatePolicy.

Prototype

public int getUpdatePolicy() 

Source Link

Document

Gets the caret movement policy on document updates.

Usage

From source file:Main.java

/**
 * sets text of textComp without moving its caret.
 *
 * @param textComp  text component whose text needs to be set
 * @param text      text to be set. null will be treated as empty string
 *///  w  w w .j  a v  a2 s . c  om
public static void setText(JTextComponent textComp, String text) {
    if (text == null)
        text = "";

    if (textComp.getCaret() instanceof DefaultCaret) {
        DefaultCaret caret = (DefaultCaret) textComp.getCaret();
        int updatePolicy = caret.getUpdatePolicy();
        caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
        try {
            textComp.setText(text);
        } finally {
            caret.setUpdatePolicy(updatePolicy);
        }
    } else {
        int mark = textComp.getCaret().getMark();
        int dot = textComp.getCaretPosition();
        try {
            textComp.setText(text);
        } finally {
            int len = textComp.getDocument().getLength();
            if (mark > len)
                mark = len;
            if (dot > len)
                dot = len;
            textComp.setCaretPosition(mark);
            if (dot != mark)
                textComp.moveCaretPosition(dot);
        }
    }
}

From source file:processing.app.EditorTab.java

/**
 * Replace the entire contents of this tab.
 *//*from   ww w  . ja v  a2s  .co m*/
public void setText(String what) {
    // Remove all highlights, since these will all end up at the start of the
    // text otherwise. Preserving them is tricky, so better just remove them.
    textarea.removeAllLineHighlights();
    // Set the caret update policy to NEVER_UPDATE while completely replacing
    // the current text. Normally, the caret tracks inserts and deletions, but
    // replacing the entire text will always make the caret end up at the end,
    // which isn't really useful. With NEVER_UPDATE, the caret will just keep
    // its absolute position (number of characters from the start), which isn't
    // always perfect, but the best we can do without making a diff of the old
    // and new text and some guesswork.
    // Note that we cannot use textarea.setText() here, since that first removes
    // text and then inserts the new text. Even with NEVER_UPDATE, the caret
    // always makes sure to stay valid, so first removing all text makes it
    // reset to 0. Also note that simply saving and restoring the caret position
    // will work, but then the scroll position might change in response to the
    // caret position.
    DefaultCaret caret = (DefaultCaret) textarea.getCaret();
    int policy = caret.getUpdatePolicy();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
    try {
        Document doc = textarea.getDocument();
        int oldLength = doc.getLength();
        // The undo manager already seems to group the insert and remove together
        // automatically, but better be explicit about it.
        textarea.beginAtomicEdit();
        try {
            doc.insertString(oldLength, what, null);
            doc.remove(0, oldLength);
        } catch (BadLocationException e) {
            System.err.println("Unexpected failure replacing text");
        } finally {
            textarea.endAtomicEdit();
        }
    } finally {
        caret.setUpdatePolicy(policy);
    }
}