Example usage for javax.swing.text JTextComponent getDocument

List of usage examples for javax.swing.text JTextComponent getDocument

Introduction

In this page you can find the example usage for javax.swing.text JTextComponent getDocument.

Prototype

public Document getDocument() 

Source Link

Document

Fetches the model associated with the editor.

Usage

From source file:Main.java

/**
 * Method getCaretRow.// ww  w  .j ava2 s  .c  o  m
 * 
 * @param idx
 * @param comp
 * @return int
 * @throws Exception
 */
public static int getCaretRow(int idx, JTextComponent comp) throws BadLocationException {

    return getLineOfOffset(idx, comp.getDocument());
}

From source file:TextFieldViews.java

public static void displayViews(JTextComponent comp, PrintStream out) {
    View rootView = comp.getUI().getRootView(comp);
    displayView(rootView, 0, comp.getDocument(), out);
}

From source file:Main.java

public static void addTextUpdateListener(JTextComponent textComponent, Consumer<DocumentEvent> listener) {
    Document doc = textComponent.getDocument();
    doc.addDocumentListener(new DocumentListener() {
        @Override//from www . j  a v  a  2  s  .  c  om
        public void removeUpdate(DocumentEvent e) {
            listener.accept(e);
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            listener.accept(e);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            listener.accept(e);
        }
    });
}

From source file:Main.java

public static void bindUndoManager(final JTextComponent text, final UndoManager undo) {

    text.getDocument().addUndoableEditListener(new UndoableEditListener() {
        @Override//from w w  w  . jav a 2s.c o m
        public void undoableEditHappened(UndoableEditEvent e) {
            undo.addEdit(e.getEdit());
        }
    });

    text.getActionMap().put("Undo", new AbstractAction("Undo") {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });
    // Bind the undo action to ctl-Z
    text.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    // Create a redo action and add it to the text component
    text.getActionMap().put("Redo", new AbstractAction("Redo") {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException e) {
            }
        }
    });
    // Bind the redo action to ctl-Y 
    text.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
}

From source file:Main.java

public static void highlight(JTextComponent textComp, String pattern) throws Exception {
    removeHighlights(textComp);//ww w .  j  a va2s .com

    Highlighter hilite = textComp.getHighlighter();
    Document doc = textComp.getDocument();
    String text = doc.getText(0, doc.getLength());
    int pos = 0;

    while ((pos = text.indexOf(pattern, pos)) >= 0) {
        hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
        pos += pattern.length();
    }

}

From source file:ShowHTMLViews.java

public static void displayModel(JTextComponent comp, PrintStream out) {
    Document doc = comp.getDocument();
    Element e = doc.getDefaultRootElement();
    displayElement(doc, e, 0, out);//from  w  w w  .  j av  a  2 s  .  com
}

From source file:Main.java

@SuppressWarnings("serial")
public static void installUndoManager(JTextComponent textComponent, final UndoManager undoManager) {

    Document doc = textComponent.getDocument();
    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent e) {
            undoManager.addEdit(e.getEdit());
        }//from w  ww . j  a v  a2  s .  c  o m
    });

    ActionMap am = textComponent.getActionMap();
    InputMap im = textComponent.getInputMap();
    am.put("undo", new AbstractAction("undo") {
        @Override
        public void actionPerformed(ActionEvent e) {
            undoManager.undo();
        }

        @Override
        public boolean isEnabled() {
            return undoManager.canUndo();
        }
    });
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, getMenuShortcutKeyMask()), "undo");

    am.put("redo", new AbstractAction("redo") {
        @Override
        public void actionPerformed(ActionEvent e) {
            undoManager.redo();
        }

        @Override
        public boolean isEnabled() {
            return undoManager.canRedo();
        }
    });
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, getMenuShortcutKeyMask()), "redo");
}

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  ww . jav a  2 s.c  o m*/
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:InsertAction.java

public void actionPerformed(ActionEvent evt) {
    JTextComponent c = (JTextComponent) evt.getSource();

    try {/*from w w w  .  j av a 2s .c  o  m*/
        c.getDocument().insertString(c.getCaretPosition(), " space", null);
    } catch (BadLocationException e) {
    }
}

From source file:Main.java

public void actionPerformed(ActionEvent evt) {
    JTextComponent c = (JTextComponent) evt.getSource();

    try {// w w  w .  j  a v a2s. c  o  m
        c.getDocument().insertString(c.getCaretPosition(), " ", null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}