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


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.Activities;
using System.Workflow.ComponentModel.Design;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.ObjectModel;
using System.IO;
using System.Workflow.ComponentModel.Serialization;
using System.Xml;
using System.CodeDom.Compiler;
using System.Workflow.Runtime;
using System.Reflection;
using System.Threading;

namespace wxwinter.wf.Design
{


public class wxdWFStateMachineDesigner : UserControl, IDisposable, IServiceProvider
{




private System.ComponentModel.IContainer components = null;

private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.PropertyGrid propertyGrid;
private System.Windows.Forms.Panel panel4;
private System.Windows.Forms.Splitter splitter2;
private System.Windows.Forms.Panel panel5;
private System.Windows.Forms.Splitter splitter1;


private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.panel2 = new System.Windows.Forms.Panel();
this.panel3 = new System.Windows.Forms.Panel();
this.propertyGrid = new System.Windows.Forms.PropertyGrid();
this.panel4 = new System.Windows.Forms.Panel();
this.splitter2 = new System.Windows.Forms.Splitter();
this.panel5 = new System.Windows.Forms.Panel();
this.splitter1 = new System.Windows.Forms.Splitter();
this.panel3.SuspendLayout();
this.panel4.SuspendLayout();
this.panel5.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(266, 262);
this.panel1.TabIndex = 3;
//
// panel2
//
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(422, 543);
this.panel2.TabIndex = 4;
//
// panel3
//
this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel3.Controls.Add(this.propertyGrid);
this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel3.Location = new System.Drawing.Point(0, 262);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(266, 281);
this.panel3.TabIndex = 6;
//
// propertyGrid
//
this.propertyGrid.Dock = System.Windows.Forms.DockStyle.Fill;
this.propertyGrid.Location = new System.Drawing.Point(0, 0);
this.propertyGrid.Name = "propertyGrid";
this.propertyGrid.Size = new System.Drawing.Size(262, 277);
this.propertyGrid.TabIndex = 0;
//
// panel4
//
this.panel4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel4.Controls.Add(this.splitter1);
this.panel4.Controls.Add(this.panel3);
this.panel4.Controls.Add(this.panel1);
this.panel4.Dock = System.Windows.Forms.DockStyle.Left;
this.panel4.Location = new System.Drawing.Point(0, 0);
this.panel4.Name = "panel4";
this.panel4.Size = new System.Drawing.Size(270, 547);
this.panel4.TabIndex = 7;
//
// splitter2
//
this.splitter2.Location = new System.Drawing.Point(270, 0);
this.splitter2.Name = "splitter2";
this.splitter2.Size = new System.Drawing.Size(3, 547);
this.splitter2.TabIndex = 8;
this.splitter2.TabStop = false;
//
// panel5
//
this.panel5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel5.Controls.Add(this.panel2);
this.panel5.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel5.Location = new System.Drawing.Point(273, 0);
this.panel5.Name = "panel5";
this.panel5.Size = new System.Drawing.Size(426, 547);
this.panel5.TabIndex = 9;
//
// splitter1
//
this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter1.Location = new System.Drawing.Point(0, 262);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(266, 3);
this.splitter1.TabIndex = 7;
this.splitter1.TabStop = false;
//
// WorkflowDesignerControl
//
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.panel5);
this.Controls.Add(this.splitter2);
this.Controls.Add(this.panel4);
this.Name = "wxwinterWFD";
this.Size = new System.Drawing.Size(699, 547);
this.panel3.ResumeLayout(false);
this.panel4.ResumeLayout(false);
this.panel5.ResumeLayout(false);
this.ResumeLayout(false);

}


string activityToolItemFile = "";



private WorkflowView workflowView; //工作查看器
private DesignSurface designSurface;
private wxdWFLoader loader;




System.Workflow.Activities.StateMachineWorkflowActivity workflow;


public wxdWFStateMachineDesigner()
{
this.activityToolItemFile = System.AppDomain.CurrentDomain.BaseDirectory + "ActivityTooIteml.txt";
InitializeComponent();

wxdActivityTool toolbox = new wxdActivityTool(this, activityToolItemFile);
panel1.Controls.Add(toolbox);
toolbox.Dock = DockStyle.Fill;
toolbox.BackColor = BackColor;
toolbox.Font = WorkflowTheme.CurrentTheme.AmbientTheme.Font;

WorkflowTheme.CurrentTheme.ReadOnly = false;
WorkflowTheme.CurrentTheme.AmbientTheme.ShowConfigErrors = false;
WorkflowTheme.CurrentTheme.ReadOnly = true;

this.propertyGrid.BackColor = BackColor;
this.propertyGrid.Font = WorkflowTheme.CurrentTheme.AmbientTheme.Font;

}












/// <summary>
/// Xoml文件名
/// </summary>
public string XomlFile
{
get
{
return this.loader.XomlFile;
}
set
{
this.loader.XomlFile = value;
}
}


/// <summary>
/// Xoml文件名字串
/// </summary>
public string Xoml
{
get
{
string xoml = string.Empty;
if (this.loader != null)
{
try
{
this.loader.Flush();
xoml = this.loader.Xoml;
}
catch
{
}
}
return xoml;
}

set
{
try
{
if (!String.IsNullOrEmpty(value))
LoadWorkflow(value);
}
catch
{
}
}
}


/// <summary>
/// 工作流的名称
/// </summary>
public string WorkflowName
{
get
{
return this.workflow == null ? string.Empty : this.workflow.Name;
}
}


