public JsParserToolWindowManager(
     JsParserService jsParserService,
     IUIThemeProvider uiThemeProvider,
     Func<IJsParserToolWindow> findWindowDelegate)
 {
     _jsParserService = jsParserService;
     _uiThemeProvider = uiThemeProvider;
     _findWindowDelegate = findWindowDelegate;
 }
Example #2
0
        /// <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)
        {
            _applicationObject = (DTE2)application;
            _addInInstance = (EnvDTE.AddIn)addInInst;
            if(connectMode == ext_ConnectMode.ext_cm_UISetup)
            {
                object []contextGUIDS = new object[] { };
                Commands2 commands = (Commands2)_applicationObject.Commands;
                string toolsMenuName;

                try
                {
                    //If you would like to move the command to a different menu, change the word "Tools" to the
                    //  English version of the menu. This code will take the culture, append on the name of the menu
                    //  then add the command to that menu. You can find a list of all the top-level menus in the file
                    //  CommandBar.resx.
                    string resourceName;
                    ResourceManager resourceManager = new ResourceManager("JsParser_AddIn.CommandBar", Assembly.GetExecutingAssembly());
                    CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID);

                    if(cultureInfo.TwoLetterISOLanguageName == "zh")
                    {
                        System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent;
                        resourceName = String.Concat(parentCultureInfo.Name, "Tools");
                    }
                    else
                    {
                        resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools");
                    }
                    toolsMenuName = resourceManager.GetString(resourceName);
                }
                catch
                {
                    //We tried to find a localized version of the word Tools, but one was not found.
                    //  Default to the en-US word, which may work for the current culture.
                    toolsMenuName = "Tools";
                }

                //Place the command on the tools menu.
                //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
                Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

                //Find the Tools command bar on the MenuBar command bar:
                CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
                CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

                //This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
                //  just make sure you also update the QueryStatus/Exec method to include the new command names.
                try
                {
                    //Add a command to the Commands collection:
                    var comShow = commands.AddNamedCommand2(_addInInstance,
                        "Show",
                        "Javascript Parser",
                        "Javascript Parser AddIn Show",
                        true,
                        629,
                        ref contextGUIDS,
                        (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                        (int)vsCommandStyle.vsCommandStylePictAndText,
                        vsCommandControlType.vsCommandControlTypeButton
                    );

                    //Add a command to the Commands collection:
                    var comFind = commands.AddNamedCommand2(_addInInstance,
                        "Find",
                        "Javascript Parser Find",
                        "Javascript Parser AddIn 'Find' Feature",
                        true,
                        0,
                        ref contextGUIDS,
                        (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                        (int)vsCommandStyle.vsCommandStylePictAndText,
                        vsCommandControlType.vsCommandControlTypeButton
                    );

                    //Add a control for the command to the tools menu:
                    if(toolsPopup != null)
                    {
                        if (comShow != null)
                        {
                            comShow.AddControl(toolsPopup.CommandBar, 1);
                        }

                        if (comFind != null)
                        {
                            comFind.Bindings = "Text Editor::SHIFT+ALT+J";
                            comFind.AddControl(toolsPopup.CommandBar, 2);
                        }
                    }
                }
                catch(System.ArgumentException)
                {
                    //If we are here, then the exception is probably because a command with that name
                    //  already exists. If so there is no need to recreate the command and we can
                    //  safely ignore the exception.
                }
            }

            //Subscribe to IDE events
            Events events = _applicationObject.Events;
            _documentEvents = events.get_DocumentEvents(null);
            _windowEvents = events.get_WindowEvents(null);
            _documentEvents.DocumentSaved += documentEvents_DocumentSaved;
            _documentEvents.DocumentOpened += documentEvents_DocumentOpened;
            _windowEvents.WindowActivated += windowEvents_WindowActivated;

            _uiThemeProvider = new DefaultUIThemeProvider();
            _jsParserService = new JsParserService(Settings.Default);
            _jsParserToolWindowManager = new JsParserToolWindowManager(_jsParserService, _uiThemeProvider, () =>
            {
                return EnsureWindowCreated();
            });
        }