The previous days to learn <<java and patterns >>, to the command mode, brought to the implementation of a CTRL + Z case, want to learn programming so long, CTRL + Z has not been seriously implemented.
Therefore, with the help of Java and the source code inside the schema, the revocation and fallback are implemented (some problems in Java and the code in the schema book). This time using the command mode, because there are interfaces, there are buttons, there are receivers, there are senders.
The following is a class diagram, for convenience only, without considering the UML detail specification.
The following is the implementation of the program. Mainframe class: Provides the main method, the client class: Similar to watching TV, this is associated with undoabletextarea,itsukyuquatation because you want to initialize the object
PackageUndoredo;ImportJava.awt.Frame;Import java.awt.event.*;PublicClass MainFrameExtendsFrame {PrivateStaticUndoabletextarea text;PrivateStaticItsukyuquatation Panel;PublicMainFrame (String title) {Super(title); }PublicStaticvoidMain (string[] args) {//Constructs an undoable textarea text =New Undoabletextarea ("Your text Here");// construction panel panel = new< Span style= "color: #000000;" > itsukyuquatation (text); // structure frame MainFrame frame = new MainFrame ("Test undo and Redo" //new Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0); } }); Frame.add (panel); Frame.setsize (300,300true); }}
Itsukyuquatation class: Panel class, which includes Undo and redo two buttons, similar to the command mode of the remote controller. Implements the ActionListener interface.
PackageUndoredo;ImportJava.awt.BorderLayout;ImportJava.awt.Panel;ImportJava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;PublicClass ItsukyuquatationExtends PanelImplementsActionListener {PrivateFinalCommand undo;PrivateFinalCommand Redo;PublicItsukyuquatation (Undoabletextarea text) {This.setlayout (NewBorderLayout ()); Panel toolbar =NewPanel (); Undo =New Undocommand (text); redo = new Redocommand (text); Undo.addactionlistener (this); Redo.addactionlistener (thisthis.add (Toolbar, "North" this.add (Text, "Center" public void// TODO auto-generated Method stub Command cmd =
Undoabletextarea class: Similar to the TV class in command mode. Implements the Stateeditable interface, so the class has the ability to undo and redo. The overloaded storestate and restorestate are the contents of the text in the Hashtable.
The class is the most core class, the realization of the principle of CTRL + Z is simply that undomanger.edits is a vector dynamic array, for each keystroke action, will store a copy of the current text content to the data. When you click the Undo button,
The array index (the Indexofnextadd variable of the Unomanager class) moves forward one bit to get the text content of the previous state. Click the Redo button to move the array index back one bit, taking the text content of the next state. However, the contents of each state are not deleted or modified.
Initundoable () method, if the Keypressed event is used, then the recent state is not added to Undomanager.edits joins. So choose the Keyreleased event. If the valuechanged event is monitored, then the code
As in Java and schema, only the last edit undo is possible.
PackageUndoredo;ImportJava.awt.TextArea;Import java.awt.event.*;Importjava.util.Hashtable;Import javax.swing.undo.*;Importjavax.swing.undo.StateEditable;PublicClass UndoabletextareaExtends TextAreaImplementsstateeditable {PrivateUndoManager UndoManager;PrivateFinalStatic String key_state= "Undoabletextareakey";PrivateStateedit Currentedit;Boolean textChanged =False;PublicUndoabletextarea () {Super(); Initundoable (); }PublicUndoabletextarea (String string) {Super(string); Initundoable (); }//Storage StatePublicvoid Storestate (Hashtable<object, object>hashTable) {//TODO auto-generated Method Stub hashtable.put (Key_state,This. GetText ()); }//Restore statePublicvoid Restorestate (hashtable<?,? >hashTable) {//TODO auto-generated Method Stub Object data =Hashtable.get (key_state);if (data!=Null) {This. SetText (String) data; } }//Revocation methodPublicBooleanUndo () {Try{Undomanager.undo (); System.out.println ("undo=" +Undomanager.tostring ());ReturnTrue; }Catch(Cannotundoexception e) {System.out.println ("Can ' t undo"); }ReturnFalse; }//Redo methodPublicBooleanRedo () {Try{Undomanager.redo (); System.out.println ("redo=" +Undomanager.tostring ());ReturnTrue; }Catch(Cannotredoexception e) {System.out.println ("Can ' t Redo"); }ReturnFalse; }PrivatevoidInitundoable () {This.undomanager =NewUndoManager ();This.currentedit =New Stateedit (This);This.addkeylistener (NewKeyadapter () {/** * If the keypressed event is used, the recent status is not added to Undomanager.edits joins. * Therefore choose keyreleased event. * public void keypressed (KeyEvent event) {if (Event.isactionkey ()) {//takesnapshot ();} else {textChanged = true; Takesnapshot ();}}*/PublicvoidKeyreleased (KeyEvent event) {If(Event.isactionkey ()) {//Takesnapshot (); }Else{textChanged =True; Takesnapshot (); } } });This.addfocuslistener (NewFocusadapter () {PublicvoidFocuslost (FocusEvent event) {//takesnapshot (); private voidif (textChanged) {this.currentedit.end (); this.undomanager.addedit (this .currentedit); this.currentedit = new stateedit (this); System.out.println ("takesnapshot=" + undomanager.tostring ()); textChanged = false
Command abstract class: Define button, abstract method execute ()
Undocommand,redocommand are implementations of the command class, similar to the specific buttons in the commands pattern, and are implemented by invoking the receiver's method.
Package Undoredo; Import Java.awt.Button; Extendspublicsupervoid execute ();}
package Undoredo; Public class undocommand extends Command {Undoabletextarea text; public Undocommand (Undoabletextarea text) {super ("Undo" ); this.text = text;} @Override public void execute () { // TODO auto-generated method Stub Text.undo (); }}
Package Undoredo; Extendspublicsuper ("Redo"this.text =void// TODO auto-generated method stub Text.redo (); }}
CTRL + Z Java implementation with command pattern