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/Mesnac.Gui.Edit/Pad/ProjectWindow.cs

657 lines
26 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Configuration;
using System.IO;
using System.Threading;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;
using System.Reflection.Emit;
using ICSharpCode.Core;
using Mesnac.Gui.Workbench;
using Mesnac.PlugIn.Pad;
using Mesnac.PlugIn.View;
using Mesnac.PlugIn.Workbench;
using Mesnac.Basic;
using Mesnac.Equips;
using Mesnac.Gui.Common;
using Mesnac.Gui.Edit.Global;
using Mesnac.Gui.Edit.Common;
using Mesnac.Gui.Edit.Dialog;
using Mesnac.Gui.Edit.ViewContent;
using Mesnac.Controls.Default;
namespace Mesnac.Gui.Edit.Pad
{
public partial class ProjectWindow : DefaultPadContent
{
#region 定义变量
//private AccidenceAnalyzer express;
//private Dictionary<string, Form> _runForms = new Dictionary<string, Form>();
//private readonly string _componentsActionFile = Path.Combine(Application.StartupPath, PublicConfig.Instance.ComponentsActionFile); //工具箱控件动画配置文件
#endregion
#region 公共属性
/// <summary>
/// 工程树
/// </summary>
public TreeView Tree
{
get
{
return tvProject;
}
}
#endregion
#region 构造方法与初始化处理
public ProjectWindow()
{
InitializeComponent();
this.InitUIMethod();
this.Init();
}
#region 重绘DefaultPadContent的RedrawContent方法
/// <summary>
/// 重绘组件事件触发时执行
/// </summary>
public override void RedrawContent()
{
base.RedrawContent();
this.InitUIMethod();
this.RefreshTreeNodeUI();
}
#endregion
/// <summary>
/// 初始化界面元素
/// </summary>
private void InitUIMethod()
{
this.mnuPreRun.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Form_mnuPreRun_Text")); //临时仿真
this.mnuDeleteFormItem.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Form_mnuDelete_Text")); //删除
this.mnuDeviceManager.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Form_mnuManage_Text")); //管理
this.mnuNewForm.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Form_mnuNew_Text")); //新建
this.mnuActionStrip.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Form_mnuNew_Text")); //新建
this.smuDelete.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Form_mnuDelete_Text")); //删除
this.tsmManger.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Form_mnuManage_Text")); ; //管理
}
/// <summary>
/// 刷新固定节点文本
/// </summary>
private void RefreshTreeNodeUI()
{
TreeNode tnFormName = this.tvProject.GetFirstNodeByNodeName(FixNodeName.NodeFormName);
TreeNode tnDeviceName = this.tvProject.GetFirstNodeByNodeName(FixNodeName.NodeDeviceName);
TreeNode tnDataSourceName = this.tvProject.GetFirstNodeByNodeName(FixNodeName.NodeDataSourceName);
TreeNode tnCommandName = this.tvProject.GetFirstNodeByNodeName(FixNodeName.NodeCommandName);
if (tnFormName != null)
{
tnFormName.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Gui_Edit_Pad_ProjectWindow_Forms")); //"画面"
}
if (tnDeviceName != null)
{
tnDeviceName.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Gui_Edit_Pad_ProjectWindow_Devices")); //"设备"
}
if (tnDataSourceName != null)
{
tnDataSourceName.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Gui_Edit_Pad_ProjectWindow_DataSources")); //"数据源";
}
if (tnCommandName != null)
{
tnCommandName.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Gui_Edit_Pad_ProjectWindow_Commands")); //"命令";
}
}
/// <summary>
/// 初始加载时展开树节点
/// </summary>
private void Init()
{
//this.tvProject.ExpandAll();
this.tvProject.Nodes.Clear();
//注册表达式
//express = new AccidenceAnalyzer();
//express.OnAccidenceAnalysis += new AccidenceAnalyzer.AccidenceAnalysis(express_OnAccidenceAnalysis);
//express.OnCallBack += new AccidenceAnalyzer.CallBack(express_OnCallBack);
//设备节点更新处理
Mesnac.Equips.Factory.Instance.OnConfigChanged += delegate(object sender, EventArgs e)
{
TreeNode tnDeviceName = this.tvProject.GetFirstNodeByNodeName(FixNodeName.NodeDeviceName);
if (tnDeviceName != null)
{
tnDeviceName.Nodes.Clear();
Dictionary<string, Mesnac.Equips.SetConfig.ConfigEquip> allConfigEquips = Mesnac.Equips.Factory.Instance.AllConfigEquip;
foreach (string key in allConfigEquips.Keys)
{
TreeNode tnDevice = new TreeNode();
tnDevice.Name = key;
tnDevice.Text = key;
tnDevice.Tag = key;
tnDevice.ImageIndex = 5;
tnDevice.SelectedImageIndex = 5;
tnDeviceName.Nodes.Add(tnDevice);
}
tnDeviceName.ExpandAll();
}
};
//数据源节点更新处理
DataSourceFactory.Instance.DataSourceRefresh += delegate(object sender, EventArgs e)
{
TreeNode tnDataSourceName = this.tvProject.GetFirstNodeByNodeName(FixNodeName.NodeDataSourceName);
if (tnDataSourceName != null)
{
tnDataSourceName.Nodes.Clear();
Dictionary<string, DataSourceItem> allDataSources = DataSourceFactory.Instance.DataSources;
foreach (string key in allDataSources.Keys)
{
TreeNode tnDataSource = new TreeNode();
tnDataSource.Name = key;
tnDataSource.Text = key;
tnDataSource.Tag = key;
tnDataSource.ImageIndex = 7;
tnDataSource.SelectedImageIndex = 7;
tnDataSourceName.Nodes.Add(tnDataSource);
}
tnDataSourceName.ExpandAll();
}
};
}
#endregion
#region 公共方法
/// <summary>
/// 生成工程树
/// </summary>
public void GenerateProjectTree(string fullFileName)
{
string projectName = Path.GetFileNameWithoutExtension(fullFileName);
TreeView tv = this.Tree;
tv.Nodes.Clear();
TreeNode root = new TreeNode();
root.Name = projectName;
root.Text = projectName;
root.ToolTipText = projectName;
root.Tag = AppConfigHandler.Instance.CurrentProjectWizardName;
root.ImageIndex = 0;
root.SelectedImageIndex = 0;
TreeNode nodeForm = new TreeNode();
nodeForm.Name = FixNodeName.NodeFormName;
nodeForm.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Gui_Edit_Pad_ProjectWindow_Forms")); //"画面";
nodeForm.Tag = FixNodeName.NodeFormName;
nodeForm.ImageIndex = 1;
nodeForm.SelectedImageIndex = 1;
root.Nodes.Add(nodeForm);
TreeNode nodeDeviceDriver = new TreeNode();
nodeDeviceDriver.Name = FixNodeName.NodeDeviceName;
nodeDeviceDriver.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Gui_Edit_Pad_ProjectWindow_Devices")); //"设备";
nodeDeviceDriver.Tag = FixNodeName.NodeDeviceName;
nodeDeviceDriver.ImageIndex = 2;
nodeDeviceDriver.SelectedImageIndex = 2;
root.Nodes.Add(nodeDeviceDriver);
TreeNode nodeDataSource = new TreeNode();
nodeDataSource.Name = FixNodeName.NodeDataSourceName;
nodeDataSource.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Gui_Edit_Pad_ProjectWindow_DataSources")); //"数据源";
nodeDataSource.Tag = FixNodeName.NodeDataSourceName;
nodeDataSource.ImageIndex = 6;
nodeDataSource.SelectedImageIndex = 6;
root.Nodes.Add(nodeDataSource);
TreeNode nodeAction = new TreeNode();
nodeAction.Name = FixNodeName.NodeCommandName;
nodeAction.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Gui_Edit_Pad_ProjectWindow_Commands")); //"命令";
nodeAction.Tag = FixNodeName.NodeCommandName;
nodeAction.ImageIndex = 5;
nodeAction.SelectedImageIndex = 5;
root.Nodes.Add(nodeAction);
tv.Nodes.Add(root);
tv.ExpandAll();
tv.SaveToXml(fullFileName);
Mesnac.Equips.Factory.Instance.ConfigFile = Path.Combine(Path.GetDirectoryName(AppConfigHandler.Instance.FullFileName), nodeDeviceDriver.GetPathNoRoot()) + ".xml";
}
#endregion
#region 工程树节点的单击事件(左键激活设计画面,右键弹出新建、临时仿真菜单)
private void tvProject_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
#region 画面处理
if (e.Button == MouseButtons.Right)
{
#region 画面
this.Tree.SelectedNode = e.Node;
//新建画面
if (e.Node.Name == FixNodeName.NodeFormName)
{
this.cmsForm.Show(this.tvProject, e.X, e.Y);
}
//临时仿真与删除画面
if (e.Node.Parent != null && e.Node.Parent.Name == FixNodeName.NodeFormName)
{
this.cmsFormItem.Show(this.tvProject, e.X, e.Y);
this.mnuPreRun.Visible = true;
}
else
{
this.mnuPreRun.Visible = false;
}
#endregion
#region 命令
if (e.Node.Parent != null && e.Node.Parent.Name == FixNodeName.NodeCommandName)
{
this.cmsActionChild.Show(this.tvProject, e.X, e.Y);
}
if (e.Node.Name == FixNodeName.NodeCommandName)
{
this.cmsActionFrom.Show(this.tvProject, e.X, e.Y);
}
#endregion
#region 设备
if (e.Node.Name == FixNodeName.NodeDeviceName)
{
this.cmsDevice.Show(this.tvProject, e.X, e.Y);
}
#endregion
#region 数据源
if (e.Node.Name == FixNodeName.NodeDataSourceName)
{
this.cmsResourceForm.Show(this.tvProject, e.X, e.Y);
}
#endregion
}
else if (e.Button == MouseButtons.Left)
{
//鼠标点击某个画面时,同时激活对应的画面
this.Tree.SelectedNode = e.Node;
if (e.Node.Parent != null && (e.Node.Parent.Name == FixNodeName.NodeFormName))
{
foreach (IViewContent vc in WorkbenchSingleton.Workbench.ViewContentCollection)
{
if (vc.ViewContentID.StartsWith(e.Node.Name))
{
WorkbenchSingleton.Workbench.WorkbenchLayout.ShowView(vc);
break;
}
}
}
//鼠标点击设备的处理
}
#endregion
}
#endregion
#region 工程树的双击事件(激活画面或设备的设计画面)
private void tvProject_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
//加载显示画面
if (e.Button == MouseButtons.Left && e.Clicks == 2)
{
//鼠标点击某个画面时,同时激活对应的画面
bool flag = false;
this.Tree.SelectedNode = e.Node;
if (e.Node.Parent!= null && e.Node.Parent.Name == FixNodeName.NodeFormName)
{
IWorkbenchWindow window = WorkbenchSingleton.Workbench.GetViewByViewContentID(e.Node.Name);
if (window != null)
{
flag = true;
try
{
window.SelectWindow();
}
catch
{
window.CloseWindow(true);
}
}
if (!flag)
{
string fileName = Path.Combine(Path.GetDirectoryName(AppConfigHandler.Instance.FullFileName), e.Node.GetPathNoRoot()) + ".xml";
//this.TopContainer.CreateNewForm(fileName);
NewFormCommand cmm = new NewFormCommand(fileName);
cmm.Run();
}
}
else if (e.Node.Name == FixNodeName.NodeDeviceName || (e.Node.Parent != null && e.Node.Parent.Name == FixNodeName.NodeDeviceName))
{
foreach (IViewContent vc in WorkbenchSingleton.Workbench.ViewContentCollection)
{
if (vc.TitleName.StartsWith("DataConfig"))
{
WorkbenchSingleton.Workbench.ShowView(vc);
flag = true;
break;
}
}
if (!flag)
{
//Mesnac.Equips.Factory.Instance.ManageEquip(AppConfigHandler.Instance.CurrentProjectWizardName, this.TopContainer.DockPanelMain);
Mesnac.Equips.Factory.Instance.ManageEquip(WorkbenchSingleton.Workbench);
}
}
else if (e.Node.Name == FixNodeName.NodeDataSourceName || (e.Node.Parent != null && e.Node.Parent.Name == FixNodeName.NodeDataSourceName))
{
this.DataSourceManager();
}
else if (e.Node.Name == FixNodeName.NodeCommandName || (e.Node.Parent != null && e.Node.Parent.Name == FixNodeName.NodeCommandName))
{
if (e.Node.ToolTipText == CreateType.Command.ToString())
{
FrmEventAction form = new FrmEventAction();
List<Mesnac.Controls.Base.DesignAction> list = new List<Mesnac.Controls.Base.DesignAction>();
GetActionXML(out list, e.Node);
form.ActionList = list;
form.ShowDialog();
if (form.DialogResult == DialogResult.OK)
{
list = form.ActionList;
SaveActionXML(list, e.Node.Tag.ToString());
}
}
}
}
}
/// <summary>
/// 保存ActionXML
/// </summary>
/// <param name="name"></param>
/// <param name="remark"></param>
/// <param name="uid"></param>
public void SaveActionXML(List<Mesnac.Controls.Base.DesignAction> list, string node)
{
XmlDocument dom = new XmlDocument();
string path = Path.Combine(Path.GetDirectoryName(AppConfigHandler.Instance.FullFileName), Mesnac.Basic.FixNodeName.NodeCommandName + ".xml");
dom.Load(path);
int cnodes = dom.DocumentElement.ChildNodes.Count;
for (int i = 0; i < cnodes; i++)
{
XmlNode xnode = dom.DocumentElement.ChildNodes[0];
if (xnode.Name == "Property" && xnode.Attributes.Count > 0 && xnode.Attributes[0].Value == node)
{
dom.DocumentElement.RemoveChild(xnode);
}
}
foreach (Mesnac.Controls.Base.DesignAction d in list)
{
XmlElement rootxe = dom.DocumentElement;
XmlElement xe = dom.CreateElement("Property");
xe.SetAttribute("actionname", node);
xe.SetAttribute("name", d.Name);
xe.SetAttribute("remark", d.Remark);
xe.SetAttribute("guid", d.GUID);
rootxe.AppendChild(xe);
dom.Save(path);
}
}
/// <summary>
/// 保存ActionXML
/// </summary>
/// <param name="name"></param>
/// <param name="remark"></param>
/// <param name="uid"></param>
public void GetActionXML(out List<Mesnac.Controls.Base.DesignAction> list, TreeNode node)
{
list = new List<Mesnac.Controls.Base.DesignAction>();
XmlDocument dom = new XmlDocument();
string path = Path.Combine(Path.GetDirectoryName(AppConfigHandler.Instance.FullFileName), Mesnac.Basic.FixNodeName.NodeCommandName + ".xml");
dom.Load(path);
foreach (XmlElement x in dom.DocumentElement.ChildNodes)
{
if (x.Name == "Property" && x.GetAttribute("guid") != "" && x.GetAttribute("actionname") == node.Tag.ToString())
{
Mesnac.Controls.Base.DesignAction d = new Mesnac.Controls.Base.DesignAction();
d.Name = x.GetAttribute("name");
d.Remark = x.GetAttribute("remark");
d.GUID = x.GetAttribute("guid");
list.Add(d);
}
}
}
#endregion
#region 新建画面与删除画面
/// <summary>
/// 新建画面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuNewForm_Click(object sender, EventArgs e)
{
switch (this.Tree.SelectedNode.Name)
{
case FixNodeName.NodeFormName:
//string frmName = this.TopContainer.CreateNewForm(); //创建画面,并获得画面文件名
string frmName = new NewFormCommand().CreateNewForm(); //创建画面,并获得画面文件名
break;
case FixNodeName.NodeDeviceName: //创建虚拟设备
ICSharpCode.Core.LoggingService<ProjectWindow>.Info("创建虚拟设备,请等待处理...");
break;
}
}
/// <summary>
/// 删除画面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuDeleteFormItem_Click(object sender, EventArgs e)
{
string fileName = String.Empty;
TreeNode node = this.Tree.SelectedNode;
string msg1 = StringParser.Parse(ResourceService.GetString("Dialog_MessageBox_DeleteAsk")); //确定要删除吗?
DialogResult result = MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
#region 删除画面节点
fileName = Path.Combine(AppConfigHandler.Instance.FullFileName, node.GetPathNoRoot()) + ".xml";
//1、关闭设计器
IWorkbenchWindow window = Workbench.WorkbenchSingleton.Workbench.GetViewByViewContentID(node.Name);
if (window != null)
{
Workbench.WorkbenchSingleton.Workbench.CloseView(window.ViewContent);
}
//2、删除文件
if (File.Exists(fileName))
{
File.Delete(fileName);
}
//3、删除节点并更新工程文件
node.Parent.Nodes.Remove(node);
this.Tree.SaveToXml(AppConfigHandler.Instance.FullFileName);
#endregion
}
}
#endregion
#region 画面临时仿真
/// <summary>
/// 画面临时仿真
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuPreRun_Click(object sender, EventArgs e)
{
this.RunSimulation();
}
public void RunSimulation()
{
TreeNode node = this.Tree.SelectedNode;
//初始化运行引擎
//Mesnac.Gui.Common.RunEngine.Instance.Init(Path.GetDirectoryName(AppConfigHandler.Instance.FullFileName), true);
Mesnac.Gui.Common.RunEngine.Instance.Init(Path.GetDirectoryName(AppConfigHandler.Instance.FullFileName), false, false, false);
Mesnac.Gui.Common.RunEngine.Instance.Simulation(node, FormShowType.Form);
}
#endregion
#region 设备管理
private void mnuDeviceManager_Click(object sender, EventArgs e)
{
Mesnac.Equips.Factory.Instance.ManageEquip(WorkbenchSingleton.Workbench);
}
#endregion
#region 显示数据源管理
/// <summary>
/// 显示数据源管理
/// </summary>
private void DataSourceManager()
{
FrmDataSource frmDataSource = null;
IWorkbenchWindow window = WorkbenchSingleton.Workbench.GetViewByType(typeof(Mesnac.Gui.Edit.ViewContent.FrmDataSource));
if (window != null)
{
frmDataSource = window.ViewContent as FrmDataSource;
}
if (frmDataSource == null)
{
frmDataSource = new FrmDataSource();
}
WorkbenchSingleton.Workbench.ShowView(frmDataSource);
}
#endregion
#region 事件处理
private void ProjectWindow_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
private void DesigerMenuItem_Click(object sender, EventArgs e)
{
switch (this.Tree.SelectedNode.Name)
{
case FixNodeName.NodeFormName:
//string frmName = this.TopContainer.CreateNewForm(); //创建画面,并获得画面文件名
string frmName = new NewFormCommand().CreateNewForm();
break;
case FixNodeName.NodeDeviceName: //创建虚拟设备
MessageBox.Show("等待处理...ProjectWindow.cs");
break;
}
}
private void mnuActionStrip_Click(object sender, EventArgs e)
{
switch (this.Tree.SelectedNode.Name)
{
case FixNodeName.NodeCommandName:
string frmName = new NewFormCommand().CreateActionFrm();
if (frmName == "")
return;
List<Mesnac.Controls.Base.DesignAction> list = new List<Mesnac.Controls.Base.DesignAction>();
FrmEventAction form = new FrmEventAction();
form.ShowDialog();
//弹出命令对话框
if (form.DialogResult == DialogResult.OK)
{
if (form.DialogResult == DialogResult.OK)
{
list = form.ActionList;
SaveActionXML(list, frmName);
}
}
break;
}
}
/// <summary>
/// 删除命令
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void smuDelete_Click(object sender, EventArgs e)
{
TreeNode node = this.Tree.SelectedNode;
DialogResult result = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
//删除xml中节点
string fileName = Path.Combine(Path.GetDirectoryName(AppConfigHandler.Instance.FullFileName), Mesnac.Basic.FixNodeName.NodeCommandName + ".xml");
XmlDocument dom = new XmlDocument();
dom.Load(fileName);
int cnodes = dom.DocumentElement.ChildNodes.Count;
for (int i = 0; i < cnodes; i++)
{
XmlNode xnode = dom.DocumentElement.ChildNodes[0];
if (xnode.Name == "Property" && xnode.Attributes.Count > 0 && xnode.Attributes[0].Value == node.Tag.ToString())
{
dom.DocumentElement.RemoveChild(xnode);
}
}
///////////////////
node.Parent.Nodes.Remove(node);
this.Tree.SaveToXml(AppConfigHandler.Instance.FullFileName);
}
}
private void tsmManger_Click(object sender, EventArgs e)
{
this.DataSourceManager();
}
#endregion
}
}