public ServiceFacade(VisualStudio visualStudio)
        {
            vsInstance = visualStudio;
            selectedItem = vsInstance.SelectedItem;
            currentProject = vsInstance.SelectedProject;
			outputWindowWriter = new OutputWindowWriter(visualStudio.ApplicationObject);
        }
    	public MenuBuilder(VisualStudio visualStudio)
    	{
    		this.visualStudio = visualStudio;

    		try
    		{
    			CommandBars commandBars = (CommandBars)visualStudio.ApplicationObject.CommandBars;
    			CommandBar itemCommandBar = commandBars["Item"];
    			CommandBar webItemCommandBar = commandBars["Web Item"];
				CommandBar projectCommandBar = commandBars["Project"];
				CommandBar webProjectCommandBar = commandBars["Web Project Folder"];
				CommandBar toolsCommandBar = commandBars["Tools"];

    			subMenuPopup = (CommandBarPopup)itemCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
    			subMenuPopup.Caption = "WSCF.blue";
    			subMenuCommandBar = subMenuPopup.CommandBar;

    			webSubMenuPopup = (CommandBarPopup)webItemCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
    			webSubMenuPopup.Caption = "WSCF.blue";
    			webSubMenuCommandBar = webSubMenuPopup.CommandBar;

				projectSubMenuPopup = (CommandBarPopup)projectCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
				projectSubMenuPopup.Caption = "WSCF.blue";
				projectSubMenuCommandBar = projectSubMenuPopup.CommandBar;

				webProjectSubMenuPopup = (CommandBarPopup)webProjectCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
				webProjectSubMenuPopup.Caption = "WSCF.blue";
				webProjectSubMenuCommandBar = webProjectSubMenuPopup.CommandBar;

				toolsSubMenuPopup = (CommandBarPopup)toolsCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
				toolsSubMenuPopup.Caption = "WSCF.blue";
				toolsSubMenuCommandBar = toolsSubMenuPopup.CommandBar;

    			selectionEvents = visualStudio.ApplicationObject.Events.SelectionEvents;
    			selectionEvents.OnChange += OnSelectionChanged;
    		}
    		catch (Exception ex)
    		{
    			AppLog.LogMessage(ex.ToString());
    		}			
    	}
        /// <summary>
        ///      Implements the OnConnection method of the IDTExtensibility2 interface.
        ///      Receives notification that the Add-in is being loaded.
        /// </summary>
        /// <param term='application'>
        ///      Root object of the host application.
        /// </param>
        /// <param term='connectMode'>
        ///      Describes how the Add-in is being loaded.
        /// </param>
        /// <param term='addInInst'>
        ///      Object representing this Add-in.
        /// </param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            AppLog.LogMessage("Entering OnConnection method.");

            this.applicationObject = (DTE2)application;
            // Initialize VsHelper.
            this.visualStudio = new VisualStudio(this.applicationObject);

            this.addInInstance = (AddIn)addInInst;
            object[] contextGUIDS = new object[] { };
            //CommandBar cmdBar;
            //Command cmdObj;

            if (connectMode == ext_ConnectMode.ext_cm_Startup ||
                connectMode == ext_ConnectMode.ext_cm_AfterStartup ||
                connectMode == ext_ConnectMode.ext_cm_UISetup)
            {
                AppLog.LogMessage("Creating commands.");

                Commands2 commands = (Commands2)applicationObject.Commands;
                CommandBars commandBars = (CommandBars)applicationObject.CommandBars;

                try
                {
					menuBuilder = new MenuBuilder(visualStudio);

                    // Add command bar to tools menu
					contextGUIDS = menuBuilder.AddCommandBarToToolsMenu(addInInstance, applicationObject, commands, contextGUIDS, commandBars);
                    // Create the 'Generate Web Service Code...' context-menu entry
					contextGUIDS = menuBuilder.GenerateMenuItemForWSCodeGen(addInInstance, applicationObject, commands, contextGUIDS);
                    // Create the 'Edit WSDL Interface Description...' context-menu entry
					contextGUIDS = menuBuilder.GenerateMenuItemForEditWsdl(addInInstance, applicationObject, commands, contextGUIDS);
                    // Create the 'Create WSDL Interface Description...' context-menu entry
					contextGUIDS = menuBuilder.GenerateMenuItemForCreateWsdl(addInInstance, applicationObject, commands, contextGUIDS);
                    // Create the 'Choose WSDL to implement...' context-menu entry
					contextGUIDS = menuBuilder.GenerateMenuItemForChooseWsdlToImplement(addInInstance, applicationObject, commands, contextGUIDS);
                    // Create the 'Generate code...' context-menu entry
                    //GenerateMenuItemForDCCodeGen(commands, contextGUIDS);
					contextGUIDS = menuBuilder.GenerateMenuItemForDCCodeGen(addInInstance, applicationObject, commands, contextGUIDS);
					// Create the 'Paste XML as Schema' Edit menu entry.
					menuBuilder.GenerateMenuItemForPasteXmlAsSchema(addInInstance, applicationObject, commands, commandBars, contextGUIDS);
                }
                catch (ArgumentException e)
                {
                    AppLog.LogMessage(e.Message);
                }
                catch (Exception e)
                {
                    AppLog.LogMessage(e.Message);
                }


            }

            AppLog.LogMessage("Leaving OnConnection method.");
        }
        /// <summary>
        ///      Implements the OnConnection method of the IDTExtensibility2 interface.
        ///      Receives notification that the Add-in is being loaded.
        /// </summary>
        /// <param term='application'>
        ///      Root object of the host application.
        /// </param>
        /// <param term='connectMode'>
        ///      Describes how the Add-in is being loaded.
        /// </param>
        /// <param term='addInInst'>
        ///      Object representing this Add-in.
        /// </param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            AppLog.LogMessage("Entering OnConnection method.");

            applicationObject = (DTE2)application;
            // Initialize VsHelper.
            visualStudio = new VisualStudio(applicationObject);

        	outputWindowWriter = new OutputWindowWriter(applicationObject);

        	addInInstance = (AddIn)addInInst;
            object[] contextGUIDS = new object[] { };
            CommandBar cmdBar;
            Command cmdObj;

            if (connectMode == ext_ConnectMode.ext_cm_Startup ||
                connectMode == ext_ConnectMode.ext_cm_AfterStartup ||
                connectMode == ext_ConnectMode.ext_cm_UISetup)
            {
                AppLog.LogMessage("Creating commands.");

                Commands2 commands = (Commands2)applicationObject.Commands;
                CommandBars commandBars = (CommandBars)applicationObject.CommandBars;

            	CommandBar subMenuCommandBar = null;
            	CommandBar webSubMenuCommandBar = null;
            	CommandBar projectSubMenuCommandBar = null;
				CommandBar webProjectSubMenuCommandBar = null;
            	CommandBar toolsSubMenuCommandBar = null;

            	try
            	{
					CommandBar itemCommandBar = commandBars["Item"];
					CommandBar webItemCommandBar = commandBars["Web Item"];
					CommandBar projectCommandBar = commandBars["Project"];
					CommandBar webProjectCommandBar = commandBars["Web Project Folder"];
					CommandBar toolsCommandBar = commandBars["Tools"];

            		subMenuPopup = (CommandBarPopup)itemCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
            		subMenuPopup.Caption = "WSCF.blue";
					subMenuCommandBar = subMenuPopup.CommandBar;

            		webSubMenuPopup = (CommandBarPopup)webItemCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
            		webSubMenuPopup.Caption = "WSCF.blue";
            		webSubMenuCommandBar = webSubMenuPopup.CommandBar;

					projectSubMenuPopup = (CommandBarPopup)projectCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
					projectSubMenuPopup.Caption = "WSCF.blue";
					projectSubMenuCommandBar = projectSubMenuPopup.CommandBar;

					webProjectSubMenuPopup = (CommandBarPopup)webProjectCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
					webProjectSubMenuPopup.Caption = "WSCF.blue";
					webProjectSubMenuCommandBar = webProjectSubMenuPopup.CommandBar;

					toolsSubMenuPopup = (CommandBarPopup)toolsCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
					toolsSubMenuPopup.Caption = "WSCF.blue";
					toolsSubMenuCommandBar = toolsSubMenuPopup.CommandBar;

                    selectionEvents = applicationObject.Events.SelectionEvents;
					selectionEvents.OnChange += OnSelectionChanged;
            	}
            	catch (Exception ex)
            	{
            		AppLog.LogMessage(ex.ToString());
            	}

				// Create the 'Web Services Contract-First...' Tools entry
				try
				{
					Command command = commands.AddNamedCommand2(
						addInInstance,
						"WsContractFirst",
						"Web Services Contract-First...",
						"Executes the command for WsContractFirstAddin",
						true,
						190,
						ref contextGUIDS,
						(int)vsCommandStatus.vsCommandStatusUnsupported + (int)vsCommandStatus.vsCommandStatusEnabled,
						(int)vsCommandStyle.vsCommandStylePictAndText,
						vsCommandControlType.vsCommandControlTypeButton);

					command.AddControl(toolsSubMenuCommandBar, 1);

					AppLog.LogMessage("Command bar is added to the Tools menu.");
				}
				catch (ArgumentException)
				{
				}
				catch (Exception e)
				{
					AppLog.LogMessage(e.Message);
				}

            	// Create the 'Generate Web Service Code...' context-menu entry
                try
                {
                    // Create the add-in command
                    cmdObj = commands.AddNamedCommand2(
                        addInInstance,
                        "WsContractFirstContextMenu",
                        "Generate Web Service Code...",
                        "Executes the command for WsContractFirstAddin ContextMenu",
                        true,
                        190,
                        ref contextGUIDS,
                        (int)vsCommandStatus.vsCommandStatusUnsupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                        (int)vsCommandStyle.vsCommandStylePictAndText,
                        vsCommandControlType.vsCommandControlTypeButton);

					cmdObj.AddControl(subMenuCommandBar, 1);

                    // BDS 11/21/2005: Add this menu item to the web project 
                    // template.
					cmdObj.AddControl(webSubMenuCommandBar, 1);

                    AppLog.LogMessage("Generate Web Serive Code menu item is added.");
                }
                catch (ArgumentException e)
                {
                    AppLog.LogMessage(e.Message);
                }
                catch (Exception ex)
                {
                    AppLog.LogMessage(ex.Message);
                }

                // Create the 'Edit WSDL Interface Description...' context-menu entry
                try
                {
                    // Create the add-in command
                    cmdObj = commands.AddNamedCommand2(
                        addInInstance,
                        "EditWsdlContextMenu",
                        "Edit WSDL Interface Description...",
                        "Executes the command for WsContractFirstAddin ContextMenu",
                        true,
                        190,
                        ref contextGUIDS,
                        (int)vsCommandStatus.vsCommandStatusUnsupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                        (int)vsCommandStyle.vsCommandStylePictAndText,
                        vsCommandControlType.vsCommandControlTypeButton);

					cmdObj.AddControl(subMenuCommandBar, 2);

                    // BDS 11/21/2005: Add this menu item to the web project 
                    // template.
					cmdObj.AddControl(webSubMenuCommandBar, 2);

                    AppLog.LogMessage("Edit WSDL menu item is added");
                }
                catch (ArgumentException e)
                {
                    AppLog.LogMessage(e.Message);
                }
                catch (Exception ex)
                {
                    AppLog.LogMessage(ex.Message);
                }

                // Create the 'Create WSDL Interface Description...' context-menu entry
                try
                {
                    // Create the add-in command
                    cmdObj = commands.AddNamedCommand2(
                        addInInstance,
                        "CreateWsdlContextMenu",
                        "Create WSDL Interface Description...",
                        "Executes the command for WsContractFirstAddin ContextMenu",
                        true,
                        190,
                        ref contextGUIDS,
                        (int)vsCommandStatus.vsCommandStatusUnsupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                        (int)vsCommandStyle.vsCommandStylePictAndText,
                        vsCommandControlType.vsCommandControlTypeButton);

					cmdObj.AddControl(subMenuCommandBar, 1);

                    // BDS 11/21/2005: Add this menu item to the web project 
                    // template.
					cmdObj.AddControl(webSubMenuCommandBar, 1);

                    AppLog.LogMessage("Create WSDL interface desc menu item is added");
                }
                catch (ArgumentException e)
                {
                    AppLog.LogMessage(e.Message);
                }
                catch (Exception ex)
                {
                    AppLog.LogMessage(ex.Message);
                }


                // Create the 'Choose WSDL to implement...' context-menu entry
                try
                {
                    // Create the add-in command
                    cmdObj = commands.AddNamedCommand2(
                        addInInstance,
                        "WsContractFirstContextMenu2",
                        "Choose WSDL to Implement...",
                        "Executes the command for WsContractFirstAddin ContextMenu",
                        true,
                        190,
                        ref contextGUIDS,
                        (int)vsCommandStatus.vsCommandStatusUnsupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                        (int)vsCommandStyle.vsCommandStylePictAndText,
                        vsCommandControlType.vsCommandControlTypeButton);

					cmdObj.AddControl(projectSubMenuCommandBar, 1);

                    // BDS 11/21/2005: Add this menu item to the web project 
                    // template. 
					cmdObj.AddControl(webProjectSubMenuCommandBar, 1);

                    AppLog.LogMessage("Choose WSDL menu item is added");
                }
                catch (ArgumentException e)
                {
                    AppLog.LogMessage(e.Message);
                }
                catch (Exception ex)
                {
                    AppLog.LogMessage(ex.Message);
                }

				// Create the 'Generate code...' context-menu entry
				try
				{
					// Create the add-in command
					cmdObj = commands.AddNamedCommand2(
						addInInstance,
						"GenerateCodeMenu",
						"Generate Data Contract Code...",
						"Executes the command for WsContractFirstAddin ContextMenu",
						true,
						190,
						ref contextGUIDS,
						(int)vsCommandStatus.vsCommandStatusUnsupported + (int)vsCommandStatus.vsCommandStatusEnabled,
						(int)vsCommandStyle.vsCommandStylePictAndText,
						vsCommandControlType.vsCommandControlTypeButton);

					cmdObj.AddControl(subMenuCommandBar, 2);

					// BDS 11/21/2005: Add this menu item to the web project 
					// template. 
					cmdObj.AddControl(webSubMenuCommandBar, 2);

					AppLog.LogMessage("Generate code menu item is added");
				}
				catch (ArgumentException e)
				{
					AppLog.LogMessage(e.Message);
				}

				// Create the 'Paste XML as Schema' Edit menu entry.
				try
				{
					// Create the add-in command
					cmdObj = commands.AddNamedCommand2(
						addInInstance,
						"PasteSchemaMenu",
						"Paste XML as Schema",
						"Pastes the XML on the clipboard as XSD schema.",
						true,
						239,
						ref contextGUIDS,
						(int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
						(int)vsCommandStyle.vsCommandStylePictAndText,
						vsCommandControlType.vsCommandControlTypeButton);

					CommandBar menuBarCommandBar = commandBars["MenuBar"];

					CommandBarControl editControl = menuBarCommandBar.Controls["Edit"];
					CommandBarPopup editPopup = (CommandBarPopup)editControl;
					CommandBarControl pasteControl = editPopup.CommandBar.Controls["Paste"];

					cmdObj.AddControl(editPopup.CommandBar, pasteControl != null ? pasteControl.Index + 1 : 1);

					AppLog.LogMessage("Paste Schema code menu item is added");
				}
				catch (Exception ex)
				{
					AppLog.LogMessage(ex.Message);
				}
            }

            AppLog.LogMessage("Leaving OnConnection method.");
        }