Undo Algorithm for Java Swing

Introduction

I am going to show how to perform undo algorithm on your Java Swing application. I hope before you tried to understand undo algorithm, understand how to create following simple text application as I am going to perform undo algorithm on it.
undo1
Here’s the Simple Text Application source code

package UndoSwingTutorialPackage;

 
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;



public class SimpleTextApplication extends JFrame{

     private static final long serialVersionUID = -4626161399740482146L;

     SimpleTextApplication(){
          super();
          JTextArea text = new initialTextField();
          
          setTitle("Simple Text Application");
          
          getContentPane().add(text);          
          
     }
     
     class initialTextField extends JTextArea{
       
          private static final long serialVersionUID = 1L;

          initialTextField(){
               super();
               
               initialShortcutKey(this);
               
               setColumns(40);
               setLineWrap(true);
               setRows(5);
               setEditable(true);
               
                              
          }
          
          void initialShortcutKey(JComponent component){
               
               /*
                * Never tried to use keyListener as it wouldn't work on component
                * For more detail on the reason of keyListener, please find the answer on your own
                * 
                * On swing, we have to specify which component is listen to key and which key
                * We have to use InputMap and ActionMap instead of KeyListener
                * 
                */

               
               InputMap im = component.getInputMap(JComponent.WHEN_FOCUSED);
               ActionMap am = component.getActionMap();
               
               /*
                * When the JTextArea is focused (Precisely JTextArea is focused all the times),
                * Component JTextArea is listened to space key by getKeyStroke( KeyEvent.VK_SPACE ,0 )  
                */

               im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0), "listenSpacebarKey");          
               am.put("listenSpacebarKey", new AbstractAction(){

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                         
                         
                    }                    
               });
               
               im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK), "listenCtrlzKey");          
               am.put("listenCtrlzKey", new AbstractAction(){

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                         
                         
                    }                    
               });                         
          }
     }
          
          
     public static void main(String[] args){
          JFrame frame = new SimpleTextApplication();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocation(250,250);
          frame.setVisible(true);
     }

}

I hope you wouldn’t get confused on this simple application. On JTextArea component, keyEvent indicate when spacebar is pressed. But you will found it nothing is happened, when spacebar is pressed, I blank it for the purpose of performing some undo action later on.

Long introduction is over and I hope you wouldn’t get confused as it is just beginning.

How to use it?

As what SimpleTextApplication shows, spacebar key and Ctrl+Z key is indicated as it is pressed. Before explain how to use it, tried the complete work and understand how complete work is worked. Following code is the complete work.

Create MyUndoWork class

package UndoSwingTutorialPackage;

import javax.swing.JTextArea; 

public class MyUndoWork {
     

     protected String[] memory;
     protected String tempMemory;
     protected int currentNumber = 0;
     private int max = 100;

          

     MyUndoWork(JTextArea textComponent){
          super();

          memory = new String[max];

          /*
           *  initial the first layer memory value
           *  If textComponent is found, put the text that exists on it at initial
           *  Else initial the memory with blank text "" 
           */

          if(textComponent != null){
               memory[0] = textComponent.getText();
               tempMemory = textComponent.getText();
          }else{
               memory[0] = "";
               tempMemory = "";
          }          
          
     }
     
     public void updateUndoMemory(String temp){
          
          /*
           * Prevent duplicate data is store into undo Memory
           */

          if(!tempMemory.equals(temp)){
               
               //Store previous data that store in tempMemory into memory
               storingDataForUndo(tempMemory);
               
               //update tempMemory into latest content
               tempMemory = temp;
          }
     }
     
     protected void storingDataForUndo(String value1){
          
          if(currentNumber < max ){                                /*                 * If there is no overflow of memory                 * Store the tempData into memory and                  */                memory[currentNumber] = value1;                currentNumber++;           }else if(currentNumber >= max){
               /*
                * If data stores into memory is overflow
                * Dispose the most bottom level memory and level down one of every memory
                * Put the new data into top level memory
                */

               for(int i = 0;i < max-1 ;i++){                     memory[i ]= memory[i + 1];                }                currentNumber = max-1;                memory[currentNumber] = value1;                currentNumber++;           }                }            public String releaseDataForUndo(){                      if(currentNumber > 0){
               currentNumber--;
               System.out.println("currentNumber : "+currentNumber);
                              
               /*
                * Once undo is pressed and memory is release
                * previous content that stored into tempMemory must be released 
                * and update it to latest current content
                *
                */

               
               tempMemory = memory[currentNumber];
               return memory[currentNumber];
               
          }else{
               currentNumber = 0;
               tempMemory = memory[currentNumber];
               return memory[currentNumber];
          }          
     }
}

