2

I am looking to implement undo/redo file/folder functionality for edit/delete/move etc using java. Is that possible ?

Detail: I am watching a given folder using javaFileWatcher, i get a message from watcher that if any file/folder created/edited/deleted/renamed. now i want if any user has deleted/created any file/folder i would be able to undo it using java. can any one help.

Note: I am using windows

Thanks in advance.

NoNaMe
  • 6,020
  • 30
  • 82
  • 110

2 Answers2

3

Not possible, a delete is final, there is no recycle bin like in the OS, you have to implement it by yourself.

NoNaMe
  • 6,020
  • 30
  • 82
  • 110
Dima
  • 8,586
  • 4
  • 28
  • 57
1

As a kickoff idea have a look at the Command pattern. This is usually how undo/redo things are implemented. The idea is that you kind of store everything that is "important" about an operation into a single object and you store those in an ordered collection.

In your case the fact that you want to monitor the FS and not your own data model makes things really hard. However if you can afford redundant data storage then hypothetically it can be done I think. (I suggest you reading about the way git SCM works. I mean that it stores everything in a single .git folder and it is capable of restoring any previous state in its so called working directory from the compressed data it stores.)

Actually if you could involve git into your program then it would be much easier. I am thinking that you have a git repo initialized in the directory that you want to supervise and then every time that you are being notified about some FS change, you can ask git (git status) to tell you which files got changed actually. Even deleted file can be restored then using simple git commands.

This is far from a complete answer to your question however I might have given some ideas. Good luck.

NoNaMe
  • 6,020
  • 30
  • 82
  • 110
jabal
  • 11,987
  • 12
  • 51
  • 99
  • will git work with mac as well and can you please give some ref how to start with git, thanks for the nice idea – NoNaMe Apr 24 '13 at 11:22
  • Hi! git will surely work nicely on mac, linux, windows. Actually make sure that git is installed and then from your Java program you can call git commands and parse the results. See this for example: http://stackoverflow.com/questions/4157303/how-to-execute-cmd-commands-via-java If you are unfamiliar with git then choose a random git tutorial that google gives you. Basically you issue `git init` in the folder that you want to monitor, and then initially you have to `git add .` all the files and `git commit -m "initial state"` the first version. Then change something and see the `git status`.. – jabal Apr 24 '13 at 11:27