Application License $21.99

1 application Binary restricted distribution Commercial use allowed Can modify source Read full license | More Info

Developer License $87.99

Unlimited projects Source and binary distribution Commercial use allowed Can modify source 6 months support Read full license | More Info

You need to log-in or create an account
  • Create an account
  • Log-in
  • Please use your real name.
  • Account activation link will be sent to this address.
  • Minimum 8 characters

Clicking this button confirms you read and agreed to the terms of use and privacy policy.

Starting from $ 21.99

View Pricing 14 days money-back guarantee

Text Undo Manager

  • Developed by Ng Jackie, Released Aug 26, 2012

Flexible, concrete and natural undo-and-redo mechanism for your Swing text components.

Share and win up to $15k in prizes each month!Learn more

Share

Description

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.

Back to top

Features

  • Undoes edits block-by-block (which by instinct are deemed as one collective edit) based on typing intervals by default
  • Able to redefine the undo policy/ behaviour (i.e. how streams of characters are undone/ redone as one single edit unit) - two policy options are available: Typing-intervals and Word-boundaries
  • Able to override the undo policy on how deletions are remembered with one of these options: Deletion-series, Deletion-keystroke and None.
  • Able to enquiry the count of undo/ redo points created
  • Able to enquiry the nature/ description of each edit on the undo/ redo lists (e.g. get to know the characters to be undone/ redone next)
  • Able to set the limit of the number of edits to be remembered
  • Able to reset (discard) all of the undo/ redo points
  • Able to register the binding of keyboard shortcuts to the undo and redo operations with the associated JTextComponent
  • Automatically integrates the undo-and-redo functionality with the associated JTextComponent without the developer's intervention
Back to top

Usage Example

  • Creating a basic TextUndoManager for a JTextComponent instance

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);
    }

}
  • Customizing the TextUndoManager

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);
    }
}
Back to top

Installation

  1. Copy dist/TextUndoManager.jar and dist/lib to the root directory of your application project.
  2. For Eclipse, in Package Explorer right click on TextUndoManager.jar and select Build Path Add to Build Path. For Netbeans, in Project right-click on the project and select Properties. Go to Libraries, in Compile click Add JAR/ Folder and import TextUndoManager.jar.

Questions & Comments

Leave a comment

You must be logged-in to leave a comment.
Log-in now or register for a free account.
No comments have been posted yet.
You must be logged-in to vote. Log-in to your account or register now.

User Reviews

No reviews have been submitted yet.

Starting from $ 21.99

View Pricing 14 days money-back guarantee