Pergunta

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 StateEditable
{
        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)
        {

        }
}
Foi útil?

Solução

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);
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top