|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Text;
|
|
|
using System.Windows.Forms;
|
|
|
using System.Reflection;
|
|
|
using ICSharpCode.Core;
|
|
|
|
|
|
namespace Mesnac.Gui.Workbench
|
|
|
{
|
|
|
using Mesnac.Core.Service;
|
|
|
using Mesnac.PlugIn.Workbench;
|
|
|
/// <summary>
|
|
|
/// 工作区单例类
|
|
|
/// </summary>
|
|
|
public static class WorkbenchSingleton
|
|
|
{
|
|
|
private static STAThreadCaller caller;
|
|
|
private static DefaultWorkbench workbench = null;
|
|
|
private static WorkbenchLayout layout = null;
|
|
|
/// <summary>
|
|
|
/// 工作台初始化完毕事件
|
|
|
/// </summary>
|
|
|
public static event EventHandler InitFinished;
|
|
|
|
|
|
static WorkbenchSingleton()
|
|
|
{
|
|
|
PropertyService.PropertyChanged += new PropertyChangedEventHandler(PropertyService_PropertyChanged);
|
|
|
ResourceService.LanguageChanged += delegate { workbench.RedrawAllComponents(); };
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 初始化工作台
|
|
|
/// </summary>
|
|
|
public static void InitializeWorkbench()
|
|
|
{
|
|
|
workbench = new DefaultWorkbench();
|
|
|
caller = new STAThreadCaller(workbench);
|
|
|
//创建布局对象,并设置工作台的布局
|
|
|
layout = new WorkbenchLayout(workbench);
|
|
|
workbench.WorkbenchLayout = layout;
|
|
|
//调用工作台的初始化工作空间方法
|
|
|
workbench.InitializeWorkspace();
|
|
|
//触发初始化完毕事件
|
|
|
if (InitFinished != null)
|
|
|
{
|
|
|
InitFinished(null, EventArgs.Empty);
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 属性变化事件
|
|
|
/// </summary>
|
|
|
/// <param name="sender"></param>
|
|
|
/// <param name="e"></param>
|
|
|
static void PropertyService_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
{
|
|
|
if (e.OldValue != e.NewValue && workbench != null)
|
|
|
{
|
|
|
if (e.Key == "cultureName")
|
|
|
{
|
|
|
workbench.UpdateRenderer();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#region 线程安全调用器
|
|
|
|
|
|
#region 线程安全调用器类
|
|
|
/// <summary>
|
|
|
/// 线程安全调用器类
|
|
|
/// </summary>
|
|
|
private class STAThreadCaller
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 定义平台委托类
|
|
|
/// </summary>
|
|
|
/// <param name="target">目标对象</param>
|
|
|
/// <param name="methodName">方法名称</param>
|
|
|
/// <param name="arguments">参数数组</param>
|
|
|
/// <returns></returns>
|
|
|
delegate object PerformClassDelegate(object target, string methodName, object[] arguments);
|
|
|
|
|
|
private Control ctl;
|
|
|
private PerformClassDelegate performClassDelegate;
|
|
|
|
|
|
#if DEBUG
|
|
|
private string callerStack;
|
|
|
#endif
|
|
|
|
|
|
public STAThreadCaller(Control ctl)
|
|
|
{
|
|
|
this.ctl = ctl;
|
|
|
this.performClassDelegate = new PerformClassDelegate(this.DoPerformCall);
|
|
|
}
|
|
|
|
|
|
public object Call(Delegate method, object[] arguments)
|
|
|
{
|
|
|
if (method == null)
|
|
|
{
|
|
|
throw new ArgumentNullException("空方法");
|
|
|
}
|
|
|
return this.ctl.Invoke(method, arguments);
|
|
|
}
|
|
|
|
|
|
public object Call(object target, string methodName, object[] arguments)
|
|
|
{
|
|
|
if (target == null)
|
|
|
{
|
|
|
throw new ArgumentNullException("空对象");
|
|
|
}
|
|
|
#if DEBUG
|
|
|
this.callerStack = Environment.StackTrace;
|
|
|
#endif
|
|
|
return this.ctl.Invoke(this.performClassDelegate, new object[] { target, methodName, arguments });
|
|
|
}
|
|
|
|
|
|
public void BeginCall(Delegate method, object[] arguments)
|
|
|
{
|
|
|
if (method == null)
|
|
|
{
|
|
|
throw new ArgumentNullException("空方法");
|
|
|
}
|
|
|
#if DEBUG
|
|
|
this.callerStack = Environment.StackTrace;
|
|
|
#endif
|
|
|
this.ctl.BeginInvoke(method, arguments);
|
|
|
}
|
|
|
|
|
|
public void BeginCall(object target, string methodName, object[] arguments)
|
|
|
{
|
|
|
if (target == null)
|
|
|
{
|
|
|
throw new ArgumentNullException("空对象");
|
|
|
}
|
|
|
#if DEBUG
|
|
|
this.callerStack = Environment.StackTrace;
|
|
|
#endif
|
|
|
this.ctl.BeginInvoke(this.performClassDelegate, new object[] { target, methodName, arguments });
|
|
|
}
|
|
|
|
|
|
private object DoPerformCall(object target, string methodName, object[] arguments)
|
|
|
{
|
|
|
MethodInfo methodInfo = null;
|
|
|
if (target is Type)
|
|
|
{
|
|
|
methodInfo = ((Type)target).GetMethod(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
methodInfo = target.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
}
|
|
|
|
|
|
if (methodInfo == null)
|
|
|
{
|
|
|
throw new System.ArgumentException("未找到该方法:" + methodName);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (target is Type)
|
|
|
{
|
|
|
return methodInfo.Invoke(null, arguments);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return methodInfo.Invoke(target, arguments);
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
if (ex is TargetInvocationException && ex.InnerException != null)
|
|
|
{
|
|
|
ex = ex.InnerException;
|
|
|
}
|
|
|
MesnacServiceManager.Instance.MessageService.ShowException(ex, "发生异常。");
|
|
|
#if DEBUG
|
|
|
MesnacServiceManager.Instance.LoggingService.Info("线程堆栈跟踪:\n" + this.callerStack);
|
|
|
#endif
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
/// <summary>
|
|
|
/// 调用方在对控件进行方法调用时,是否必须调用Invoke方法
|
|
|
/// </summary>
|
|
|
public static bool InvokeRequired
|
|
|
{
|
|
|
get { return (workbench as Form).InvokeRequired; }
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 产生一个线程安全的GUI调用
|
|
|
/// 方法会等待操作结果返回。
|
|
|
/// </summary>
|
|
|
/// <param name="target"></param>
|
|
|
/// <param name="methodName"></param>
|
|
|
/// <param name="arguments"></param>
|
|
|
/// <returns></returns>
|
|
|
public static object SafeThreadCall(object target, string methodName, params object[] arguments)
|
|
|
{
|
|
|
return caller.Call(target, methodName, arguments);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 产生一个线程安全的GUI调用
|
|
|
/// 方法会等待操作结果返回。
|
|
|
/// </summary>
|
|
|
/// <param name="method"></param>
|
|
|
/// <param name="arguments"></param>
|
|
|
/// <returns></returns>
|
|
|
public static object SafeThreadCall(Delegate method, params object[] arguments)
|
|
|
{
|
|
|
return caller.Call(method, arguments);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 产生一个线程安全的异步GUI调用
|
|
|
/// </summary>
|
|
|
/// <param name="target"></param>
|
|
|
/// <param name="methodName"></param>
|
|
|
/// <param name="arguments"></param>
|
|
|
public static void SafeThreadAsyncCall(object target, string methodName, params object[] arguments)
|
|
|
{
|
|
|
caller.BeginCall(target, methodName, arguments);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 产生一个线程安全的异步GUI调用
|
|
|
/// </summary>
|
|
|
/// <param name="method"></param>
|
|
|
/// <param name="arguments"></param>
|
|
|
public static void SafeThreadAsyncCall(Delegate method, params object[] arguments)
|
|
|
{
|
|
|
caller.BeginCall(method, arguments);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
/// <summary>
|
|
|
/// 主窗体
|
|
|
/// </summary>
|
|
|
public static Form MainForm
|
|
|
{
|
|
|
get { return workbench as Form; }
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 工作台
|
|
|
/// </summary>
|
|
|
public static IWorkbench Workbench
|
|
|
{
|
|
|
get { return workbench; }
|
|
|
}
|
|
|
}
|
|
|
}
|