using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ICSharpCode.Core;
using Mesnac.PlugIn.View;
using Mesnac.Gui.Workbench;
using Mesnac.Core.Service;
using Mesnac.Basic;
using Mesnac.Gui.Run.Global;
using Mesnac.Gui.Common;
namespace Mesnac.Gui.Run.ViewContent
{
#region 通用功能定义
#endregion
#region 文件菜单
#region 系统工程配置
///
/// 系统工程配置
///
public class SysConfigCommand : AbstractCommand
{
public override void Run()
{
IViewContent frmSysConfig = null;
IWorkbenchWindow window = WorkbenchSingleton.Workbench.GetViewByType(typeof(FrmSysConfig));
if (window != null)
{
frmSysConfig = window.ViewContent;
}
else
{
frmSysConfig = new FrmSysConfig();
}
WorkbenchSingleton.Workbench.ShowView(frmSysConfig);
}
}
#endregion
#region 刷新自定义插件
///
/// 刷新自定义插件
///
public class RefreshPlugInCommand : AbstractCommand
{
public override void Run()
{
WorkbenchSingleton.Workbench.RefreshPlugIn();
}
}
#endregion
#region 全屏切换
///
/// 全屏切换
///
public class ToggleFullscreenCommand : AbstractCommand
{
public override void Run()
{
WorkbenchSingleton.Workbench.FullScreen = !WorkbenchSingleton.Workbench.FullScreen;
}
}
#endregion
#region 退出应用程序
///
/// 退出应用程序
///
public class ExitCommand : AbstractCommand
{
public override void Run()
{
string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption"));
string msg = StringParser.Parse(ResourceService.GetString("Dialog_MessageBox_ExtAsk"));
DialogResult result = MessageBox.Show(msg, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
WorkbenchSingleton.Workbench.CloseFlag = false; //设置系统关闭标识
(WorkbenchSingleton.Workbench as System.Windows.Forms.Form).Close();
Application.ExitThread();
}
}
}
#endregion
#endregion
#region 帮助菜单
#region 关于
public class AboutCommand : AbstractCommand
{
public override void Run()
{
Dialog.FrmAbout frmAbout = new Dialog.FrmAbout();
frmAbout.ShowDialog();
}
}
#endregion
#endregion
#region 语言菜单处理
#region 中文
public class ChineseCommand : AbstractCommand
{
public override void Run()
{
if (System.Threading.Thread.CurrentThread.CurrentUICulture == null || System.Threading.Thread.CurrentThread.CurrentUICulture.Name != "zh-CN")
{
LanguageHelper.SetCurrentCultureInfo("zh-CN");
WorkbenchSingleton.Workbench.CloseAllViews();
string projectPath = Path.Combine(Application.StartupPath, AppConfigHandler.Instance.MCProjectPath) + "." + System.Threading.Thread.CurrentThread.CurrentUICulture.Name; //组态工程路径
if (!System.IO.Directory.Exists(projectPath))
{
ICSharpCode.Core.LoggingService.Warn(String.Format("目录:{0},不存在,重新加载默认工程目录!", projectPath));
projectPath = Path.Combine(Application.StartupPath, AppConfigHandler.Instance.MCProjectPath);
}
if (Mesnac.Gui.Common.RunEngine.Instance.Init(projectPath, true, true, true)) //初始化运行引擎
{
AppConfigHandler.Instance.InitCustomerMenuAndToolStrip(WorkbenchSingleton.Workbench.TopMenu, WorkbenchSingleton.Workbench.ToolStrip); //初始化自定义系统菜单和工具栏
}
}
}
}
#endregion
#region 英文
public class EnglishCommand : AbstractCommand
{
public override void Run()
{
if (System.Threading.Thread.CurrentThread.CurrentUICulture == null || System.Threading.Thread.CurrentThread.CurrentUICulture.Name != "en-US")
{
LanguageHelper.SetCurrentCultureInfo("en-US");
WorkbenchSingleton.Workbench.CloseAllViews();
string projectPath = Path.Combine(Application.StartupPath, AppConfigHandler.Instance.MCProjectPath) + "." + System.Threading.Thread.CurrentThread.CurrentUICulture.Name; //组态工程路径
if (!System.IO.Directory.Exists(projectPath))
{
ICSharpCode.Core.LoggingService.Warn(String.Format("目录:{0},不存在,重新加载默认工程目录!", projectPath));
projectPath = Path.Combine(Application.StartupPath, AppConfigHandler.Instance.MCProjectPath);
}
if (Mesnac.Gui.Common.RunEngine.Instance.Init(projectPath, true, true, true)) //初始化运行引擎
{
AppConfigHandler.Instance.InitCustomerMenuAndToolStrip(WorkbenchSingleton.Workbench.TopMenu, WorkbenchSingleton.Workbench.ToolStrip); //初始化自定义系统菜单和工具栏
}
}
}
}
#endregion
#endregion
#region 测试命令
public class TestCommand : AbstractCommand
{
public override void Run()
{
LoggingService.Debug("测试命令执行..." + this.Owner.ToString());
LoggingService.Debug("RoleId = " + UserInfo.Instance.RoleGUID);
LoggingService.Debug("UserId = " + UserInfo.Instance.UserGUID);
LoggingService.Debug("UserName = " + UserInfo.Instance.UserName);
}
}
#endregion
#region 测试 资源回收
///
/// 资源回收
///
public class ReleaseResourceCommand : AbstractCommand
{
public override void Run()
{
//System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.Release));
string[] keys = new string[WorkbenchSingleton.Workbench.ViewContentDic.Keys.Count];
WorkbenchSingleton.Workbench.ViewContentDic.Keys.CopyTo(keys, 0);
foreach (string key in keys)
{
IViewContent content = WorkbenchSingleton.Workbench.ViewContentDic[key];
WorkbenchSingleton.Workbench.ViewContentDic.Remove(key);
IWorkbenchWindow window = content.MyOwner as IWorkbenchWindow;
//content.IsRealClose = true;
//content.Dispose();
window.CloseWindow(true);
}
//WorkbenchSingleton.Workbench.ViewContentDic.Clear();
System.GC.Collect();
LoggingService.Debug("垃圾回收执行...");
LoggingService.Debug("当前组态工程ViewContent数量:" + WorkbenchSingleton.Workbench.ViewContentDic.Count);
}
public void Release(object obj)
{
string[] keys = new string[WorkbenchSingleton.Workbench.ViewContentDic.Keys.Count];
WorkbenchSingleton.Workbench.ViewContentDic.Keys.CopyTo(keys, 0);
foreach (string key in keys)
{
IViewContent content = WorkbenchSingleton.Workbench.ViewContentDic[key];
WorkbenchSingleton.Workbench.ViewContentDic.Remove(key);
IWorkbenchWindow window = content.MyOwner as IWorkbenchWindow;
content.IsRealClose = true;
content.Dispose();
window.CloseWindow(true);
}
//WorkbenchSingleton.Workbench.ViewContentDic.Clear();
System.GC.Collect();
LoggingService.Debug("垃圾回收执行...");
LoggingService.Debug("当前组态工程ViewContent数量:" + WorkbenchSingleton.Workbench.ViewContentDic.Count);
}
}
#endregion
#region 菜单项启用、禁用操作(权限控制)
///
/// 刷新插件、工程配置权限控制(只允许管理员操作)
///
public class MCRunSuperConditionEvaluator : IConditionEvaluator
{
public bool IsValid(object owner, ICSharpCode.Core.Condition condition)
{
//如果是超级管理员或没经过登录(独立运行)则可用
if (UserInfo.Instance.RoleGUID == "-mesnac" || UserInfo.Instance.RoleGUID == "-1")
{
return true;
}
else
{
return false;
}
}
}
#endregion
}