#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Microsoft.VisualStudio.Shell.Interop;
using System.Threading;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using Microsoft.VisualStudio; 
#endregion

namespace Microsoft.Practices.RecipeFramework.Extensions.Editors
{
    /// <summary>
    /// Editor that shows the Vs Add Web Reference dialog
    /// </summary>
    public class AddWebReferenceEditor : UITypeEditor
    {
        /// <summary>
        /// Gets the editor style used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"></see> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
        /// <returns>
        /// A <see cref="T:System.Drawing.Design.UITypeEditorEditStyle"></see> value that indicates the style of editor used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"></see> method. If the <see cref="T:System.Drawing.Design.UITypeEditor"></see> does not support this method, then <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> will return <see cref="F:System.Drawing.Design.UITypeEditorEditStyle.None"></see>.
        /// </returns>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"></see> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        public override object EditValue(ITypeDescriptorContext context,
            IServiceProvider provider, object value)
        {
            object svc = provider.GetService(typeof(IWindowsFormsEditorService));
            if(svc == null)
            {
                return base.EditValue(context, provider, value);
            }

            string serviceURL;
            int num1;

            IVsAddWebReferenceDlg dlg1 = provider.GetService
                (typeof(IVsAddWebReferenceDlg)) as IVsAddWebReferenceDlg;

            if(((dlg1 != null) && ErrorHandler.Succeeded(dlg1.AddWebReferenceDlg
                (out serviceURL, out num1))) && (num1 == 0))
            {
                return serviceURL;
            }
            else
            {
                if(value != null)
                {
                    return value.ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
        }
    }
}
