Draw beautiful diagram effortlessly on ipad. Click here to know more..

Sunday, April 1, 2012

How to implement UndoManager (with example in Java on Android)

Undo-redo is a must-to-have feature in editing programs. This blog post describes how to implement undo-redo in your program. The example taken here is in Java but the concept is very simple and can be implemented easily in any language. 
Here is idea to implement the undo-redo. First of all you need these: UndoManager - a class that implements undo-redo features. UserAction - an abstract base class for all user actions.
The operations in the UndoManager are: undo, redo, canUndo, canRedo, addAction And the operations in UserAction are: perform, undo
As said above, the UserAction abstracts user actions which user can undo. For example in text editing program, inserting a character at a particular cursor location is an action which user can undo. So you need to implement this abstract class for all type of user's undo-able actions. 
Lets start with some real code. The code here is taken from an Android app "Scratchpad" 
This is a simple drawing program using touch. User can draw on the screen using touch dragging on Android tablet screen. So the user action,in this case, is draw a stroke on canvas. A stroke is defined as the drawing done between users actions of starting touch drag and stopping touch drag. Here is UndoManager class:
import java.util.*;
public class UndoManager {
 private UndoManager(){
  undoActions = new Stack();
  redoActions = new Stack();
 }
 public boolean canUndo(){
  return undoActions.size() > 0;
 }
 public boolean canRedo(){
  return redoActions.size() > 0;
 }
 public void undo(){
  if(canUndo()){
   UserAction act = this.undoActions.pop();
   act.undo();
   redoActions.push(act);
  }
 }
 public void redo(){
  if(canRedo()){
   UserAction act = this.redoActions.pop();
   act.perform();
   undoActions.add(act);
  }
 }
 public void add(UserAction action){
  this.undoActions.push(action);
  this.redoActions.clear();
 }
 private Stack undoActions;
 private Stack redoActions;
}

The code for UserAction
public abstract class UserAction {
 public abstract void perform();
 public abstract void undo();
}
Here is code for StrokeAction
public class StrokeAction extends UserAction{
 public StrokeAction(DrawingStroke ds){
  this.stroke = ds;
 }
 @Override
 public void perform(){
  ObjectStore.instance.addObject(stroke);
 }
 @Override
 public void undo(){
  ObjectStore.instance.removeObject(stroke);
 }
 private DrawingStroke stroke;
}

Whenever the user completes a drawing stroke in Scratchpad, an object of DrawingAction is created. The ObjectManager in the code above contains the all stroke objects. Whenever user performs a drawing, we add the stroke to object manager. The drawing of all available objects are managed ObjectManager. And when user wants to undo, we simply remove the stroke from object manager. For simplicity and readability, I am not adding the code of ObjectManager here. 

The code of UndoManager and UserAction is very generic and can be used any application. UserActions has to be implemented for for each undo-able user action type. This implementation will be application dependent.

34 comments:


  1. Hi, This is Jamuna from Chennai. I have read your blog and I got some knowledgeable information through this blog. Really useful blog. Keep update your blog.

    Regards...
    Java Training Chennai

    ReplyDelete
  2. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  3. Java Training Institutes Java Training Institutes
    Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

    Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

    ReplyDelete
  4. Thank You for sharing your article. I like it. It is great information about Java. We provides TIBCO Online Training in Hyderabad.

    ReplyDelete
  5. Thanks for taking time to share this post.It is really useful.Continue sharing more like this.
    Regards, Java Training in Chennai

    ReplyDelete
  6. I have completely read your post and the content is crisp and clear.Thank you for posting such an informative article, I have decided to follow your blog so that I can myself updated.
    Java Training in Chennai

    ReplyDelete
  7. I completely read your blog.It is very useful to me .I have imporve my coading knowledge. Thanks for sharing. Keep sharing more blogs.


    Software Testing Training in chennai

    ReplyDelete
  8. I found your blog while searching for the updates in Selenium WebDriver,I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about selenium tutorials for beginners with reference of your blog.

    ReplyDelete
  9. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Java Training in Bangalore

    ReplyDelete
  10. I used to write critical essays about these platforms using useful info about writing critical analysis essays and I must say that I was surprised that these system have such high security level.

    ReplyDelete
  11. Thanks for sharing wonderful information which helps all audience

    devOps Training | devOps supports

    ReplyDelete
  12. Extraordinary occupation for distributed such a gainful site. Your web log isn't just helpful however it is furthermore truly innovative as well. There have a tendency to be relatively few individuals who can surely compose not all that straightforward posts that imaginatively. Proceed with the decent written work Tax Enquiry Accounts

    ReplyDelete
  13. I have read your blog and I gathered some needful information from your blog. Keep update your blog. Waiting for your next update. Java Training in Chennai | Python Training in Chennai

    ReplyDelete
  14. This information is impressive; I am inspired by your post writing style & how continuously you describe this topic.... Java Training in Chennai | Blue prism Training in Chennai

    ReplyDelete
  15. Much obliged to you again for all the information you distribute,Good post. I was exceptionally keen on the article, it's entirely rousing I ought to concede. I like going to you site since I generally run over fascinating articles like this one.Great Job, I incredibly acknowledge that.Do Keep sharing! Respects, HMRC Tax Investigation Accountants

    ReplyDelete
  16. I"ve been surfing online over three hours today, yet I never discovered any fascinating article like yours. It"s really sufficiently worth for me. As I would like to think, if all website admins and bloggers made great substance as you did, the web will be significantly more valuable than any other time in recent memory. Tax Enquiry

    ReplyDelete
  17. On the off chance that more individuals that compose articles truly fretted about composing incredible substance like you, more perusers would be occupied with their works. Much obliged to you for thinking about your substance. Let Campaign Tax Enquiry

    ReplyDelete
  18. i do a lot of article submissions in a week or two and i hire some writers from india and other countries to do my stuf,. Tax Investigation Self Employed

    ReplyDelete
  19. Extraordinary occupation for distributed such a gainful site. Your web log isn't just helpful however it is furthermore truly innovative as well. There have a tendency to be relatively few individuals who can surely compose not all that straightforward posts that imaginatively. Proceed with the decent written work Let Campaign Tax Enquiry Accountants

    ReplyDelete
  20. Hey! A debt of gratitude is in order for the colossal data you havr gave! You have touched on crucuial focuses! Let Property Campaign

    ReplyDelete
  21. I am upbeat to discover this post exceptionally helpful for me, as it contains part of data. I generally want to peruse the quality substance and this thing I found in you post. A debt of gratitude is in order for sharing. Let Property Campaign Accountants

    ReplyDelete
  22. Thank you for sharing your article. Great efforts put it to find the list of articles which is very useful to know, Definitely will share the same to other forums.
    Data Science Training in chennai at Credo Systemz | data science course fees in chennai | data science course in chennai quora | data science with python training in chennai

    ReplyDelete
  23. Thanks for sharing this post.Keep sharing more like this.

    dgreatwallofchina
    Education

    ReplyDelete
  24. Thanks for the information, Get the best Java Training in Chennai from hope tutors.

    ReplyDelete
  25. Amazing information,thank you for your ideas.after along time i have studied
    an interesting information's.we need more updates in your blog.
    Selenium Training in Amjikarai
    Selenium Training Institutes in Vadapalani
    Selenium training near me
    Selenium Training in Chennai

    ReplyDelete