Undo and undo Functions

Source: Internet
Author: User
You must always see icons similar to the following:

They indicate "undo" and "undo ". In programs, this function is usually implemented in command mode, and this article is no exception. First, the command is abstracted and the icommand interface: public interface icommand is used.
{
Void excute ();
}

Excute indicates the action (or action sequence) contained in the target referenced by the icommand ).

An unrecoverable command is also a command, so it inherits from the icommand:
Public interface ibackablecommand: icommand
{
Void undo ();
}

An unrecoverable command indicates that after the excute method is called, the Undo method can be called to restore the operated object to the previous state.

To implement the Undo function, we need a stack to record all the executable undo commands. To implement the Undo function, similarly, we also need a stack to record all undo commands. This function is provided by the icommandmanager interface:
Public interface icommandmanager
{
Void excutecommand (icommand command );
Void undo ();
Void reverseundo (); // undo

// The following events can be used to control the Enable of the undo and undo icons
Event cbsimplebool undostatechanged; // The bool parameter indicates whether the current operation can be undone.
Event cbsimplebool reverseundostatechanged; // The bool parameter indicates whether the current operation can be undone.
}

Now, let's analyze in detail when to push the command object and when to pop the command object in the Undo Command Stack and undo Command Stack:
(1) After executing any command, the reverseundo stack is cleared.
(2) After an unrecoverable command is executed, the Undo stack is cleared.
(3) After executing an unrecoverable command, press it into the Undo stack.
(4) After revoking a command, transfer it to the reverseundo stack.

Based on this, we can implement icommandmanager: public class commandmanager: icommandmanager
{
Private stack undostack = new stack ();
Private stack reversestack = new stack ();

Public event cbsimplebool undostatechanged;
Public event cbsimplebool reverseundostatechanged;

Public commandmanager ()
{
This. undostatechanged + = new cbsimplebool (commandmanager_undostatechanged );
This. reverseundostatechanged + = new cbsimplebool (commandmanager_undostatechanged );
}

Private void commandmanager_undostatechanged (bool Val)
{

}

# Region icommandmanager Member

Public void excutecommand (icommand command)
{
Command. excute ();
This. reversestack. Clear ();

If (command is ibackablecommand)
{
This. undostack. Push (command );
}
Else
{
This. undostack. Clear ();
}

This. undostatechanged (this. undostack. Count> 0 );
}

Public void undo ()
{
Ibackablecommand command = (ibackablecommand) This. undostack. Pop ();
If (command = NULL)
{
Return;
}

Command. Undo ();
This. reversestack. Push (command );

This. reverseundostatechanged (this. reversestack. Count> 0 );
}

Public void reverseundo ()
{
Ibackablecommand command = (ibackablecommand) This. reversestack. Pop ();
If (command = NULL)
{
Return;
}

Command. excute ();
This. undostack. Push (command );

This. undostatechanged (this. undostack. Count> 0 );
This. reverseundostatechanged (this. reversestack. Count> 0 );
}
# Endregion
}

The undo and undo functions described in this article are irrelevant to the application, so they can be reused in different applications, you only need to implement the corresponding icommand interface and ibackablecommand interface according to your application needs to immediately use the Undo and undo functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.