开发者

Implements an Undo/Redo in MVC

I have a Java application and I want to implement an Undo/Redo option. the value that I want to stock and that I want to be able to recover is an integer.

My Class Model implements the interface StateEditable and I have to redefine the 2 functions restoreState(Hashtable<?, ?> state) and storeState(Hashtable<Object, Object> state) but I don't know what to put on them. It will be really great if somebody can help me to do that. These are the first lines of my Model class, the value that I want to do an undo/redo on it is value

public class Model extends Observable implements St开发者_运维知识库ateEditable
{
        private int value = 5;

        private UndoManager undoRedo = new UndoManager();

        final UndoableEditListener editListener = new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undoRedo.addEdit(evt.getEdit());
            }
        };

        @Override
        public void restoreState(Hashtable<?, ?> state)
        {

        }

        @Override
        public void storeState(Hashtable<Object, Object> state)
        {

        }
}


From looking through an example of StateEditable, it would appear that in your storeState method, you need to populate the Hashtable that is passed in. Similarly, you assign state in your restoreState from the Hashtable that is passed in. You will need to define a key for the value in the Hashtable. With that in mind, I suggest that you add

private final String KEY = "myKey";

to the top of your file, and then fill out the two methods like so:

    @Override
    public void restoreState(Hashtable<?, ?> state) 
    {
       Object val = state.get(KEY);
       if( val instanceof Integer ) //performs the null test for us.
       {
          value = val;
       }
    }

    @Override
    public void storeState(Hashtable<Object, Object> state) 
    {
        state.put(KEY, value);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