context: using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.Globalization;
  using System.IO;
  using System.Linq;
  using System.Runtime.Versioning;
  using System.Windows;
  using EnvDTE;
  using Microsoft.VisualStudio.ExtensionsExplorer;
  using Microsoft.VisualStudio.ExtensionsExplorer.UI;
  using NuGet.VisualStudio;
  using NuGetConsole;
  
  namespace NuGet.Dialog.Providers
  {
      /// <summary>
      /// Base class for all tree node types.
      /// </summary>
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification="This is due to the native of this class.")]
      internal abstract class PackagesProviderBase : VsExtensionsProvider, ILogger, IDisposable
      {
          private PackagesSearchNode _searchNode;
          private PackagesTreeNodeBase _lastSelectedNode;
          private readonly ResourceDictionary _resources;
          private readonly Lazy<IConsole> _outputConsole;
          private readonly IPackageRepository _localRepository;
          private readonly ProviderServices _providerServices;
          private Dictionary<Project, Exception> _failedProjects;
          private string _readmeFile, _originalPackageId;
  
          private object _mediumIconDataTemplate;
          private object _detailViewDataTemplate;
          private IList<IVsSortDescriptor> _sortDescriptors;
          private readonly IProgressProvider _progressProvider;
          private CultureInfo _uiCulture, _culture;
          private readonly ISolutionManager _solutionManager;
          private IDisposable _expandedNodesDisposable;
          private bool _overwriteAll, _ignoreAll;
  
          protected PackagesProviderBase(
              IPackageRepository localRepository, 
              ResourceDictionary resources, 
              ProviderServices providerServices, 
              IProgressProvider progressProvider, 
              ISolutionManager solutionManager)
          {
              if (resources == null)
              {
                  throw new ArgumentNullException("resources");
              }
  
              if (providerServices == null)
              {
                  throw new ArgumentNullException("providerServices");
              }
  
              if (solutionManager == null)
              {
                  throw new ArgumentNullException("solutionManager");
              }
  
              _localRepository = localRepository;
              _providerServices = providerServices;
              _progressProvider = progressProvider;
              _solutionManager = solutionManager;
              _resources = resources;
              _outputConsole = new Lazy<IConsole>(() => providerServices.OutputConsoleProvider.CreateOutputConsole(requirePowerShellHost: false));
          }
  
          /// <summary>
          /// Copy constructor for PackagesProviderBase
          /// </summary>
          protected PackagesProviderBase(PackagesProviderBase other) :
              this(other._localRepository, other._resources, other._providerServices, other._progressProvider, other._solutionManager)
          {
          }
  
          /// <summary>
          /// Returns either the solution repository or the active project repository, depending on whether we are targeting solution.
          /// </summary>
          protected IPackageRepository LocalRepository
          {
              get
              {
                  return _localRepository;
              }
          }
  
          public virtual bool RefreshOnNodeSelection
          {
              get
              {
                  return false;
              }
          }
  
          public PackagesTreeNodeBase SelectedNode
          {
              get;
              set;
          }
  
          public bool SuppressNextRefresh { get; private set; }
  
          internal bool SuppressLoad { get; set; }
  
          /// <summary>
          /// Gets the root node of the tree
          /// </summary>
          protected IVsExtensionsTreeNode RootNode
          {
              get;
              set;
          }
  
          public PackageSortDescriptor CurrentSort
          {
              get;
              set;
          }
  
          public virtual bool ShowPrereleaseComboBox
          {
              get
              {
                  return true;
              }
          }
  
          internal virtual bool IncludePrerelease
          {
              get;
              set;
          }
  
          public virtual IEnumerable<string> SupportedFrameworks
          {
              get
              {
                  yield break;
              }
          }
  
          public virtual bool SupportsExecuteAllCommand
          {
              get
              {
                  return false;
              }
          }
  
          public override IVsExtensionsTreeNode ExtensionsTree
          {
              get
              {
                  if (RootNode == null)
                  {
                      RootNode = new RootPackagesTreeNode(null, String.Empty);
                      CreateExtensionsTree();
                  }
  
                  return RootNode;
              }
          }
  
          public override object MediumIconDataTemplate
          {
              get
              {
                  if (_mediumIconDataTemplate == null)
                  {
                      _mediumIconDataTemplate = _resources["PackageItemTemplate"];
                  }
                  return _mediumIconDataTemplate;
              }
          }
  
          public override object DetailViewDataTemplate
          {
              get
              {
                  if (_detailViewDataTemplate == null)
                  {
                      _detailViewDataTemplate = _resources["PackageDetailTemplate"];
                  }
                  return _detailViewDataTemplate;
              }
          }
  
          // hook for unit test
          internal Action ExecuteCompletedCallback
          {
              get;
              set;
          }
  
          public IList<IVsSortDescriptor> SortDescriptors
          {
              get
              {
                  if (_sortDescriptors == null)
                  {
                      _sortDescriptors = CreateSortDescriptors();
                  }
                  return _sortDescriptors;
              }
          }
  
          protected virtual IList<IVsSortDescriptor> CreateSortDescriptors()
          {
              return new[] {
                  new PackageSortDescriptor(Resources.Dialog_SortOption_MostDownloads, "DownloadCount", ListSortDirection.Descending),
                  new PackageSortDescriptor(Resources.Dialog_SortOption_PublishedDate, "Published", ListSortDirection.Descending),
                  new PackageSortDescriptor(String.Format(CultureInfo.CurrentCulture, "{0}: {1}", Resources.Dialog_SortOption_Name, Resources.Dialog_SortAscending), new[] { "Title", "Id" }, ListSortDirection.Ascending),
                  new PackageSortDescriptor(String.Format(CultureInfo.CurrentCulture, "{0}: {1}", Resources.Dialog_SortOption_Name, Resources.Dialog_SortDescending), new[] { "Title", "Id" }, ListSortDirection.Descending)
              };
          }
  
          public override string ToString()
          {
              return Name;
          }
  
          public override IVsExtensionsTreeNode Search(string searchText)
          {
              if (OperationCoordinator.IsBusy)
              {
                  return null;
              }
  
              if (!String.IsNullOrWhiteSpace(searchText) && SelectedNode != null)
              {
                  searchText = searchText.Trim();
  
gt:                 if (_searchNode != null)