Update SimpleTextApplication class source code as following

package UndoSwingTutorialPackage;



import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;


public class SimpleTextApplication extends JFrame{

     private static final long serialVersionUID = -4626161399740482146L;

     MyUndoWork myUndoWork;

     SimpleTextApplication(){
          super();
          JTextArea text = new initialTextField();
          
          setTitle("Simple Text Application");
          
          getContentPane().add(text);          
          
     }
     
     class initialTextField extends JTextArea{
          /**
           * 
           */

          private static final long serialVersionUID = 1L;

          initialTextField(){
               super();
               
               initialShortcutKey(this);
               
               /*
                * Implement MyUndoWork class and initial it.
                */

               myUndoWork = new MyUndoWork(this);
               
               setColumns(40);
               setLineWrap(true);
               setRows(5);
               setEditable(true);
               
                              
          }
          
          void initialShortcutKey(JComponent component){
               
               /*
                * Never tried to use keyListener as it wouldn't work on component
                * For more detail on the reason of keyListener, please find the answer on your own
                * 
                * On swing, we have to specify which component is listen to key and which key
                * We have to use InputMap and ActionMap instead of KeyListener
                * 
                */

               
               InputMap im = component.getInputMap(JComponent.WHEN_FOCUSED);
               ActionMap am = component.getActionMap();
               
               /*
                * When the JTextArea is focused (Precisely JTextArea is focused all the times),
                * Component JTextArea is listened to space key by getKeyStroke( KeyEvent.VK_SPACE ,0 )  
                */

               im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0), "listenSpacebarKey");          
               am.put("listenSpacebarKey", new AbstractAction(){

                    /**
                     * 
                     */

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                         
                         /*
                          * Undo memory is updated when user press space bar on JTextArea
                          *   
                          */

                         
                         myUndoWork.updateUndoMemory(getText());                         
                         
                    }                    
               });
               
               im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK), "listenCtrlzKey");          
               am.put("listenCtrlzKey", new AbstractAction(){

                    /**
                     * 
                     */

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                         /*
                          * update the undo content when user pressed Ctrl + Z key                          * 
                          */

                         setText(myUndoWork.releaseDataForUndo());
                         
                         
                    }                    
               });                         
          }
     }
          
          
     public static void main(String[] args){
          JFrame frame = new SimpleTextApplication();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocation(250,250);
          frame.setVisible(true);
     }

}

Tried it out how undo is work.

How to make it work?

  1. When the application is opened, MyUndoWork is implemented and initialized.
  2. Content is updated when user press spacebar key by getText() method.
  3. Content updated is retrieved and store it undo memory by declare updateUndoMemory() method.
  4. To update undo content when user press Ctrl + Z by declare releaseDataForUndo() method.

That’s all! You can perform undo work on your application by declare just only 2 method if implemented MyUndoWork class.

 

 

How Undo is work?

Predict user input “ B C D” into text area and “A” is exists on initial.

undo2

From the picture above, “A” letter is there when user is open the application After that, letter “B” is input and spacebar by user. Before content is changed, initial content “A” is store into memory. Again, “C” is input and spacebar. The content before “C” is input, ”A B “ is store into memory. Again, “D” is input and spacebar. The content before “D” is input, content “A B C” is retrieved and store into memory.

When user press shortcut key Ctrl + Z to perform undo for three times return to initial state “A”. Pictures following shows how it works.

undo3

Ctrl+Z is the shortcut key of undo. When shortcut key is pressed, the top layer of the memory is released into content. For instances, current content “A B C D” is input and Ctrl+Z  is pressed, the previous top layer memory “A B C” is released into content. When the top layer memory”A B C” is released into content, current content “A B C D” is replaced by top layer memory. So, what user will see just only the content of top layer memory.

Again, Ctrl + Z is pressed and “A B” is released into content and the current content “A B C” is replaced by it.

This is how my undo is work.

Thank you for reading my article.

Leave a comment