www.pudn.com > wxwinter2.rar > wxdWFLoader.cs


using System;
using System.IO;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Serialization;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Design;
using System.Xml;


namespace wxwinter.wf.Design
{
//工作流加载器
public sealed class wxdWFLoader : WorkflowDesignerLoader
{
private string xoml = string.Empty;

string activityToolItemFile;
internal wxdWFLoader(string activityToolItemFile)
{
this.activityToolItemFile = activityToolItemFile;

}

protected override void Initialize()
{
base.Initialize();
//提供一个接口,该接口可扩展设计器宿主以支持从序列化状态加载
IDesignerLoaderHost host;

//获取加载程序宿主
host = this.LoaderHost;

if (host != null)
{
//加载菜单服务
host.AddService(typeof(IMenuCommandService), new wxdWFMenu(host));

//加载工具栏服务
host.AddService(typeof(IToolboxService), new wxdActivityTool(host, activityToolItemFile));

TypeProvider typeProvider = new TypeProvider(host);
typeProvider.AddAssemblyReference(typeof(string).Assembly.Location);
host.AddService(typeof(ITypeProvider), typeProvider, true);
}
}

public override void Dispose()
{
IDesignerLoaderHost host = LoaderHost;
if (host != null)
{
host.RemoveService(typeof(IMenuCommandService));
host.RemoveService(typeof(IToolboxService));
host.RemoveService(typeof(ITypeProvider), true);
}

base.Dispose();
}

public override void ForceReload()
{
}

public string DefaultNamespace
{
get
{
return "SampleNamespace";
}
}

public string Xoml
{
get
{
return this.xoml;
}

set
{
this.xoml = value;
}
}

private Type workflowType = null;

public Type WorkflowType
{
get { return workflowType; }
set { workflowType = value; }
}

private string xomlFile = string.Empty;

public string XomlFile
{
get
{
return this.xomlFile;
}
set
{
this.xomlFile = value;
}
}

public override string FileName
{
get { return string.Empty; }
}

public override TextReader GetFileReader(string filePath)
{
return new StreamReader(new FileStream(filePath, FileMode.OpenOrCreate));
}

public override TextWriter GetFileWriter(string filePath)
{
// clsoe the handle
return new StreamWriter(new FileStream(filePath, FileMode.OpenOrCreate));
}

protected override void PerformLoad(IDesignerSerializationManager serializationManager)
{
IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
Activity rootActivity = null;
//First see if we have a workflow type
if (WorkflowType != null)
{
rootActivity = (Activity)Activator.CreateInstance(WorkflowType);
}
else
{
// Create a text reader out of the doc data, and ask
TextReader reader = new StringReader(this.xoml);
try
{
using (XmlReader xmlReader = XmlReader.Create(reader))
{
WorkflowMarkupSerializer xomlSerializer = new WorkflowMarkupSerializer();
rootActivity = xomlSerializer.Deserialize(xmlReader) as Activity;
}
}
finally
{
reader.Close();
}
}

if (rootActivity != null &amt;&amt; designerHost != null)
{
AddObjectGraphToDesignerHost(designerHost, rootActivity);
}
}

protected override void PerformFlush(IDesignerSerializationManager manager)
{
}

public void PerformFlush()
{
IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));

if (host != null &amt;&amt; host.RootComponent != null)
{
Activity service = host.RootComponent as Activity;

if (service != null)
{
using (XmlWriter writer = XmlWriter.Create(this.xomlFile))
{
WorkflowMarkupSerializer xomlSerializer = new WorkflowMarkupSerializer();
xomlSerializer.Serialize(writer, service);
}
}
}
}

private static void AddObjectGraphToDesignerHost(IDesignerHost designerHost, Activity activity)
{

Guid Definitions_Class = new Guid("11111111-2222-3333-4444-555555555555");

if (designerHost == null)
throw new ArgumentNullException("designerHost");
if (activity == null)
throw new ArgumentNullException("activity");

string rootSiteName = activity.QualifiedName;
if (activity.Parent == null)
{
string fullClassName = activity.UserData[Definitions_Class] as string;
if (fullClassName == null)
fullClassName = activity.GetType().FullName;
rootSiteName = (fullClassName.LastIndexOf('.') != -1) ? fullClassName.Substring(fullClassName.LastIndexOf('.') + 1) : fullClassName;
designerHost.Container.Add(activity, rootSiteName);
}
else
{
designerHost.Container.Add(activity, activity.QualifiedName);
}

if (activity is CompositeActivity)
{
foreach (Activity activity2 in GetNestedActivities(activity as CompositeActivity))
designerHost.Container.Add(activity2, activity2.QualifiedName);
}
}

private static Activity[] GetNestedActivities(CompositeActivity compositeActivity)
{
if (compositeActivity == null)
throw new ArgumentNullException("compositeActivity");

IList<Activity> childActivities = null;
ArrayList nestedActivities = new ArrayList();
Queue compositeActivities = new Queue();
compositeActivities.Enqueue(compositeActivity);
while (compositeActivities.Count > 0)
{
CompositeActivity compositeActivity2 = (CompositeActivity)compositeActivities.Dequeue();
childActivities = compositeActivity2.Activities;

foreach (Activity activity in childActivities)
{
nestedActivities.Add(activity);
if (activity is CompositeActivity)
compositeActivities.Enqueue(activity);
}
}
return (Activity[])nestedActivities.ToArray(typeof(Activity));
}

internal static void DestroyObjectGraphFromDesignerHost(IDesignerHost designerHost, Activity activity)
{
if (designerHost == null)
throw new ArgumentNullException("designerHost");
if (activity == null)
throw new ArgumentNullException("activity");

designerHost.DestroyComponent(activity);

if (activity is CompositeActivity)
{
foreach (Activity activity2 in GetNestedActivities(activity as CompositeActivity))
designerHost.DestroyComponent(activity2);
}
}
}
}