MessageEditor.cs 3.92 KB
//*************************************************************************************************
// MessageEditor.cs
// Owner: Mahipal Kante
//
// UI of Editor for Web Service Request Body
// The Web Service response can be plain XML or 'application/soap+msbin1'
// (binary - used by Silverlight application with WCF RIA) or JSON
//
// Copyright(c) Microsoft Corporation, 2010
//*************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using WebTest.WebService.Plugin.ResultTab;
using WebTest.WebService.Plugin.Runtime;

namespace WebTest.WebService.Plugin.MessageEditors
{
    internal partial class MessageEditor : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="contentHandler">Handler for given content type</param>
        public MessageEditor(ContentHandler contentHandler)
        {
            InitializeComponent();

            this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.TabChanged);
            this.xmlTreeView = new XmlTreeView(this.treeViewRequest);

            this.contentHandler = contentHandler;
            this.xmlTreeView.ShowXML(contentHandler.XmlDocument);
            this.requestTextBox.Text = contentHandler.XmlString;

            ContextMenu treeContextMenu = new ContextMenu();
            this.treeParameterizeMenuItem = new MenuItem("Parameterize");
            treeContextMenu.MenuItems.Add(treeParameterizeMenuItem);
            this.treeViewRequest.ContextMenu = treeContextMenu;


            this.treeParameterizeMenuItem.MenuItems.Clear();
            this.treeParameterizeMenuItem.MenuItems.Add(new MenuItem("Type Parameter", AddParameter));

            Connect connect = Connect.GetInstance();
            if (connect != null)
            {
                this.webTestDetail = connect.GetActiveWebTest();
            }
            requestXMLEditor.Visible = (this.webTestDetail != null);
            if (this.webTestDetail != null)
            {
                List<string> parameters = this.webTestDetail.GetParameterList();

                foreach (string parameter in parameters)
                {
                    this.treeParameterizeMenuItem.MenuItems.Add(new MenuItem(parameter, AddParameter));
                }
            }
        }

        /// <summary>
        /// Makes sure both tabs are synced with the current changes that are done in Text Box
        /// </summary>
        public void UpdateContent()
        {
            if (this.tabControl1.SelectedTab.Equals(this.tabPage2))
            {
                this.contentHandler.XmlString = this.requestTextBox.Text;
            }
        }

        private void AddParameter(object sender, EventArgs e)
        {
            MenuItem menuItem = sender as MenuItem;
            if (menuItem == null)
            {
                return;
            }
            this.xmlTreeView.AddParameter(menuItem.Text);
        }

        private void TabChanged(object sender, EventArgs e)
        {
            if (this.tabControl1.SelectedTab.Equals(this.tabPage1))
            {
                this.contentHandler.XmlString = this.requestTextBox.Text;
                xmlTreeView.ShowXML(this.contentHandler.XmlDocument);
            }
            else
            {
                this.requestTextBox.Text = this.contentHandler.XmlString;
            }
        }

        private void requestXMLEditor_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Connect.GetInstance().ShowInXMLEditor("Request Content", this.requestTextBox.Text);
        }

        private ContentHandler contentHandler;

        private WebTestDetail webTestDetail;
        private XmlTreeView xmlTreeView;
        private MenuItem treeParameterizeMenuItem;
    }
}