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.
lj_plc/Main/Mensac.Gui.Run.Global/AppConfigHandler.cs

828 lines
30 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
{
/// <summary>
/// 此类用于解析平台运行应用程序配置文件及保存全局变量
/// </summary>
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 属性定义
/// <summary>
/// 自定义插件目录
/// </summary>
public string MesnacPlugInPath
{
get
{
return this._mesnacPlugInPath;
}
}
/// <summary>
/// 组态工程目录
/// </summary>
public string MCProjectPath
{
get
{
return this._mcProjectPath;
}
}
/// <summary>
/// 系统工程配置目录
/// </summary>
public string SysConfigPath
{
get
{
return this._sysConfigPath;
}
}
/// <summary>
/// 系统自定义菜单配置文件
/// </summary>
public string MenuConfig
{
get
{
return this._menuConfig;
}
}
/// <summary>
/// 系统自定义工具栏配置文件
/// </summary>
public string ToolStripConfig
{
get
{
return this._toolStripConfig;
}
}
/// <summary>
/// 系统国际化资源文件配置文件
/// </summary>
public string SysMessageConfig
{
get
{
return this._sysMessageConfig;
}
}
#endregion
#region 公共方法
/// <summary>
/// 把树节点序列化至配置文件
/// </summary>
/// <param name="root">要序列化的树节点</param>
/// <param name="file">配置文件完整路径</param>
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();
}
/// <summary>
/// 从配置文件反序列化树节点对象
/// </summary>
/// <param name="file">配置文件完整路径</param>
/// <returns>反序列化后的树节点对象</returns>
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<AppConfigHandler>.Error(ex.Message + "\r\n" + ex.StackTrace);
return null;
}
}
#region 重构系统主工具栏
/// <summary>
/// 重构系统主工具栏,目前只支持一级
/// </summary>
/// <param name="toolStrip">工具栏对象</param>
/// <param name="topMenu">主菜单对象</param>
/// <param name="toolButtons">设置的工具栏项集合</param>
public void ReBuilderToolStrip(ToolStrip toolStrip, MenuStrip topMenu, List<RunConfigMenuItem> toolButtons)
{
#region 删除工具栏中多余项
List<ToolStripItem> lst = new List<ToolStripItem>();
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 重构系统主菜单
/// <summary>
/// 以递归方式重构系统主菜单
/// </summary>
/// <param name="topMenu">原始菜单栏</param>
/// <param name="node">当前递归下要重构的目标节点</param>
/// <param name="root">要重构的目标菜单结构树根节点</param>
/// <param name="allFormPurview">所有权限列表</param>
/// <param name="PurviewList">当前用户具有的权限列表</param>
public void ReBuilderMenu(MenuStrip topMenu, TreeNode node, TreeNode root, List<string> allFormPurview, List<string> 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);
}
}
/// <summary>
/// 以递归方式重构系统主菜单
/// </summary>
/// <param name="topMenu">原始菜单栏</param>
/// <param name="node">当前递归下要重构的目标节点</param>
/// <param name="root">要重构的目标菜单结构树根节点</param>
public void ReBuilderMenu(MenuStrip topMenu, TreeNode node, TreeNode root)
{
this.ReBuilderMenu(topMenu, node, root, UserInfo.Instance.AllFormPurview, UserInfo.Instance.PurviewList);
}
#region 自定义菜单处理辅助方法
/// <summary>
/// 在菜单栏中查找菜单项
/// </summary>
/// <param name="id">要查找的条件id</param>
/// <param name="topMenu">菜单栏</param>
/// <returns>如果找到则返回对应的菜单项如果找不到返回null</returns>
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;
}
/// <summary>
/// 以递归方式在指定菜单的子级菜单中查找对应id的菜单项
/// </summary>
/// <param name="parentMenu">指定的菜单项</param>
/// <param name="id">要匹配的id</param>
/// <returns>返回对应菜单项找不到返回null</returns>
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;
}
/// <summary>
/// 判断菜单与指定ID是否匹配
/// </summary>
/// <param name="menuItem">菜单项</param>
/// <param name="id">ID号</param>
/// <returns>如果匹配返回真,否则返回假</returns>
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;
}
/// <summary>
/// 把ToolStripItem转换为对应的TreeNode
/// </summary>
/// <param name="menuItem">菜单项</param>
/// <returns>树节点</returns>
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 初始化自定义系统菜单
/// <summary>
/// 初始化自定义系统菜单和工具栏
/// </summary>
/// <param name="topMenu">系统菜单栏</param>
/// <param name="toolStrip">系统工具栏</param>
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<RunConfigMenuItem> lst = this.DeserializeObject(this._toolStripConfig) as List<RunConfigMenuItem>;
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 执行业务
/// <summary>
/// 执行命令
/// </summary>
/// <param name="sender">事件源</param>
/// <param name="rcmi">命令对象</param>
public void RunCommand(object sender, RunConfigMenuItem rcmi)
{
Mesnac.ActionService.Service.Instance.Run(sender, rcmi.CommandValue);
}
#endregion
#region 自定义菜单处理程序
/// <summary>
/// 自定义菜单处理程序
/// </summary>
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<SysConfigMenuCommand>.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;
/// <summary>
/// 从配置文件中加载国际化资源数据
/// </summary>
/// <returns></returns>
public SysMessage LoadSysMessage()
{
SysMessage sysMessage = null;
if (File.Exists(this._sysMessageConfig))
{
sysMessage = this.DeserializeObject(this._sysMessageConfig) as SysMessage;
}
else
{
sysMessage = new SysMessage();
}
return sysMessage;
}
/// <summary>
/// 保存国际化资源至配置文件
/// </summary>
/// <param name="sysMessage"></param>
public void SaveSysMessage(SysMessage sysMessage)
{
this.SerializeObject(sysMessage, this._sysMessageConfig);
}
/// <summary>
/// 国际化资源数据
/// </summary>
public SysMessage SysMessages
{
get
{
if (this._sysMessages == null)
{
this._sysMessages = this.LoadSysMessage();
}
return this._sysMessages;
}
}
#endregion
#endregion
}
}