PowerCommands implementation (2) - Clear Recent Lists commands

Command handler classes' source code:

using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.VisualStudio.Shell;
using Microsoft.Win32;
using VSXtra;

namespace DeepDiver.VSXtraCommands
{
  public partial class VSXtraCommandGroup
  {
    public abstract class ClearRecentListBase : CommandHandlerBase
    {
      protected override bool CanExecute(OleMenuCommand command)
      {
        return VsRegistry.GetFileEntryList(RecentListKey).Count > 0;
      }

      protected override void OnExecute(OleMenuCommand command)
      {
        var view = new ClearListView();
        VsRegistry.GetFileEntryList(RecentListKey).
          ForEach(item => view.Model.ListEntries.Add(item));
        if ((bool)view.ShowDialog())
        {
          VsIde.File.SaveAll();
          DeleteRecentFileList(view.Model.SelectedListEntries);
          VsIde.RestartVS();
        }
      }

      protected abstract RegistryKey RecentListKey { get; }

      private void DeleteRecentFileList(List<FileEntry> entriesToDelete)
      {
        using (var key = RecentListKey)
        {
          entriesToDelete.ForEach(entry => key.DeleteValue(entry.Key));
          var valueNames = key.GetValueNames();
          if (valueNames.Length <= 0) return;

          var fileCounter = 1;
          
          valueNames.ForEach(
            valueName =>
              {
                key.SetValue(string.Concat(valueName, "_"), key.GetValue(valueName));
                key.DeleteValue(valueName);
              });

          key.GetValueNames().ForEach(
            valueName =>
              {
                key.SetValue(string.Format("File{0}", fileCounter++), key.GetValue(valueName));
                key.DeleteValue(valueName);
              });
        }
      }
    }

    [CommandId(CmdIDs.ClearRecentFileListCommand)]
    [DisplayName("Clear Recent Files")]
    public sealed class ClearRecentFileList : ClearRecentListBase
    {
      protected override RegistryKey RecentListKey
      {
        get { return VsRegistry.GetRecentFilesListKey(true); }
      }
    }

    [CommandId(CmdIDs.ClearRecentProjectListCommand)]
    [DisplayName("Clear Recent Projects")]
    public sealed class ClearRecentProjectList : ClearRecentListBase
    {
      protected override RegistryKey RecentListKey
      {
        get { return VsRegistry.GetRecentProjectsListKey(true); }
      }
    }
  }
}

Last edited Aug 9, 2008 at 8:10 AM by INovak, version 1

Comments

No comments yet.