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");
}
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);
}
}
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;
}
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");
}
}
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();
}
}
Aggregations