Home
1 application Binary restricted distribution Commercial use allowed Can modify source Read full license | More Info
Unlimited projects Source and binary distribution Commercial use allowed Can modify source 6 months support Read full license | More Info
Starting from $ 21.99
TextUndoManager is a library that provides natural undo-and-redo functionality for your Swing text components such as JTextField, JTextArea and JTextPane. It undoes blocks of character-based edits as a single edit unit at a time using a set of algorithms.
The library comes with a number of useful functions that allow you to configure the undo-and-redo features. You can easily create a ready-to-use undo-and-redo mechanism for your text components with just a few lines of code and there's no further implementation work required on the developer's side.
For the convenience of developers, the library package is fully Javadoc'ed.
import java.awt.BorderLayout;
import javax.swing.*;
import ngjmy.undo.TextUndoManager;
public class BasicExample extends JFrame {
public BasicExample() {
JTextArea textArea = new JTextArea();
// Creates a TextUndoManager with an edit storage capacity of 200 which integrates with textArea
TextUndoManager undoManager = new TextUndoManager(textArea, 200);
// Registers the default undo key (Ctrl+Z) and redo key (Ctrl+Y) with the text component
undoManager.registerDefaultKeysMap();
add(textArea, BorderLayout.CENTER);
}
/** Creates and displays the JFrame */
public static void main(String[] args) {
JFrame frame = new BasicExample();
frame.setTitle("Basic Example");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import ngjmy.undo.*;
import ngjmy.undo.TextUndoManager.MenuItemFactory;
public class AnotherExample extends JFrame {
private JLabel label;
private JTextArea textArea;
private TextUndoManager undoManager;
public AnotherExample() {
label = new JLabel(" ");
textArea = new JTextArea();
// Creates a TextUndoManager with a default edit storage capacity of 100 which integrates with textArea
undoManager = new TextUndoManager(textArea);
// Undoes edits based on word boundaries, i.e. a word at a time
undoManager.setUndoPolicy(UndoPolicy.WORD_BOUNDARIES);
// Undoes a deletion entry at a time
undoManager.setDeletionPattern(DeletionPattern.DELETION_KEYSTROKE);
// Registers the undo key (Ctrl+Z) with the text component
undoManager.registerUndoKeyMap(KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK));
// Registers the redo key (Ctrl+X) with the text component
undoManager.registerRedoKeyMap(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_DOWN_MASK));
// Creates a popup menu
JPopupMenu popupMenu = new JPopupMenu();
// Gets the menu item factory for undoManager
MenuItemFactory menuItemFactory = undoManager.getMenuItemFactory();
// Creates a ready-made undo menu item using the factory and adds it to the popup menu
popupMenu.add(menuItemFactory.createUndoMenuItem());
// Creates a ready-made redo menu item and adds it to the popup menu
popupMenu.add(menuItemFactory.createRedoMenuItem());
// Creates a listener that listens for undo and redo events from undoManager
undoManager.addTextUndoListener(new TextUndoListener() {
@Override
public void undoPerformed(BasicEdit edit) {
updateLabelText(edit, "Undo");
}
@Override
public void redoPerformed(BasicEdit edit) {
updateLabelText(edit, "Redo");
}
});
// Associates the popup menu with textArea
textArea.setComponentPopupMenu(popupMenu);
add(textArea, BorderLayout.CENTER);
add(label, BorderLayout.SOUTH);
}
private void updateLabelText(BasicEdit edit, String type) {
label.setText("Last Operation: " + type + " " + edit.getType() + " " + edit.getContent() + ".");
}
/** Creates and displays the JFrame */
public static void main(String[] args) {
JFrame frame = new AnotherExample();
frame.setTitle("Another Example");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Starting from $ 21.99
Questions & Comments
Leave a comment
Log-in now or register for a free account.