/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

  1. using System;
  2. using System.ComponentModel.Design;
  3. using Microsoft.VisualStudio.Shell;
  4. using Microsoft.VisualStudio.Shell.Interop;
  5. // ReSharper disable once CheckNamespace
  6. namespace ErikEJ.SqlCeToolbox.ToolWindows
  7. {
  8. /// <summary>
  9. /// Command handler
  10. /// </summary>
  11. internal sealed class OtherWindowsCommand
  12. {
  13. /// <summary>
  14. /// Command ID.
  15. /// </summary>
  16. public const int CommandId = 0x101;
  17. /// <summary>
  18. /// Command menu group (command set GUID).
  19. /// </summary>
  20. public static readonly Guid CommandSet = new Guid("97755b4a-7dce-4a6f-b60e-b9cffd7ffd8b");
  21. /// <summary>
  22. /// VS Package that provides this command, not null.
  23. /// </summary>
  24. private readonly Package _package;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="OtherWindowsCommand"/> class.
  27. /// Adds our command handlers for menu (commands must exist in the command table file)
  28. /// </summary>
  29. /// <param name="package">Owner package, not null.</param>
  30. private OtherWindowsCommand(Package package)
  31. {
  32. if (package == null)
  33. {
  34. throw new ArgumentNullException(nameof(package));
  35. }
  36. _package = package;
  37. OleMenuCommandService commandService = ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
  38. if (commandService != null)
  39. {
  40. var menuCommandId = new CommandID(CommandSet, CommandId);
  41. var menuItem = new MenuCommand(ShowToolWindow, menuCommandId);
  42. commandService.AddCommand(menuItem);
  43. }
  44. }
  45. /// <summary>
  46. /// Gets the instance of the command.
  47. /// </summary>
  48. public static OtherWindowsCommand Instance
  49. {
  50. get;
  51. private set;
  52. }
  53. /// <summary>
  54. /// Gets the service provider from the owner package.
  55. /// </summary>
  56. private IServiceProvider ServiceProvider
  57. {
  58. get
  59. {
  60. return _package;
  61. }
  62. }
  63. /// <summary>
  64. /// Initializes the singleton instance of the command.
  65. /// </summary>
  66. /// <param name="package">Owner package, not null.</param>
  67. public static void Initialize(Package package)
  68. {
  69. Instance = new OtherWindowsCommand(package);
  70. }
  71. /// <summary>
  72. /// Shows the tool window when the menu item is clicked.
  73. /// </summary>
  74. /// <param name="sender">The event sender.</param>
  75. /// <param name="e">The event args.</param>
  76. private void ShowToolWindow(object sender, EventArgs e)
  77. {
  78. // Get the instance number 0 of this tool window. This window is single instance so this instance
  79. // is actually the only one.
  80. // The last flag is set to true so that if the tool window does not exists it will be created.
  81. ToolWindowPane window = _package.FindToolWindow(typeof(ExplorerToolWindow), 0, true);
  82. if ((null == window) || (null == window.Frame))
  83. {
  84. throw new NotSupportedException("Cannot create tool window");
  85. }
  86. IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
  87. Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
  88. }
  89. }
  90. }