> insert 23 "The cat in the hat ate a rat."
As you might infer, the intended effect of this command is to insert, at line 23 of the document, the quoted text, meaning that this text would come to occupy line 23 while the text that had been in lines 23, 24, etc., etc., would become lines 24, 25, etc., etc.
As you would expect, in addition to an insert command, such editing tools supported commands such as delete (to delete a line) and replace (which, in effect, is a deletion of a line followed by an insertion of a line at the same place).
Other commonly supported commands included move and copy, which have the obvious interpretations. Among these, the delete, move, and copy commands often had versions that applied to a block of lines (as opposed to a single line), so that, for example, the command delete 20 24 would indicate the user's intent to delete four lines of text starting at line 20 (i.e., those whose locations were in the range [20..24)).
Some CLI editors of that time supported what was then considered an advanced feature: undo. This meta-command allowed the user to reverse the effects of one or more of the most recently executed edit commands. Going even one step further was the redo meta-command, which "undoes" the effects of the undo command by "re-doing" the command that was most recently "undone"!1
To insert, remove, and replace lines within a document is straightforward, given a Java class that implements the Document interface (which, you will notice, dictates the presence of insertLine(), replaceLine(), and deleteLine() methods). Such a class has been provided.
What makes this assignment interesting, and nontrivial, is the requirement that the implemented CLI editor supports the undo and redo meta-commands.
Our premise is that the current state of a document (i.e., its contents) is determined by its initial state (i.e., as it existed when editing began) and the modifications resulting from the sequence of edit commands that were applied to it since then. (Thus, a document could be reconstructed exactly if known are both its initial state and that sequence of edit commands.)
The idea of the undo meta-command is that, when applied, a document reverts back to the state it had been in before the most recent edit command, as though that command were never executed. But we need not stop there, as a second application of undo should take the document backwards one step further, to the state it had been in before the next-to-last edit command was executed. And so on and so forth, in case undo were applied three or more times. Indeed, if undo were applied as many times as edit commands had been applied, the document should revert back to its initial state!
By what logic can this be implemented?
Answer:
Clearly, it is necessary to "remember" the exact sequence of edit commands
that were executed upon the document.2
Because the order in which commands are undone is opposite to the order
in which they were executed, the clear choice for an appropriate storage
structure is the stack, which provides LIFO (last-in-first-out) access to
its elements.
The solution, then, is that each time an edit command is executed, a representation of it is pushed onto the stack. When undo is applied, the command on top of the stack is popped and whatever actions are necessary to reverse its effects are taken. (In other words, the inverse of the popped command is executed.)
But what, exactly, would be the form taken by the "representation of an
edit command"?
Answer:
Java is an object-oriented programming language. Hence, an edit command
would be naturally represented by an object whose instance variables contained
the data/information sufficient to unambiguously describe the command.
For the purposes of this assignment, such an object would be an instance
of one of the classes that implements the EditCommand interface
(see above).
Now, what about the redo meta-command? What is its intended effect?
Answer: The intent of redo is to have the
effect of re-executing the most recently undone edit command!
Like undo, redo can be invoked several times in a row,
but is limited by the number of times that undo was invoked
since the last time that an edit command was invoked.
This guarantees that every application of redo puts the document
back into a state that it had been in at some previous moment.
To illustrate using a generic example, suppose that ten edit commands have been executed, say C1, C2, ..., C10, in that order. Now the user realizes that he may have made a mistake when issuing the command C7, so he invokes undo four times, thereby reversing the effects of C10, C9, C8, and C7, in that order. At that point, the document would have reverted back to the state it had been in immediately after the application of C6.
But then the user realizes that he really did want to apply commands C7 and C8. To get that effect, he invokes redo two times, causing C7 and C8 to be "redone". At this point, it is possible to redo C9 (and C10 thereafter). It is also possible to undo C8 again (and, afterwards, to undo any number of C7, C6, etc.).
But suppose that, after having redone C7 and C8, the user chooses to invoke a "new" edit command C11. At that point, there is no longer an option to redo, and the fact that edit commands C9 and C10 were done and later undone can be forgotten.
Question: Why can neither C9 nor C10
be redone after the application of C11?
Answer: Suppose, after applying C11, C9
were redone. Then the document would be in the state obtained, beginning
in its initial state, by the sequence of edit commands
C1, C2, ..., C8, C11,
C9. But this state is not guaranteed to be one that the
document had been in previously, which is contrary to the intended
effect of redo.
Implementing redo adds some complexity to our problem. In the first place, we now need a second stack, this one to store the edit commands that have been undone (since the last time a "new" edit command was issued), making them candidates for being "redone" later.
Referring to the two stacks as the Done-stack (which holds the sequence of edit commands that put the document into its current state) and the Undone-stack (which holds the edit commands that have been undone since the last time a "new" edit command was issued), the logic now is that whenever an undo occurs, the edit command on top of the Done-stack must be transferred to the Undone-stack. Meanwhile, whenever the user issues an edit command, the Undone-stack must be cleared (i.e., made empty), because invoking redo at that point is prohibited.
In the second place, we must ensure that the objects that represent edit commands (i.e., instances of the classes InsertCommand, DeleteCommand, and ReplaceCommand) contain data sufficient not only to make clear how to undo them but also sufficient to make clear how to redo them.
$ java CLI_Editor star_trek.txt Welcome to the CLI_Editor ------------------------- > help Command templates: insert <line_number> <text> delete <line_number> replace <line_number> <text> display <line_number> <line_number> display saveAs <file_name> save undo redo exit > display [0]: Arena [1]: Mirror, Mirror [2]: City on the Edge of Forever [3]: Amok Time [4]: Devil in the Dark > insert 3 The Enterprise Incident Executed: INSERT "The Enterprise Incident" at line 3 > display [0]: Arena [1]: Mirror, Mirror [2]: City on the Edge of Forever [3]: The Enterprise Incident [4]: Amok Time [5]: Devil in the Dark > replace 3 This Side of Paradise Executed: REPLACE "The Enterprise Incident" in line 3 by "This Side of Paradise" > display [0]: Arena [1]: Mirror, Mirror [2]: City on the Edge of Forever [3]: This Side of Paradise [4]: Amok Time [5]: Devil in the Dark > delete 0 Executed: DELETE line 0 > display [0]: Mirror, Mirror [1]: City on the Edge of Forever [2]: This Side of Paradise [3]: Amok Time [4]: Devil in the Dark > undo Undid: DELETE line 0 > display [0]: Arena [1]: Mirror, Mirror [2]: City on the Edge of Forever [3]: This Side of Paradise [4]: Amok Time [5]: Devil in the Darkcontinued in next column |
> undo Undid: REPLACE "The Enterprise Incident" in line 3 by "This Side of Paradise" > display [0]: Arena [1]: Mirror, Mirror [2]: City on the Edge of Forever [3]: The Enterprise Incident [4]: Amok Time [5]: Devil in the Dark > redo Redid: REPLACE "The Enterprise Incident" in line 3 by "This Side of Paradise" > display [0]: Arena [1]: Mirror, Mirror [2]: City on the Edge of Forever [3]: This Side of Paradise [4]: Amok Time [5]: Devil in the Dark > insert 4 Spock's Brain Executed: INSERT "Spock's Brain" at line 4 > redo Nothing left to redo! > undo Undid: INSERT "Spock's Brain" at line 4 > display [0]: Arena [1]: Mirror, Mirror [2]: City on the Edge of Forever [3]: This Side of Paradise [4]: Amok Time [5]: Devil in the Dark > undo Undid: REPLACE "The Enterprise Incident" in line 3 by "This Side of Paradise" > display [0]: Arena [1]: Mirror, Mirror [2]: City on the Edge of Forever [3]: The Enterprise Incident [4]: Amok Time [5]: Devil in the Dark > redo Redid: REPLACE "The Enterprise Incident" in line 3 by "This Side of Paradise" > display [0]: Arena [1]: Mirror, Mirror [2]: City on the Edge of Forever [3]: This Side of Paradise [4]: Amok Time [5]: Devil in the Dark > insert 6 Plato's Stepchildren Executed: INSERT "Plato's Stepchildren" at line 6 > display [0]: Arena [1]: Mirror, Mirror [2]: City on the Edge of Forever [3]: This Side of Paradise [4]: Amok Time [5]: Devil in the Dark [6]: Plato's Stepchildren > exit Goodbye! |
In the study of software design patterns, the pattern referred to as Command applies, as it turns a user action (such as typing text, deleting a file, or drawing a shape) into a stand-alone object containing all the information about that action, making it possible to undo or redo it later.