using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Data;
using ICSharpCode.Core;
using Mesnac.Core;
using Mesnac.Basic;
using Mesnac.Gui.Workbench;
namespace Mesnac.Gui.Run.Global
{
///
/// 此类用于解析平台运行应用程序配置文件及保存全局变量
///
public class AppConfigHandler
{
#region 字段定义
private static AppConfigHandler instance = null;
public string IsNormalUser { get; set; }
private string _mesnacPlugInPath = String.Empty; //自定义插件目录
private string _mcProjectPath = String.Empty; //组态工程目录
private string _sysConfigPath = String.Empty; //系统工程配置目录
private string _menuConfig = String.Empty; //系统自定义菜单配置文件
private string _toolStripConfig = String.Empty; //系统自定义工具栏配置文件
private string _sysMessageConfig = String.Empty; //系统国际化资源文件配置文件
#endregion
#region 单例实现
private AppConfigHandler()
{
this.IsNormalUser = ConfigurationManager.AppSettings["IsNormalUser"];
this._mesnacPlugInPath = ConfigurationManager.AppSettings["MesnacPlugInPath"];
this._mcProjectPath = ConfigurationManager.AppSettings["MCProjectPath"];
this._sysConfigPath = ConfigurationManager.AppSettings["SysConfigPath"];
this._menuConfig = Path.Combine(Application.StartupPath, this._sysConfigPath, "MenuConfig.dat");
this._toolStripConfig = Path.Combine(Application.StartupPath, this._sysConfigPath, "ToolStripConfig.dat");
this._sysMessageConfig = Path.Combine(Application.StartupPath, this._sysConfigPath, "SysMessage.dat");
}
public static AppConfigHandler Instance
{
get
{
if (instance == null)
{
instance = new AppConfigHandler();
}
return instance;
}
}
#endregion
#region 属性定义
///
/// 自定义插件目录
///
public string MesnacPlugInPath
{
get
{
return this._mesnacPlugInPath;
}
}
///
/// 组态工程目录
///
public string MCProjectPath
{
get
{
return this._mcProjectPath;
}
}
///
/// 系统工程配置目录
///
public string SysConfigPath
{
get
{
return this._sysConfigPath;
}
}
///
/// 系统自定义菜单配置文件
///
public string MenuConfig
{
get
{
return this._menuConfig;
}
}
///
/// 系统自定义工具栏配置文件
///
public string ToolStripConfig
{
get
{
return this._toolStripConfig;
}
}
///
/// 系统国际化资源文件配置文件
///
public string SysMessageConfig
{
get
{
return this._sysMessageConfig;
}
}
#endregion
#region 公共方法
///
/// 把树节点序列化至配置文件
///
/// 要序列化的树节点
/// 配置文件完整路径
public void SerializeObject(object obj, string file)
{
FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write);
//SoapFormatter formatter = new SoapFormatter();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, obj);
fs.Flush();
fs.Close();
}
///
/// 从配置文件反序列化树节点对象
///
/// 配置文件完整路径
/// 反序列化后的树节点对象
public object DeserializeObject(string file)
{
try
{
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
//SoapFormatter formatter = new SoapFormatter();
BinaryFormatter formatter = new BinaryFormatter();
object obj = formatter.Deserialize(fs);
fs.Flush();
fs.Close();
return obj;
}
catch (Exception ex)
{
LoggingService.Error(ex.Message + "\r\n" + ex.StackTrace);
return null;
}
}
#region 重构系统主工具栏
///
/// 重构系统主工具栏,目前只支持一级
///
/// 工具栏对象
/// 主菜单对象
/// 设置的工具栏项集合
public void ReBuilderToolStrip(ToolStrip toolStrip, MenuStrip topMenu, List toolButtons)
{
#region 删除工具栏中多余项
List lst = new List();
foreach (ToolStripItem item1 in toolStrip.Items)
{
if (item1 is IStatusUpdate) continue; //如果是系统工具栏则跳过
bool flag1 = false;
foreach (RunConfigMenuItem toolButton1 in toolButtons)
{
if (item1.Name == toolButton1.ID)
{
flag1 = true;
break;
}
}
if (!flag1)
{
lst.Add(item1);
}
}
foreach (ToolStripItem item2 in lst)
{
toolStrip.Items.Remove(item2);
}
#endregion
int index = 0;
foreach (RunConfigMenuItem toolButton in toolButtons)
{
bool flag = false;
ToolStripItem tsi = null;
foreach (ToolStripItem item in toolStrip.Items)
{
if (item is IStatusUpdate)
{
if ((item as IStatusUpdate).ID == toolButton.ID)
{
tsi = item;
flag = true;
break;
}
}
else
{
if (item.Name == toolButton.ID)
{
tsi = item;
flag = true;
break;
}
}
}
if (flag)
{
toolStrip.Items.Remove(tsi);
toolStrip.Items.Insert(index, tsi);
#region 国际化处理
if (!String.IsNullOrEmpty(toolButton.MsgId))
{
if (AppConfigHandler.Instance.SysMessages.Messages.ContainsKey(toolButton.MsgId))
{
if (AppConfigHandler.instance.SysMessages.Messages[toolButton.MsgId].ContainsKey(System.Threading.Thread.CurrentThread.CurrentUICulture.Name))
{
tsi.ToolTipText = AppConfigHandler.instance.SysMessages.Messages[toolButton.MsgId][System.Threading.Thread.CurrentThread.CurrentUICulture.Name].Value;
}
}
}
#endregion
}
else
{
if (toolButton.Text == "-")
{
tsi = new ToolStripSeparator();
}
else
{
tsi = new ToolStripButton();
tsi.Name = toolButton.ID;
#region 国际化处理
if (!String.IsNullOrEmpty(toolButton.MsgId))
{
if (AppConfigHandler.Instance.SysMessages.Messages.ContainsKey(toolButton.MsgId))
{
if (AppConfigHandler.instance.SysMessages.Messages[toolButton.MsgId].ContainsKey(System.Threading.Thread.CurrentThread.CurrentUICulture.Name))
{
tsi.ToolTipText = AppConfigHandler.instance.SysMessages.Messages[toolButton.MsgId][System.Threading.Thread.CurrentThread.CurrentUICulture.Name].Value;
}
else
{
tsi.ToolTipText = toolButton.Text;
}
}
else
{
tsi.ToolTipText = toolButton.Text;
}
}
else
{
tsi.ToolTipText = toolButton.Text;
}
#endregion
//tsi.ToolTipText = toolButton.Text;
tsi.Image = toolButton.Img == null ? ResourceService.GetImageResource("Icons_16X16_Default") as System.Drawing.Image : toolButton.Img;
tsi.Click += delegate(object sender, EventArgs e)
{
ToolStripItem menuItem = this.FindItemFromTopMenu((sender as ToolStripItem).Name, topMenu);
if (menuItem != null)
{
menuItem.PerformClick();
}
};
}
tsi.Name = toolButton.ID;
toolStrip.Items.Insert(index, tsi);
}
//权限处理
ToolStripItem menuItem1 = this.FindItemFromTopMenu(tsi.Name, topMenu);
if (menuItem1 != null)
{
tsi.Enabled = menuItem1.Enabled;
}
index++;
}
}
#endregion
#region 重构系统主菜单
///
/// 以递归方式重构系统主菜单
///
/// 原始菜单栏
/// 当前递归下要重构的目标节点
/// 要重构的目标菜单结构树根节点
/// 所有权限列表
/// 当前用户具有的权限列表
public void ReBuilderMenu(MenuStrip topMenu, TreeNode node, TreeNode root, List allFormPurview, List purviewList)
{
foreach (TreeNode tn in node.Nodes)
{
int index = tn.Index;
RunConfigMenuItem rcmi = tn.Tag as RunConfigMenuItem;
ToolStripItem tsItem = null;
ToolStripItem menuItem = this.FindItemFromTopMenu(rcmi.ID, topMenu);
if (menuItem != null)
{
#region 自定义菜单国际化处理
if (rcmi.Text != "-")
{
if (!String.IsNullOrEmpty(rcmi.MsgId))
{
if (AppConfigHandler.Instance.SysMessages.Messages.ContainsKey(rcmi.MsgId))
{
if (AppConfigHandler.instance.SysMessages.Messages[rcmi.MsgId].ContainsKey(System.Threading.Thread.CurrentThread.CurrentUICulture.Name))
{
menuItem.Text = AppConfigHandler.instance.SysMessages.Messages[rcmi.MsgId][System.Threading.Thread.CurrentThread.CurrentUICulture.Name].Value;
}
}
}
}
#endregion
#region 系统原有菜单处理,先移除,再添加至合适位置
if (menuItem.OwnerItem != null)
{
ToolStripMenuItem parentMenu = menuItem.OwnerItem as ToolStripMenuItem;
parentMenu.DropDownItems.Remove(menuItem);
if (index > parentMenu.DropDownItems.Count)
{
parentMenu.DropDownItems.Add(menuItem);
}
else
{
parentMenu.DropDownItems.Insert(index, menuItem);
}
}
else
{
topMenu.Items.Remove(menuItem);
if (index > topMenu.Items.Count)
{
topMenu.Items.Add(menuItem);
}
else
{
topMenu.Items.Insert(index, menuItem);
}
}
#endregion
}
else
{
#region 自定义菜单处理,添加至合适位置
if (rcmi.Text == "-")
{
tsItem = new ToolStripSeparator();
tsItem.Name = rcmi.ID;
}
else
{
menuItem = new ToolStripMenuItem();
menuItem.Name = rcmi.ID;
#region 自定义菜单国际化处理
if (!String.IsNullOrEmpty(rcmi.MsgId))
{
if (AppConfigHandler.Instance.SysMessages.Messages.ContainsKey(rcmi.MsgId))
{
if (AppConfigHandler.instance.SysMessages.Messages[rcmi.MsgId].ContainsKey(System.Threading.Thread.CurrentThread.CurrentUICulture.Name))
{
menuItem.Text = AppConfigHandler.instance.SysMessages.Messages[rcmi.MsgId][System.Threading.Thread.CurrentThread.CurrentUICulture.Name].Value;
}
else
{
menuItem.Text = rcmi.Text;
}
}
else
{
menuItem.Text = rcmi.Text;
}
}
else
{
menuItem.Text = rcmi.Text;
}
#endregion
menuItem.Image = rcmi.Img;
if (!String.IsNullOrEmpty(rcmi.ShortCut))
{
Keys keyValue = Mesnac.Core.MenuCommand.ParseShortcut(rcmi.ShortCut);
(menuItem as ToolStripMenuItem).ShortcutKeys = keyValue;
}
if (rcmi.CreateType == CreateType.Form && !String.IsNullOrEmpty(rcmi.FormFile))
{
//自定义菜单事件处理,打开组态画面
menuItem.Click += delegate(object sender, EventArgs e)
{
SysConfigMenuCommand.Instance.Run(rcmi.FormFile, rcmi.ShowType);
};
}
else if (rcmi.CreateType == CreateType.Command)
{
//自定义菜单事件处理,执行组态命令
menuItem.Click += delegate(object sender, EventArgs e)
{
Mesnac.ActionService.Service.Instance.Run(sender, rcmi.CommandValue);
};
}
}
if (tn.Parent != root)
{
ToolStripMenuItem parentMenu = this.FindItemFromTopMenu((tn.Parent.Tag as RunConfigMenuItem).ID, topMenu) as ToolStripMenuItem;
if (parentMenu != null)
{
if (tsItem == null)
{
parentMenu.DropDownItems.Insert(index, menuItem);
}
else
{
parentMenu.DropDownItems.Add(tsItem);
}
}
else
{
if (tsItem == null)
{
topMenu.Items.Insert(index, menuItem);
}
else
{
topMenu.Items.Insert(index, tsItem);
}
}
}
else
{
if (tsItem == null)
{
topMenu.Items.Insert(index, menuItem);
}
else
{
topMenu.Items.Insert(index, tsItem);
}
}
#endregion
}
//权限处理
if (rcmi.CreateType == CreateType.Form)
{
if (!UserInfo.Instance.RoleGUID.Equals("-99") && allFormPurview.Contains(rcmi.ID) && !purviewList.Contains(rcmi.ID))
{
menuItem.Enabled = false;
}
else
{
if (menuItem != null)
{
menuItem.Enabled = true;
if (rcmi.IsAutoLoad)
{
#region 如果是自动加载的项,则直接执行
if (!WorkbenchSingleton.Workbench.ViewContentDic.ContainsKey(rcmi.FormFile))
{
SysConfigMenuCommand.Instance.Run(rcmi.FormFile, rcmi.ShowType);
}
else
{
if (WorkbenchSingleton.Workbench.ViewContentDic[rcmi.FormFile].Visible == false)
{
SysConfigMenuCommand.Instance.Run(rcmi.FormFile, rcmi.ShowType);
}
}
#endregion
}
}
}
}
else if (rcmi.CreateType == CreateType.Command)
{
string formName = rcmi.ID.Split(new char[] { '_' })[1];
if (!UserInfo.Instance.RoleGUID.Equals("-99") && allFormPurview.Contains(formName) && !purviewList.Contains(formName))
{
menuItem.Enabled = false;
}
else
{
if (menuItem != null) menuItem.Enabled = true;
}
}
this.ReBuilderMenu(topMenu, tn, root, allFormPurview, purviewList);
}
}
///
/// 以递归方式重构系统主菜单
///
/// 原始菜单栏
/// 当前递归下要重构的目标节点
/// 要重构的目标菜单结构树根节点
public void ReBuilderMenu(MenuStrip topMenu, TreeNode node, TreeNode root)
{
this.ReBuilderMenu(topMenu, node, root, UserInfo.Instance.AllFormPurview, UserInfo.Instance.PurviewList);
}
#region 自定义菜单处理辅助方法
///
/// 在菜单栏中查找菜单项
///
/// 要查找的条件id
/// 菜单栏
/// 如果找到则返回对应的菜单项,如果找不到返回null
public ToolStripItem FindItemFromTopMenu(string id, MenuStrip topMenu)
{
ToolStripItem tsmi = null;
foreach (ToolStripMenuItem menuItem in topMenu.Items)
{
if (this.IsMatchById(menuItem, id))
{
return menuItem;
}
else
{
tsmi = this.FindItemById(menuItem, id);
if (tsmi != null)
{
break;
}
}
}
return tsmi;
}
///
/// 以递归方式在指定菜单的子级菜单中查找对应id的菜单项
///
/// 指定的菜单项
/// 要匹配的id
/// 返回对应菜单项,找不到返回null
private ToolStripItem FindItemById(ToolStripItem parentMenu, string id)
{
ToolStripItem tsmi = null;
ToolStripMenuItem pm = parentMenu as ToolStripMenuItem;
if (pm != null)
{
foreach (ToolStripItem menuItem in pm.DropDownItems)
{
if (this.IsMatchById(menuItem, id))
{
return menuItem;
}
else
{
tsmi = this.FindItemById(menuItem, id);
if (tsmi != null)
{
break;
}
}
}
}
return tsmi;
}
///
/// 判断菜单与指定ID是否匹配
///
/// 菜单项
/// ID号
/// 如果匹配返回真,否则返回假
public bool IsMatchById(ToolStripItem menuItem, string id)
{
IStatusUpdate su = menuItem as IStatusUpdate;
if (su != null && su.ID == id)
{
return true;
}
else if (menuItem.Name == id)
{
return true;
}
return false;
}
///
/// 把ToolStripItem转换为对应的TreeNode
///
/// 菜单项
/// 树节点
public TreeNode ConvertToolStripItemToTreeNode(ToolStripItem menuItem)
{
RunConfigMenuItem rItem = new RunConfigMenuItem();
TreeNode tn = new TreeNode();
IStatusUpdate su = menuItem as Mesnac.Core.IStatusUpdate;
if (su != null)
{
tn.Name = (menuItem as Mesnac.Core.IStatusUpdate).ID;
}
else
{
if (String.IsNullOrEmpty(menuItem.Name))
{
tn.Name = Guid.NewGuid().ToString();
menuItem.Name = tn.Name;
}
}
tn.Text = menuItem.Text;
rItem.ID = tn.Name;
if (menuItem is ToolStripMenuItem)
{
rItem.Text = menuItem.Text;
}
else if (menuItem is ToolStripButton)
{
rItem.Text = menuItem.ToolTipText;
}
rItem.IsSystem = true;
tn.Tag = rItem;
if (menuItem is ToolStripMenuItem)
{
foreach (ToolStripItem mi in (menuItem as ToolStripMenuItem).DropDownItems)
{
tn.Nodes.Add(ConvertToolStripItemToTreeNode(mi));
}
}
else if (menuItem is ToolStripSeparator)
{
rItem.Text = "-";
tn.Text = "-";
}
return tn;
}
#endregion
#endregion
#region 初始化自定义系统菜单
///
/// 初始化自定义系统菜单和工具栏
///
/// 系统菜单栏
/// 系统工具栏
public void InitCustomerMenuAndToolStrip(MenuStrip topMenu, ToolStrip toolStrip)
{
if (File.Exists(this._menuConfig))
{
TreeNode root = this.DeserializeObject(this._menuConfig) as TreeNode;
if (root != null && topMenu != null)
{
AppConfigHandler.Instance.ReBuilderMenu(topMenu, root, root);
}
}
if (File.Exists(this._toolStripConfig))
{
List lst = this.DeserializeObject(this._toolStripConfig) as List;
if (lst != null && lst.Count > 0)
{
AppConfigHandler.Instance.ReBuilderToolStrip(toolStrip, topMenu, lst);
}
}
if (Mesnac.Basic.RunSchema.Instance.Exists())
{
//如果工程配置文件存在,则读取用户权限
DataTable table = UserInfo.Instance.GetPurview();
}
}
#endregion
#region 执行业务
///
/// 执行命令
///
/// 事件源
/// 命令对象
public void RunCommand(object sender, RunConfigMenuItem rcmi)
{
Mesnac.ActionService.Service.Instance.Run(sender, rcmi.CommandValue);
}
#endregion
#region 自定义菜单处理程序
///
/// 自定义菜单处理程序
///
public class SysConfigMenuCommand : AbstractCommand
{
private static SysConfigMenuCommand instance;
private SysConfigMenuCommand()
{
//Mesnac.Gui.Common.RunEngine.Instance.Init(this._projectPath); //初始化运行引擎
}
public static SysConfigMenuCommand Instance
{
get
{
if (instance == null)
{
instance = new SysConfigMenuCommand();
}
return instance;
}
}
public override void Run()
{
//test//
//TreeNode nodeForm = Mesnac.Gui.Common.RunEngine.Instance.Tree.GetFirstNodeByNodeName("FrmPlan");
//Mesnac.Gui.Common.RunEngine.Instance.Simulation(nodeForm, false);
LoggingService.Debug("自定义菜单处理程序...测试");
}
public void Run(string formFile, FormShowType showType)
{
TreeNode nodeForm = Mesnac.Gui.Common.RunEngine.Instance.Tree.GetFirstNodeByNodeName(formFile);
Mesnac.Gui.Common.RunEngine.Instance.Simulation(nodeForm, showType);
//查找DLL模块显示
if(nodeForm == null)
{
Mesnac.Gui.Common.RunEngine.Instance.SimulationDLL(formFile,showType);
}
}
}
#endregion
#region 国际化支持
private SysMessage _sysMessages;
///
/// 从配置文件中加载国际化资源数据
///
///
public SysMessage LoadSysMessage()
{
SysMessage sysMessage = null;
if (File.Exists(this._sysMessageConfig))
{
sysMessage = this.DeserializeObject(this._sysMessageConfig) as SysMessage;
}
else
{
sysMessage = new SysMessage();
}
return sysMessage;
}
///
/// 保存国际化资源至配置文件
///
///
public void SaveSysMessage(SysMessage sysMessage)
{
this.SerializeObject(sysMessage, this._sysMessageConfig);
}
///
/// 国际化资源数据
///
public SysMessage SysMessages
{
get
{
if (this._sysMessages == null)
{
this._sysMessages = this.LoadSysMessage();
}
return this._sysMessages;
}
}
#endregion
#endregion
}
}