Search in sources :

Example 26 with Document

use of javax.swing.text.Document in project jabref by JabRef.

the class TextField method setupUndoRedo.

private void setupUndoRedo() {
    undo = new UndoManager();
    Document doc = getDocument();
    // Listen for undo and redo events
    doc.addUndoableEditListener(evt -> undo.addEdit(evt.getEdit()));
    // Create an undo action and add it to the text component
    getActionMap().put("Undo", new AbstractAction("Undo") {

        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException ignored) {
            // Ignored
            }
        }
    });
    // Bind the undo action to ctl-Z
    getInputMap().put(Globals.getKeyPrefs().getKey(org.jabref.gui.keyboard.KeyBinding.UNDO), "Undo");
    // Create a redo action and add it to the text component
    getActionMap().put("Redo", new AbstractAction(Actions.REDO) {

        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException ignored) {
            // Ignored
            }
        }
    });
    // Bind the redo action to ctl-Y
    getInputMap().put(Globals.getKeyPrefs().getKey(org.jabref.gui.keyboard.KeyBinding.REDO), "Redo");
}
Also used : UndoManager(javax.swing.undo.UndoManager) ActionEvent(java.awt.event.ActionEvent) CannotRedoException(javax.swing.undo.CannotRedoException) CannotUndoException(javax.swing.undo.CannotUndoException) Document(javax.swing.text.Document) AbstractAction(javax.swing.AbstractAction)

Example 27 with Document

use of javax.swing.text.Document in project jabref by JabRef.

the class JTextFieldChangeListenerUtil method addChangeListener.

/**
     * Installs a listener to receive notification when the text of any
     * {@code JTextComponent} is changed. Internally, it installs a
     * {@link DocumentListener} on the text component's {@link Document},
     * and a {@link PropertyChangeListener} on the text component to detect
     * if the {@code Document} itself is replaced.
     *
     * Taken from
     *
     * @param text any text component, such as a {@link JTextField}
     *        or {@link JTextArea}
     * @param changeListener a listener to receive {@link ChangeEvent}s
     *        when the text is changed; the source object for the events
     *        will be the text component
     * @throws NullPointerException if either parameter is null
     */
public static void addChangeListener(JTextComponent text, ChangeListener changeListener) {
    Objects.requireNonNull(text);
    Objects.requireNonNull(changeListener);
    DocumentListener dl = new DocumentListener() {

        private int lastChange;

        private int lastNotifiedChange;

        @Override
        public void insertUpdate(DocumentEvent e) {
            changedUpdate(e);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            changedUpdate(e);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            lastChange++;
            SwingUtilities.invokeLater(() -> {
                if (lastNotifiedChange != lastChange) {
                    lastNotifiedChange = lastChange;
                    changeListener.stateChanged(new ChangeEvent(text));
                }
            });
        }
    };
    text.addPropertyChangeListener("document", (PropertyChangeEvent e) -> {
        Document d1 = (Document) e.getOldValue();
        Document d2 = (Document) e.getNewValue();
        if (d1 != null) {
            d1.removeDocumentListener(dl);
        }
        if (d2 != null) {
            d2.addDocumentListener(dl);
        }
        dl.changedUpdate(null);
    });
    Document d = text.getDocument();
    if (d != null) {
        d.addDocumentListener(dl);
    }
}
Also used : DocumentListener(javax.swing.event.DocumentListener) PropertyChangeEvent(java.beans.PropertyChangeEvent) ChangeEvent(javax.swing.event.ChangeEvent) PropertyChangeEvent(java.beans.PropertyChangeEvent) DocumentEvent(javax.swing.event.DocumentEvent) Document(javax.swing.text.Document)

Example 28 with Document

use of javax.swing.text.Document in project jabref by JabRef.

the class JEditorPaneWithHighlighting method getDocumentText.

private String getDocumentText() {
    Document doc = getDocument();
    String text;
    try {
        text = doc.getText(0, doc.getLength());
    } catch (Exception e) {
        LOGGER.error("Error while getting document text");
        text = "";
    }
    return text;
}
Also used : Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 29 with Document

use of javax.swing.text.Document in project jabref by JabRef.

the class JTextAreaWithHighlighting method setupUndoRedo.

