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.

346 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using ICSharpCode.Core;
using Mesnac.Basic;
using Mesnac.Gui.Common;
using Mesnac.Gui.Edit.Common;
using Mesnac.Gui.Edit.Global;
namespace Mesnac.Gui.Edit.Dialog
{
public partial class FrmNewProject : Form
{
#region 定义变量
private bool exitFlag = true; //是否退出
private bool isLoad = true; //只有空项目的时候为创建,其他都为加载,就是在主窗体中的工程面中数据的初始化方式
private readonly string ProjectExtName = ".mprj"; //组态工程文件扩展名
private readonly string projectWizardFile = Application.StartupPath + AppConfigHandler.Instance.ProjectWizardFile;
private static Dictionary<string, ProjectWizard> _projectWizardList = null; //工程模板
private string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption"));
private string msg = String.Empty;
#endregion
#region 公共属性
private string _fullFileName;
/// <summary>
/// 包括完整路径的工程文件名
/// </summary>
public string FullFileName
{
get { return _fullFileName; }
set { _fullFileName = value; }
}
/// <summary>
/// 基于语言区域的工程向导文件路径
/// </summary>
public string CultureProjectWizardFile
{
get
{
string culture = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
string wizardFile = Path.Combine(Path.GetDirectoryName(this.projectWizardFile), Path.GetFileNameWithoutExtension(this.projectWizardFile));
wizardFile += "." + culture + Path.GetExtension(this.projectWizardFile);
if (!File.Exists(wizardFile))
{
wizardFile = this.projectWizardFile;
}
return wizardFile;
}
}
#endregion
public FrmNewProject()
{
InitializeComponent();
this.InitUIMethod();
if (_projectWizardList == null)
{
_projectWizardList = XmlHandler.ParseFromProjectWizardXml(this.CultureProjectWizardFile);
}
this.InitProjectWizardList();
//订阅工作台界面刷新事件
Workbench.WorkbenchSingleton.Workbench.RefreshUI += new EventHandler(Workbench_RefreshUI);
}
/// <summary>
/// 刷新工程向导语言界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Workbench_RefreshUI(object sender, EventArgs e)
{
_projectWizardList = XmlHandler.ParseFromProjectWizardXml(this.CultureProjectWizardFile);
}
/// <summary>
/// 初始化界面元素
/// </summary>
public void InitUIMethod()
{
this.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_Text"));
this.label1.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_label1"));
this.label2.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_label2"));
this.label3.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_label3"));
this.btnBig.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_btnBig"));
this.btnSmall.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_btnSmall"));
this.btnBrowser.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_btnBrowser"));
this.btnOK.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnOK"));
this.btnCancel.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnCancel"));
}
/// <summary>
/// 初始化工程向导列表
/// </summary>
public void InitProjectWizardList()
{
this.lvTemplate.Clear();
foreach (string key in _projectWizardList.Keys)
{
ProjectWizard wizard = _projectWizardList[key];
ListViewItem lvItem = new ListViewItem();
lvItem.Text = wizard.Name;
if (wizard.ImageIndex == -1)
{
lvItem.ImageIndex = 0;
}
else
{
lvItem.ImageIndex = wizard.ImageIndex;
}
this.lvTemplate.Items.Add(lvItem);
}
}
/// <summary>
/// 大图标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBig_Click(object sender, EventArgs e)
{
this.lvTemplate.View = View.LargeIcon;
this.btnBig.Enabled = false;
this.btnSmall.Enabled = true;
}
/// <summary>
/// 小图标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSmall_Click(object sender, EventArgs e)
{
this.lvTemplate.View = View.SmallIcon;
this.btnBig.Enabled = true;
this.btnSmall.Enabled = false;
}
/// <summary>
/// 选择模板事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lvTemplate_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.lvTemplate.SelectedItems.Count > 0)
{
#region 工程名称处理
string path = this.txtPrjPath.Text.Trim();
string key = this.lvTemplate.SelectedItems[0].Text;
string prjName = this.GetNewPrjName(path, key);
this.txtPrjName.Text = prjName;
this.txtInfo.Text = _projectWizardList[key].Description;
#endregion
}
}
/// <summary>
/// 获取一个可用的工程名称
/// </summary>
/// <param name="templateIndex">模板索引</param>
/// <returns></returns>
private string GetNewPrjName(string path, string key)
{
string prjName = String.Empty;
string fullName = String.Empty;
int i = 1;
while (File.Exists(fullName = path + @"\" + (prjName = _projectWizardList[key].EnglishShortName + i) + @"\" + prjName + this.ProjectExtName))
{
i++;
}
return prjName;
}
/// <summary>
/// 选择项目位置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBrowser_Click(object sender, EventArgs e)
{
try
{
DialogResult result = fbDialog.ShowDialog();
if (result == DialogResult.OK)
{
string path = fbDialog.SelectedPath;
this.txtPrjPath.Text = path;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 创建项目
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOK_Click(object sender, EventArgs e)
{
try
{
if (this.lvTemplate.SelectedItems.Count == 0)
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_Msg1")); //请选择一个工程模板!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.exitFlag = false;
}
else if (String.IsNullOrEmpty(this.txtPrjName.Text.Trim()))
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_Msg2")); //请填写工程名称!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.exitFlag = false;
}
else if (String.IsNullOrEmpty(this.txtPrjPath.Text.Trim()))
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_Msg3")); //请选择工程位置!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.exitFlag = false;
}
else
{
string path = this.txtPrjPath.Text.Trim() + @"\" + this.txtPrjName.Text.Trim();
string fullFileName = path + @"\" + this.txtPrjName.Text.Trim() + this.ProjectExtName;
if (File.Exists(fullFileName))
{
this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_Msg4")); //此工程已经存在,请更改工程名称!
MessageBox.Show(this.msg, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.exitFlag = false;
}
else
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path); //创建目录
}
string templateName = this.lvTemplate.SelectedItems[0].Text;
AppConfigHandler.Instance.CurrentProjectWizardName = templateName;
string templatePath = String.Empty;
if (templateName != StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmNewProject_DefaultTemplateName"))) //"空项目"
{
FrmTemplateList frmTemplateList = new FrmTemplateList(templateName);
DialogResult result = frmTemplateList.ShowDialog(this);
if (result != DialogResult.OK)
{
//MessageBox.Show("请选择模板文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.exitFlag = false;
return;
}
else
{
templatePath = frmTemplateList.SelectTemplatePath;
PublicConfig.Instance.CopyFilesDirs(templatePath, path); //复制模板
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
if (file.EndsWith("mprj"))
{
File.Copy(file, fullFileName);
File.Delete(file);
break;
}
}
TreeView tree = new TreeView();
tree.LoadFromXml(fullFileName);
string root = Path.GetFileNameWithoutExtension(fullFileName);
tree.Nodes[0].Name = root;
tree.Nodes[0].Text = root;
tree.Nodes[0].ToolTipText = root;
tree.Nodes[0].Tag = templateName;
tree.SaveToXml(fullFileName);
this.isLoad = true;
}
}
else
{
//创建工程文件
XmlDocument doc = new XmlDocument();
XmlElement eMprj = doc.CreateElement("MesnacPrj"); //创建根元素
doc.AppendChild(eMprj);
doc.Save(fullFileName); //创建工程文件
this.isLoad = false;
}
this.FullFileName = fullFileName; //保存工程文件路径
this.exitFlag = true;
this.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
this.exitFlag = false;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.exitFlag = true;
this.Close();
}
private void FrmNewProject_FormClosing(object sender, FormClosingEventArgs e)
{
if (!this.exitFlag)
{
e.Cancel = true;
}
}
/// <summary>
/// 是否加载
/// </summary>
public bool IsLoad
{
get
{
return this.isLoad;
}
}
}
}