You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mesnac.Basic.Service
{
/// <summary>
/// UndoRedo服务类
/// </summary>
public class UndoRedoService
{
#region 字段定义
private static Dictionary<Control, EventHandlerProcessor> _dicControlEventHandler = new Dictionary<Control, EventHandlerProcessor>();
#endregion
#region 属性定义
/// <summary>
/// Undo操作栈
/// </summary>
public static Stack<ICommand> UndoStack = new Stack<ICommand>();
/// <summary>
/// Redo操作栈
/// </summary>
public static Stack<ICommand> RedoStack = new Stack<ICommand>();
/// <summary>
/// 控件事件处理绑定集合
/// </summary>
public static Dictionary<Control, EventHandlerProcessor> DicControlEventHandler
{
get
{
return _dicControlEventHandler;
}
}
#endregion
#region Undo Redo服务初始化
/// <summary>
/// Undo Redo服务初始化
/// </summary>
/// <param name="controls">要注册UndoRedo服务的控件集合</param>
public static void Init(List<Control> controls)
{
Clear();
EventHandlerProcessor ehp = null;
foreach (Control ctl in controls)
{
if (ctl is TextBox)
{
ehp = new EventHandlerProcessor((ctl as TextBox).Text);
_dicControlEventHandler.Add(ctl, ehp);
(ctl as TextBox).LostFocus -= ehp.TextBox_LostFocus;
(ctl as TextBox).LostFocus += ehp.TextBox_LostFocus;
}
if (ctl is CheckBox)
{
ehp = new EventHandlerProcessor((ctl as CheckBox).Checked);
_dicControlEventHandler.Add(ctl, ehp);
(ctl as CheckBox).CheckedChanged -= ehp.CheckBox_CheckedChanged;
(ctl as CheckBox).CheckedChanged += ehp.CheckBox_CheckedChanged;
}
if (ctl is ComboBox)
{
ehp = new EventHandlerProcessor((ctl as ComboBox).SelectedItem);
_dicControlEventHandler.Add(ctl, ehp);
(ctl as ComboBox).SelectedIndexChanged -= ehp.ComboBox_SelectedIndexChanged;
(ctl as ComboBox).SelectedIndexChanged += ehp.ComboBox_SelectedIndexChanged;
}
if (ctl is DateTimePicker)
{
ehp = new EventHandlerProcessor((ctl as DateTimePicker).Value);
_dicControlEventHandler.Add(ctl, ehp);
(ctl as DateTimePicker).ValueChanged -= ehp.DateTimePicker_ValueChanged;
(ctl as DateTimePicker).ValueChanged += ehp.DateTimePicker_ValueChanged;
}
if (ctl is DataGridView)
{
DataTable dtOld = Mesnac.Basic.DataProcessor.GetDataTableFromGridView((ctl as DataGridView));
ehp = new EventHandlerProcessor(dtOld);
_dicControlEventHandler.Add(ctl, ehp);
(ctl as DataGridView).CellValueChanged -= ehp.DataGridView_CellValueChanged;
(ctl as DataGridView).CellValueChanged += ehp.DataGridView_CellValueChanged;
}
}
}
#endregion
#region 清除操作栈
/// <summary>
/// 清除操作栈
/// </summary>
public static void Clear()
{
foreach(Control ctl in _dicControlEventHandler.Keys)
{
if (ctl is TextBox)
{
(ctl as TextBox).LostFocus -= _dicControlEventHandler[ctl].TextBox_LostFocus;
}
if (ctl is CheckBox)
{
(ctl as CheckBox).CheckedChanged -= _dicControlEventHandler[ctl].CheckBox_CheckedChanged;
}
if (ctl is ComboBox)
{
(ctl as ComboBox).SelectedIndexChanged -= _dicControlEventHandler[ctl].ComboBox_SelectedIndexChanged;
}
if (ctl is DateTimePicker)
{
(ctl as DateTimePicker).ValueChanged -= _dicControlEventHandler[ctl].DateTimePicker_ValueChanged;
}
if (ctl is DataGridView)
{
(ctl as DataGridView).CellValueChanged -= _dicControlEventHandler[ctl].DataGridView_CellValueChanged;
}
}
_dicControlEventHandler.Clear();
UndoStack.Clear();
RedoStack.Clear();
}
#endregion
}
}