/src/GUI/SSMSToolbox/Package/OtherWindowsCommand.cs
https://github.com/ErikEJ/SqlCeToolbox · C# · 101 lines · 54 code · 11 blank · 36 comment · 8 complexity · 335da7560184cd5d612f65b1755e6654 MD5 · raw file
- using System;
- using System.ComponentModel.Design;
- using Microsoft.VisualStudio.Shell;
- using Microsoft.VisualStudio.Shell.Interop;
- // ReSharper disable once CheckNamespace
- namespace ErikEJ.SqlCeToolbox.ToolWindows
- {
- /// <summary>
- /// Command handler
- /// </summary>
- internal sealed class OtherWindowsCommand
- {
- /// <summary>
- /// Command ID.
- /// </summary>
- public const int CommandId = 0x101;
- /// <summary>
- /// Command menu group (command set GUID).
- /// </summary>
- public static readonly Guid CommandSet = new Guid("97755b4a-7dce-4a6f-b60e-b9cffd7ffd8b");
- /// <summary>
- /// VS Package that provides this command, not null.
- /// </summary>
- private readonly Package _package;
- /// <summary>
- /// Initializes a new instance of the <see cref="OtherWindowsCommand"/> class.
- /// Adds our command handlers for menu (commands must exist in the command table file)
- /// </summary>
- /// <param name="package">Owner package, not null.</param>
- private OtherWindowsCommand(Package package)
- {
- if (package == null)
- {
- throw new ArgumentNullException(nameof(package));
- }
- _package = package;
- OleMenuCommandService commandService = ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
- if (commandService != null)
- {
- var menuCommandId = new CommandID(CommandSet, CommandId);
- var menuItem = new MenuCommand(ShowToolWindow, menuCommandId);
- commandService.AddCommand(menuItem);
- }
- }
- /// <summary>
- /// Gets the instance of the command.
- /// </summary>
- public static OtherWindowsCommand Instance
- {
- get;
- private set;
- }
- /// <summary>
- /// Gets the service provider from the owner package.
- /// </summary>
- private IServiceProvider ServiceProvider
- {
- get
- {
- return _package;
- }
- }
- /// <summary>
- /// Initializes the singleton instance of the command.
- /// </summary>
- /// <param name="package">Owner package, not null.</param>
- public static void Initialize(Package package)
- {
- Instance = new OtherWindowsCommand(package);
- }
- /// <summary>
- /// Shows the tool window when the menu item is clicked.
- /// </summary>
- /// <param name="sender">The event sender.</param>
- /// <param name="e">The event args.</param>
- private void ShowToolWindow(object sender, EventArgs e)
- {
- // Get the instance number 0 of this tool window. This window is single instance so this instance
- // is actually the only one.
- // The last flag is set to true so that if the tool window does not exists it will be created.
- ToolWindowPane window = _package.FindToolWindow(typeof(ExplorerToolWindow), 0, true);
- if ((null == window) || (null == window.Frame))
- {
- throw new NotSupportedException("Cannot create tool window");
- }
- IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
- Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
- }
- }
- }