I was working on a compiler using java
every thing is OK but how to implement the functions of undo/redo using stacks.
thanks
Printable View
I was working on a compiler using java
every thing is OK but how to implement the functions of undo/redo using stacks.
thanks
I havent used stacks that much. But i think you would want to do somthing like the following
Code:class stack{
private Object[] stackarray;
private int topofstack;
private static int counter;
public stack(int capacity){
stackarray = new Object[capacity];
topofstack = -1;
counter++;
}
public void push(Object element){stackarray[topofstack++] = element;}
public Object pop(){return stackarray[topofstack--];}
pubilc boolean isEmpty(){return topofstack < 0;}
public boolean isFull(){return topofstack == stackarray.length - 1;}
}
public static getInstanceCount(){return counter;}
}
There is already a
java.util.Stack
I would guess that you would need two Stack objects undo and redo. Consider the "Back" and "Forward" buttons in a browser. They are even more visible than say a word processor's undo/redo. You can see which website you'd be going to in the "Back" and "Forward" buttons in a browser.
I don't know what you want to track for undo/redo, but say you are tracking commands. You may need to decide when to put a command on the "undo stack" -- push (before or after completion of the command). Then whenever you go Back "Back button" -- pop the "undo stack", you push on the "redo stack" "Forward button".
Basically, push commands on the undo stack. Then a pop on the undo stack is synchronized with a push on the redo stack and a pop on the redo stack is synchronized with a push on the undo stack.
java.util.Stack undo;
java.util.Stack redo;
I was thinking of subclassing your own undo and redo stacks so that they are both Observable and implement Observer, but you should know when an attempt to undo/redo occurs.
Depending on what you are tracking, you will need to have a kind of inverse to every command. For instance, if you want to undo a compile, then you may have already overwritten your last successfully compiled file.
I actually implemented a RedoUndo class recently. I found it advantageous to extend the Vector class since Vector has much of the functions you need to implement a RedoUndo stack. I created a RedoUndoItem class that had the following properties or instance variables:
char status;
String timeStamp;
String key; // I was storing multiple types of information in my RedoUndo list that I retrieved by key
String value; // The actual data
Then I created methods like do() which did a push, undo() which did a non-destructive pop--you don't want to really delete anything when you undo otherwise you can't redo, instead you set a status property to 'U' for undone, and, of course a redo() function which examined the status values and turned the oldest 'U', undone item to 'A', active. The do() function had an additional effect: it deleted all of the existing status = 'U' entries for a given key. That's because when you start 'doing' things, previous undone elements no longer have meaning. This prevents things like drawing a line and then pressing 'redo' for example. The getActiveKey() function would walk backwards through the stack and find the first 'A' active entry for a given key. Since these redo/undo stacks are fairly small, I wasn't too concerned about doing a sequential search like this--it was quick enough.
The reason why I kept a timeStamp is sometimes I needed to store a group of items or entries as a single action that needed to be undone or redone as a whole. I assigned the group the same timestamp and retrieved the items as an array of RedoUndoItem.
Yeah, I know, this is kinda unreadable. If you have any questions, I'll be glad to answer them.
cudabean
ok
what should I push into the stack
should I push the string or its location..
forget the redo command
I want to implement the undo command only.
how to use the stack for undo operation with a JTextArea ?
thanx