using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace Mesnac.Basic.Service
{
///
/// 操作命令类,用与进行撤销和恢复操作的封装类
///
public class OperationCommand : ICommand
{
#region 字段定义
private Control _ctrl;
private object _newValue;
private object _oldValue;
private EventHandler _eventHandler;
private DataGridViewCellEventHandler _dataGridViewCellEventHandler;
#endregion
#region 构造方法
public OperationCommand (Control ctrl, object newValue, object oldValue)
{
this._ctrl = ctrl;
this._newValue = newValue;
this._oldValue = oldValue;
}
public OperationCommand(Control ctrl, object newValue, object oldValue, EventHandler eventHandler)
{
this._ctrl = ctrl;
this._newValue = newValue;
this._oldValue = oldValue;
this._eventHandler = eventHandler;
}
public OperationCommand(Control ctrl, object newValue, object oldValue, DataGridViewCellEventHandler dataGridViewCellEventHandler, int rowIndex, int columnIndex)
{
this._ctrl = ctrl;
this._newValue = newValue;
this._oldValue = oldValue;
this._dataGridViewCellEventHandler = dataGridViewCellEventHandler;
}
#endregion
#region ICommand接口成员实现
#region 恢复操作实现
///
/// 恢复操作实现
///
public void execute()
{
if (this._ctrl is TextBox)
{
(this._ctrl as TextBox).TextChanged -= this._eventHandler;
(this._ctrl as TextBox).Text = this._newValue == null ? String.Empty : this._newValue.ToString();
(this._ctrl as TextBox).SelectionStart = (this._ctrl as TextBox).Text.Length;
(this._ctrl as TextBox).TextChanged += this._eventHandler;
}
if (this._ctrl is CheckBox)
{
(this._ctrl as CheckBox).CheckedChanged -= this._eventHandler;
bool newValue = false;
if (this._newValue != null)
{
bool.TryParse(this._newValue.ToString(), out newValue);
}
(this._ctrl as CheckBox).Checked = newValue;
(this._ctrl as CheckBox).CheckedChanged += this._eventHandler;
}
if (this._ctrl is ComboBox)
{
(this._ctrl as ComboBox).SelectedIndexChanged -= this._eventHandler;
(this._ctrl as ComboBox).SelectedItem = this._newValue;
(this._ctrl as ComboBox).SelectedIndexChanged += this._eventHandler;
}
if (this._ctrl is DateTimePicker)
{
(this._ctrl as DateTimePicker).ValueChanged -= this._eventHandler;
DateTime newValue = DateTime.Now;
if (this._newValue != null)
{
DateTime.TryParse(this._newValue.ToString(), out newValue);
}
(this._ctrl as DateTimePicker).Value = newValue;
(this._ctrl as DateTimePicker).ValueChanged += this._eventHandler;
}
if (this._ctrl is DataGridView)
{
if (this._dataGridViewCellEventHandler != null)
{
(this._ctrl as DataGridView).CellValueChanged -= this._dataGridViewCellEventHandler;
}
(this._ctrl as DataGridView).DataSource = this._newValue;
Mesnac.Basic.DataProcessor.ClearSelectedStatus((this._ctrl as DataGridView));
if (this._dataGridViewCellEventHandler != null)
{
(this._ctrl as DataGridView).CellValueChanged += this._dataGridViewCellEventHandler;
}
}
}
#endregion
#region 撤销操作实现
///
/// 撤销操作实现
///
public void undo()
{
if (this._ctrl is TextBox)
{
(this._ctrl as TextBox).TextChanged -= this._eventHandler;
(this._ctrl as TextBox).Text = this._oldValue == null ? String.Empty : this._oldValue.ToString();
(this._ctrl as TextBox).SelectionStart = (this._ctrl as TextBox).Text.Length;
(this._ctrl as TextBox).TextChanged += this._eventHandler;
}
if (this._ctrl is CheckBox)
{
(this._ctrl as CheckBox).CheckedChanged -= this._eventHandler;
bool oldValue = false;
if (this._oldValue != null)
{
bool.TryParse(this._oldValue.ToString(), out oldValue);
}
(this._ctrl as CheckBox).Checked = oldValue;
(this._ctrl as CheckBox).CheckedChanged += this._eventHandler;
}
if (this._ctrl is ComboBox)
{
(this._ctrl as ComboBox).SelectedIndexChanged -= this._eventHandler;
(this._ctrl as ComboBox).SelectedItem = this._oldValue;
(this._ctrl as ComboBox).SelectedIndexChanged += this._eventHandler;
}
if (this._ctrl is DateTimePicker)
{
(this._ctrl as DateTimePicker).ValueChanged -= this._eventHandler;
DateTime oldValue = DateTime.Now;
if (this._oldValue != null)
{
DateTime.TryParse(this._oldValue.ToString(), out oldValue);
}
(this._ctrl as DateTimePicker).Value = oldValue;
(this._ctrl as DateTimePicker).ValueChanged += this._eventHandler;
}
if (this._ctrl is DataGridView)
{
if (this._dataGridViewCellEventHandler != null)
{
(this._ctrl as DataGridView).CellValueChanged -= this._dataGridViewCellEventHandler;
}
(this._ctrl as DataGridView).DataSource = this._oldValue;
Mesnac.Basic.DataProcessor.ClearSelectedStatus((this._ctrl as DataGridView));
if (this._dataGridViewCellEventHandler != null)
{
(this._ctrl as DataGridView).CellValueChanged += this._dataGridViewCellEventHandler;
}
}
}
#endregion
#endregion
}
}