PowerCommands implementation (1) - Command registration

Package source code:

using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.VSXtraCommands
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "VSXtraCommands", "DeepDiver", 1)]
  [ProvideMenuResource(1000, 1)]
  [XtraProvideAutoLoad(typeof(UIContext.NoSolution))]
  [ProvideService(typeof(SCommandManagerService), ServiceName = "CommandManagerService")]
  [XtraProvideOptionPageAttribute(typeof(GeneralPage), "VSXtraCommands", "General", 2000, 2001, true)]
  [ProvideProfileAttribute(typeof(GeneralPage), "VSXtraCommands", "General", 2000, 2001, true, DescriptionResourceID = 2002)]
  [XtraProvideOptionPageAttribute(typeof(CommandsPage), "VSXtraCommands", "Commands", 2000, 2003, true)]
  [ProvideProfileAttribute(typeof(CommandsPage), "VSXtraCommands", "Commands", 2000, 2003, true, DescriptionResourceID = 2004)]
  [Guid(GuidList.guidVSXtraCommandsPkgString)]
  public sealed class VSXtraCommandsPackage : PackageBase
  {
    protected override void Initialize()
    {
      RegisterCommands();
    }

    private void RegisterCommands()
    {
      var commandService = this.GetService<SCommandManagerService, ICommandManagerService>();
      if (commandService != null)
      {
        foreach (var handler in MenuCommandHandler.
          GetRegisteredHandlerInstances<VSXtraCommandsPackage>())
        {
          commandService.RegisterCommand(handler);
        }
      }
    }
  }
}
Command Manager Service:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using VSXtra;

namespace DeepDiver.VSXtraCommands
{
  [Promote]
  internal class CommandManagerService : 
    VsxService<VSXtraCommandsPackage, SCommandManagerService>,
    ICommandManagerService
  {
    private readonly IList<MenuCommandHandler> _RegisteredCommands = 
      new List<MenuCommandHandler>();
    
    public void RegisterCommand(MenuCommandHandler command)
    {
      if (_RegisteredCommands.SingleOrDefault(
          cmd => cmd.Guid.Equals(command.Guid) && cmd.Id.Equals(command.Id)) == null)
      {
        _RegisteredCommands.Add(command);
      }
    }

    public void UnRegisterCommand(MenuCommandHandler command)
    {
      if (_RegisteredCommands.SingleOrDefault(
          cmd => cmd.Guid.Equals(command.Guid) && cmd.Id.Equals(command.Id)) != null)
      {
        _RegisteredCommands.Remove(command);
      }
    }

    public IEnumerable<MenuCommandHandler> GetRegisteredCommands()
    {
      return new ReadOnlyCollection<MenuCommandHandler>(_RegisteredCommands);
    }
  }
}
Base class for command handlers:

using System.Linq;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.VSXtraCommands
{
  public abstract class CommandHandlerBase : MenuCommandHandler 
  {
    protected override void OnQueryStatus(OleMenuCommand command)
    {
      var commandsPage = Package.GetDialogPage<CommandsPage>();
      bool canExecute = 
        commandsPage.DisabledCommands.SingleOrDefault(
          cmd => cmd.Guid.Equals(command.CommandID.Guid) &&
              cmd.ID.Equals(command.CommandID.ID)) == null;
      if (canExecute)
      {
        canExecute = CanExecute(command);
      }
      command.Enabled = command.Visible = command.Supported = canExecute;
    }

    protected virtual bool CanExecute(OleMenuCommand command)
    {
      return true;
    }
  }
}
"Clear All Panes" command

using System.ComponentModel;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.VSXtraCommands
{
  public partial class VSXtraCommandGroup
  {
    [CommandId(CmdIDs.ClearAllPanesCommand)]
    [DisplayName("Clear All Panes")]
    public sealed class ClearAllPanes: CommandHandlerBase
    {
      protected override void OnExecute(OleMenuCommand command)
      {
        OutputWindow.OutputWindowPanes.ForEach(pane => pane.Clear());
      }
    }
  }
}
"Close All Documents" command

using System.ComponentModel;
using VSXtra;

namespace DeepDiver.VSXtraCommands
{
  public partial class VSXtraCommandGroup
  {
    [CommandId(CmdIDs.CloseAllDocumentsCommand)]
    [DisplayName("Close All Documents")]
    [ExecuteCommandAction("Window.CloseAllDocuments")]
    public sealed class CloseAllDocuments : CommandHandlerBase
    {
    }
  }
}

Last edited Aug 5, 2008 at 7:05 AM by INovak, version 3

Comments

No comments yet.