private void setupUndoRedo() {
    undo = new UndoManager();
    Document doc = getDocument();
    // Listen for undo and redo events
    doc.addUndoableEditListener(evt -> undo.addEdit(evt.getEdit()));
    // Create an undo action and add it to the text component
    getActionMap().put("Undo", new AbstractAction("Undo") {

        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException ignored) {
            // Ignored
            }
        }
    });
    // Bind the undo action to ctl-Z
    getInputMap().put(Globals.getKeyPrefs().getKey(org.jabref.gui.keyboard.KeyBinding.UNDO), "Undo");
    // Create a redo action and add it to the text component
    getActionMap().put("Redo", new AbstractAction(Actions.REDO) {

        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException ignored) {
            // Ignored
            }
        }
    });
    // Bind the redo action to ctrl-Y
    boolean bind = true;
    KeyStroke redoKey = Globals.getKeyPrefs().getKey(org.jabref.gui.keyboard.KeyBinding.REDO);
    if (Globals.prefs.getBoolean(JabRefPreferences.EDITOR_EMACS_KEYBINDINGS)) {
        // If emacs is enabled, check if we have a conflict at keys
        // If yes, do not bind
        // Typically, we have: CTRL+y is "yank" in emacs and REDO in 'normal' settings
        // The Emacs key bindings are stored in the keymap, not in the input map.
        Keymap keymap = getKeymap();
        KeyStroke[] keys = keymap.getBoundKeyStrokes();
        int i = 0;
        while ((i < keys.length) && !keys[i].equals(redoKey)) {
            i++;
        }
        if (i < keys.length) {
            // conflict found -> do not bind
            bind = false;
        }
    }
    if (bind) {
        getInputMap().put(redoKey, "Redo");
    }
}
Also used : UndoManager(javax.swing.undo.UndoManager) ActionEvent(java.awt.event.ActionEvent) CannotRedoException(javax.swing.undo.CannotRedoException) KeyStroke(javax.swing.KeyStroke) CannotUndoException(javax.swing.undo.CannotUndoException) Document(javax.swing.text.Document) AbstractAction(javax.swing.AbstractAction) Keymap(javax.swing.text.Keymap)

Example 30 with Document

use of javax.swing.text.Document in project gephi by gephi.

the class FileCompletionPopup method keyPressed.

/****** implementation of KeyListener of fileNameTextField ******/
@Override
public void keyPressed(KeyEvent e) {
    if (!isVisible()) {
        return;
    }
    int code = e.getKeyCode();
    switch(code) {
        case KeyEvent.VK_DOWN:
            setSelectNext();
            e.consume();
            break;
        case KeyEvent.VK_UP:
            setSelectPrevious();
            e.consume();
            break;
        case KeyEvent.VK_ESCAPE:
            setVisible(false);
            textField.requestFocus();
            e.consume();
            break;
    }
    if (isCompletionKey(code, textField)) {
        File file = (File) list.getSelectedValue();
        if (file != null) {
            if (file.equals(chooser.getCurrentDirectory())) {
                chooser.firePropertyChange(JFileChooser.DIRECTORY_CHANGED_PROPERTY, false, true);
            } else {
                chooser.setSelectedFiles(new File[] { file });
                chooser.setCurrentDirectory(file);
            }
            if (file.isDirectory()) {
                try {
                    Document doc = textField.getDocument();
                    doc.insertString(doc.getLength(), File.separator, null);
                } catch (BadLocationException ex) {
                    Logger.getLogger(getClass().getName()).log(Level.FINE, "Cannot append directory separator.", ex);
                }
            }
        }
        setVisible(false);
        textField.requestFocus();
        e.consume();
    }
}
Also used : Document(javax.swing.text.Document) File(java.io.File) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

Document (javax.swing.text.Document)64 BadLocationException (javax.swing.text.BadLocationException)29 DocumentEvent (javax.swing.event.DocumentEvent)10 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)8 DocumentAdapter (com.intellij.ui.DocumentAdapter)7 HTMLDocument (javax.swing.text.html.HTMLDocument)7 ActionEvent (java.awt.event.ActionEvent)5 PlainDocument (javax.swing.text.PlainDocument)5 ActionListener (java.awt.event.ActionListener)4 File (java.io.File)3 IOException (java.io.IOException)3 DocumentListener (javax.swing.event.DocumentListener)3 Element (javax.swing.text.Element)3 FieldPanel (com.intellij.ui.FieldPanel)2 CheckBox (com.intellij.util.ui.CheckBox)2 NumberFormat (java.text.NumberFormat)2 ParseException (java.text.ParseException)2 AbstractAction (javax.swing.AbstractAction)2 AbstractDocument (javax.swing.text.AbstractDocument)2 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)2