/// <summary>
/// 改变显示比例
/// </summary>
/// <param name="zoomFactor"></param>
public void ProcessZoom(int zoomFactor)
{
this.workflowView.Zoom = zoomFactor;
this.workflowView.Update();
}


/// <summary>
/// 删除选中结点
/// </summary>
public void DeleteSelected()
{
ISelectionService selectionService = (ISelectionService)this.GetService(typeof(ISelectionService));

if (selectionService != null)
{
if (selectionService.PrimarySelection is Activity)
{
Activity activity = (Activity)selectionService.PrimarySelection;

if (activity.Name != this.WorkflowName)
{
activity.Parent.Activities.Remove(activity);
this.workflowView.Update();
}
}
}
}


/// <summary>
/// 从xoml文件中加载工作流
/// </summary>
public void LoadExistingWorkflow()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "xoml files (*.xoml)|*.xoml|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
using (XmlReader xmlReader = XmlReader.Create(openFileDialog.FileName))
{
WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

this.workflow = (System.Workflow.Activities.StateMachineWorkflowActivity)serializer.Deserialize(xmlReader);

this.LoadWorkflow();

this.XomlFile = openFileDialog.FileName;
this.Text = "打开文件 [" + openFileDialog.FileName + "]";
}
}
}


/// <summary>
/// 保存工作流
/// </summary>
/// <returns></returns>
public bool Save()
{
return this.Save(true);
}




protected override void Dispose(bool disposing)
{
if (disposing &amt;&amt; (components != null))
{
UnloadWorkflow();
components.Dispose();
}
base.Dispose(disposing);
}

new public object GetService(Type serviceType)
{
return (this.workflowView != null) ? ((IServiceProvider)this.workflowView).GetService(serviceType) : null;
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ShowDefaultWorkflow();
}


private void LoadWorkflow(string xoml)
{
SuspendLayout();

DesignSurface designSurface = new DesignSurface();
wxdWFLoader loader = new wxdWFLoader(activityToolItemFile);
loader.Xoml = xoml;
designSurface.BeginLoad(loader);
IDesignerHost designerHost;
designerHost = designSurface.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (designerHost != null &amt;&amt; designerHost.RootComponent != null)
{
IRootDesigner rootDesigner = designerHost.GetDesigner(designerHost.RootComponent) as IRootDesigner;
if (rootDesigner != null)
{
UnloadWorkflow();

this.designSurface = designSurface;
this.loader = loader;
this.workflowView = rootDesigner.GetView(ViewTechnology.Default) as WorkflowView;
panel2.Controls.Add(this.workflowView);
this.workflowView.Dock = DockStyle.Fill;
this.workflowView.TabIndex = 1;
this.workflowView.TabStop = true;
this.workflowView.HScrollBar.TabStop = false;
this.workflowView.VScrollBar.TabStop = false;
this.workflowView.Focus();

ISelectionService selectionService = GetService(typeof(ISelectionService)) as ISelectionService;

if (selectionService != null)
{
selectionService.SelectionChanged += new EventHandler(OnSelectionChanged);
}
}
}

ResumeLayout(true);
}

private void UnloadWorkflow()
{
IDesignerHost designerHost;
designerHost = GetService(typeof(IDesignerHost)) as IDesignerHost;
if (designerHost != null &amt;&amt; designerHost.Container.Components.Count > 0)
wxdWFLoader.DestroyObjectGraphFromDesignerHost(designerHost, designerHost.RootComponent as Activity);

if (this.designSurface != null)
{
this.designSurface.Dispose();
this.designSurface = null;
}

if (this.workflowView != null)
{
Controls.Remove(this.workflowView);
this.workflowView.Dispose();
this.workflowView = null;
}
}

//显示默认的工作流
private void ShowDefaultWorkflow()
{

this.workflow = new StateMachineWorkflowActivity();
workflow.Name = "wxwinter";

this.LoadWorkflow();
}


private void LoadWorkflow()
{
using (StringWriter stringWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
{
WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
serializer.Serialize(xmlWriter, workflow);
this.Xoml = stringWriter.ToString();
}
}
}



private void OnSelectionChanged(object sender, EventArgs e)
{
ISelectionService selectionService = GetService(typeof(ISelectionService)) as ISelectionService;

if (selectionService != null)
{
this.propertyGrid.SelectedObjects = new ArrayList(selectionService.GetSelectedComponents()).ToArray();
}
}

private void SaveFile()
{
if (this.XomlFile.Length != 0)
{
this.SaveExistingWorkflow(this.XomlFile);
}
else
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "xoml files (*.xoml)|*.xoml|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
this.SaveExistingWorkflow(saveFileDialog.FileName);
this.Text = "Designer Hosting Sample -- [" + saveFileDialog.FileName + "]";
}
}
}

internal void SaveExistingWorkflow(string filePath)
{
if (this.designSurface != null &amt;&amt; this.loader != null)
{
this.XomlFile = filePath;
this.loader.PerformFlush();
}
}

public bool Save(bool showMessage)
{
Cursor cursor = this.Cursor;
this.Cursor = Cursors.WaitCursor;

bool saveOK = true;

try
{

this.SaveFile();

if (showMessage)
{
MessageBox.Show(this, "文件已保存 \n" + Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), this.XomlFile), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
saveOK = false;
}
finally
{
this.Cursor = cursor;
}

return saveOK;
}

}

}