一个简单的命令模式类

using System;
using System.IO;
using EPS.UI.CommonBase;
using System.Collections;
using System.Reflection;
using System.Windows.Forms;
using Sunisoft.IrisSkin;
using DevExpress.XtraNavBar;

namespace EPS.UI.MainFrame
{
 /// <summary>
 /// Command 命令模式抽象类
 /// </summary>
    public abstract class Command
 {
  protected string _resourceName;

  // 克隆方法
  public abstract Command Clone();
  // 执行方法
  public abstract bool Execute();
  // 撤销方法
  public abstract bool Undo();
  // 重做方法
  public abstract bool Redo();

  //该命令的名字
  public string ResourceName
  {
   get { return _resourceName;}
  }

  public abstract string Description
  {
   get;
  }
 }

 /// <summary>
 /// 加载外部Form的命令
 /// </summary>
 public class FormNodeClickCommand : Command
 { 
  //全局FormHash表
        public static System.Collections.Hashtable g_FormTable = Hashtable.Synchronized(new Hashtable());
  
  //文件名
  protected string _resourceFileName;
  //当前主窗体指针
  protected Form _mainFrame = null;

  public FormNodeClickCommand(Form mainFrame, string fileName, string itemTitle)
  {
   this._mainFrame = mainFrame;
   this._resourceFileName = fileName;
   this._resourceName = itemTitle;
  }

  private void InitialForm()
  {
   string appPath = System.Environment.CurrentDirectory;
   string filePath = appPath + @"/" + this._resourceFileName;

   Assembly formAssembly = Assembly.LoadFrom(filePath);

   Type frmType = formAssembly.GetType("EPS.UI.SecurityManager.SecurityManager");
   Object frmObject = Activator.CreateInstance(frmType);
   FormBase loadform = (FormBase)frmObject;
   
   loadform.MdiParent = this._mainFrame;
   loadform.Show();
   //加入到全局表中
   g_FormTable.Add(_resourceName, loadform);   
  }

  public override bool Execute()
  {
   FormBase form = (FormBase)g_FormTable[_resourceName];

   if (form == null)
   {
    //重新加载
    InitialForm();
    return true;
   }
   else
   {
    if (form.IsDisposed)
    {
     //移除
     g_FormTable.Remove(_resourceName);
     //重新加载
     InitialForm();
     return true;
    }
    else
    {
     //重置到前面页
     form.MdiParent = this._mainFrame;
     form.Activate();
     form.WindowState = FormWindowState.Maximized;
     return true;
    }
   }   
  }

  public override bool Redo()
  {
   return false;
  }

  public override bool Undo()
  {
   return false;
  }

  public override string Description
  {
   get
   {
    return this._resourceName;
   }
  }

  public override Command Clone()
  {
   return new FormNodeClickCommand(this._mainFrame, this._resourceFileName, this._resourceName);
  }

 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值