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.

62 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

  26. Thank you for taking the time to write about this much needed subject. I felt that your remarks on this technology is helpful and were especially timely.

    Right now, DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.

    devops course fees in chennai | devops training in chennai with placement | devops training in chennai omr | best devops training in chennai quora | devops foundation certification chennai

    ReplyDelete
  27. I accept there are numerous more pleasurable open doors ahead for people that took a gander at your site.we are providing ReactJs training in Chennai.
    For more details: ReactJs training in Velachery | ReactJs training in chennai

    ReplyDelete
  28. This is the kind of data I've long been attempting to discover. Much obliged to you for composing this data. Buy to Let Property Tax Advisors

    ReplyDelete
  29. That is truly decent to listen. much obliged to you for the upgrade and good fortunes. Buy to Let Property Tax Advice

    ReplyDelete
  30. I am happy you take pride in what you compose. This makes you stand way out from numerous different authors that push ineffectively composed substance. high DA backlinks

    ReplyDelete
  31. Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
    organic oil
    organic oil in jaipur
    organic cold pressed oils
    ayurvedic oil store in jaipur

    ReplyDelete
  32. Is it alright to post some portion of this on my site essentially present a hyperlink on this page? high DA backlinks

    ReplyDelete
  33. Hey Your site is awesome and full of information. I have read you posts they are so informative. Keep Posting wonderful content.
    Ani international provide the security solutions for all kind of secruity system and other equipment.
    Home security system in jaipur
    Wireless Home Security System in jaipur
    Realtime attendance machine in jaipur
    CCTV Camera dealer in jaipur
    Hikvision DVR in jaipur at Rajasthan
    security system solutions in jaipur
    website design in jaipur
    website development company in jaipur
    seo company in jaipur

    ReplyDelete
  34. Incredible post I might want to thank you for the endeavors you have made in composing this fascinating and learned article. Buy to Let Tax Advisor

    ReplyDelete
  35. plz any one can help with the code how to add undo redo function using bitmap

    ReplyDelete
  36. Much obliged to you for some other educational web journal. Where else would I be able to understand that sort of data written in such a perfect means? I have a mission that I'm a little while ago chipping away at, and I have been at the post for such data. Buy to Let Property Tax Advice London

    ReplyDelete
  37. Your site is truly cool and this is an awesome rousing article. Much obliged to you to such an extent. Property Tax Accountant

    ReplyDelete
  38. This is really the sort of data I have been attempting to discover. Much thanks to you for composing this data. Property Tax Advice

    ReplyDelete
  39. Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
    organic oil
    organic oil in jaipur
    organic cold pressed oils
    ayurvedic oil store in jaipur

    ReplyDelete
  40. I have perused your web journal it is exceptionally useful for me. I need to express profound gratitude to you. I have bookmark your site for future redesigns. seo services

    ReplyDelete
  41. A debt of gratitude is in order For sharing this Superb article.I utilize this Article to demonstrate my task in college.it is helpful For me Great Work. Property Tax Advisors

    ReplyDelete
  42. Awesome occupation for distributed such a gainful site. Your web log isn't just valuable yet it is also truly innovative as well. There have a tendency to be very few individuals who can absolutely compose not all that basic posts that creatively. Proceed with the decent written work blog comments backlinks

    ReplyDelete
  43. I cherish the way you compose and share your corner! Exceptionally intriguing and distinctive! Keep it coming! Tax on Buy to Let Property

    ReplyDelete
  44. I adore this online journal!! The glimmer up the top is wonderful!! Tax Investigation Accountants

    ReplyDelete
  45. Fantastic article. Exceptionally fascinating to peruse. I truly love to peruse such a decent article. Much obliged! continue shaking. Tax Investigation Advisors

    ReplyDelete