Click here to Skip to main content
15,892,005 members
Articles / Desktop Programming / XAML

WF4 Custom activities for message mediation

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
20 Dec 2012CPOL24 min read 108.6K   2K   72  
This article describes a design, implementation, and usage of custom message mediation activities for a XAML workflow.
//*****************************************************************************
//    Description.....Library of the activities 
//                                
//    Author..........Roman Kiss, rkiss@pathcom.com
//    Copyright © 2005 ATZ Consulting Inc. (see included license.rtf file)         
//                        
//    Date Created:    04/04/09
//
//    Date        Modified By     Description
//-----------------------------------------------------------------------------
//    04/04/09    Roman Kiss     Initial Revision
//
//*****************************************************************************
//

#region Namespaces
using System;
using System.Collections.Generic;
using System.ServiceModel.Dispatcher;
using System.Windows.Forms;
#endregion

namespace RKiss.MessageMediationActivityLibrary.Design.Dialogs.UserControls
{
    
    public partial class XpathUserControl : UserControl
    {
        public event StatusEventHandler RaiseEventOnStatus;
        public delegate void StatusEventHandler(object sender, StatusEventArgs args);

        #region Constructor
        public XpathUserControl()
        {
            InitializeComponent();
            this.xmlNotepadPanelControl1.XmlNotepadForm.ExpandAll();

            this.xmlNotepadPanelControl1.XmlNotepadForm.XmlTreeView.SelectionChanged += delegate(object source, EventArgs args)
            {
                this.textBoxXPathGenerator.Text = this.xmlNotepadPanelControl1.XmlNotepadForm.XPathSelectedNode;
            };
        }
        #endregion

        #region Cleanup
        protected override void DestroyHandle()
        {
            this.Cleanup();          
            base.DestroyHandle();
        }
        
        public void Cleanup()
        {
            this.ClearXmlDocument();
            this.xmlNotepadPanelControl1.Close();
        }
        public void ClearXmlDocument()
        {
            this.xmlNotepadPanelControl1.XmlNotepadForm.ClearXmlDocument();
        }
        #endregion

        #region Properties
        public string XPath
        {
            get { return this.textBoxXPathInput.Text.Trim(); }
            set { this.textBoxXPathInput.Text = value; }
        }

        public string XmlDocument
        {
            get { return this.xmlNotepadPanelControl1.XmlNotepadForm.GetCurrentXmlDocument; }
            set { this.xmlNotepadPanelControl1.XmlNotepadForm.LoadXmlDocument(value); }
        }
        public Dictionary<string, string> Namespaces
        {
            get
            {
                Dictionary<string, string> namespaces = new Dictionary<string, string>();
                foreach (DataGridViewRow row in this.dataGridViewNamespaces.Rows)
                {
                    string prefix = (string)row.Cells[0].Value;
                    string ns = (string)row.Cells[1].Value;
                    if (string.IsNullOrEmpty(prefix) || string.IsNullOrEmpty(ns)) continue;
                    namespaces.Add(prefix, ns);
                }
                return namespaces;
            }
            set
            {
                this.dataGridViewNamespaces.Rows.Clear();
                if (value != null)
                {                   
                    foreach (var item in value)
                    {
                        this.dataGridViewNamespaces.Rows.Add(new object[] { item.Key, item.Value });
                    }
                }
            }
        }
        #endregion    

        #region Public
        public void SelectXmlNode()
        {
            this.SelectXmlNode(this.textBoxXPathInput.Text);
        }
        public void SelectXmlNode(string xpath)
        {
            this.FindXmlNode(xpath);
        }
        #endregion

        #region Events
        private void clearAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.dataGridViewNamespaces.Rows.Clear();
        }
        private void textBoxXPathGenerator_DoubleClick(object sender, EventArgs e)
        {
            this.textBoxXPathInput.Text = this.textBoxXPathGenerator.Text;
        }
        private void textBoxXPathInput_TextChanged(object sender, EventArgs e)
        {
            this.SelectXmlNode();
        }
        private void textBoxXPathGenerator_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }
        private void textBoxXPathGenerator_MouseHover(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }
        private void textBoxXPathGenerator_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }
        private void textBoxXPathInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                this.FindXmlNode(this.textBoxXPathInput.SelectionLength > 0 ? this.textBoxXPathInput.SelectedText : this.textBoxXPathInput.Text);
            }
            //else if (e.KeyChar == '/' || e.KeyChar == '[' || e.KeyChar == ']')
            //{
            //    this.textBoxXPathInput.ForeColor = Color.Black;
            //}
            //else
            //{
            //    this.textBoxXPathInput.ForeColor = Color.Blue;
            //}
        }
        private void textBoxXPathInput_DoubleClick(object sender, EventArgs e)
        {
            this.SelectXmlNode();
        }
        #endregion

        #region Helpers
        private void FindXmlNode(string xpath)
        {
            if (string.IsNullOrEmpty(xpath) == false)
            {
                string msg = string.Empty;
                try
                {
                    // custom namespaces
                    XPathMessageContext context = new XPathMessageContext();
                    foreach (DataGridViewRow row in this.dataGridViewNamespaces.Rows)
                    {
                        string prefix = (string)row.Cells[0].Value;
                        string ns = (string)row.Cells[1].Value;
                        if (string.IsNullOrEmpty(prefix) || string.IsNullOrEmpty(ns)) continue;
                        context.AddNamespace(prefix, ns);
                    }

                    if (string.IsNullOrEmpty(this.XmlDocument) == false)
                    {
                        // search document
                        this.xmlNotepadPanelControl1.XmlNotepadForm.XPathSelectNode(xpath, context);
                    }
                    else
                    {
                        // check xpath expression only
                        XPathMessageFilter filter = new XPathMessageFilter(xpath, context);
                    }
                    msg = "OK";
                }
                catch (Exception ex)
                {
                    msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    this.xmlNotepadPanelControl1.XmlNotepadForm.XPathSelectNode(null, null);
                }
                finally
                {
                    if (this.RaiseEventOnStatus != null)
                    {
                        this.RaiseEventOnStatus(this, new StatusEventArgs() { Status = msg });
                    }
                }
            }
        }
        #endregion       
    }

    #region EventArgs
    public class StatusEventArgs : EventArgs
    {
        public string Status { get; set; }
    }
    #endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions