Basic Hierarchy Tool Window Sample

This small package demonstrates how to create a basic hierarchy tool window using a custom hierarchy with VSXtra.

Points of Interest:

  • Hierarchy node definition is simple, inheritance is supported.
  • Hierarchy window style is declared with decorating attributes.
Package source code:

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

namespace DeepDiver.BasicHierarchy
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "Basic Hierarchy", "DeepDiver", 1)]
  [ProvideMenuResource(1000, 1)]
  [XtraProvideToolWindow(typeof(BasicUIHierarchyToolWindow))]
  [Guid(GuidList.guidBasicHierarchyPkgString)]
  public sealed class BasicHierarchyPackage : PackageBase
  {
    [CommandExecMethod]
    [CommandId(GuidList.guidBasicHierarchyCmdSetString, CmdIDs.cmdidShowBasicHierachyWIndow)]
    [ShowToolWindowAction(typeof(BasicUIHierarchyToolWindow))]
    private static void ShowBasicUIHierarchyWindow()
    {
    }
  }
}
Hierarchy tool window pane source code:

using VSXtra.Hierarchy;
using VSXtra.Windows;

namespace DeepDiver.BasicHierarchy
{
  [InitialCaption("Basic UI Hierarchy window")]
  [BitmapResourceId(301)]
  [LinesAtRoot]
  [RouteCmdidDelete]
  [DoNotSortRootNodes]
  public class BasicUIHierarchyToolWindow: UIHierarchyToolWindow<BasicHierarchyPackage>
  {
    protected override HierarchyManager<BasicHierarchyPackage> HierarchyManager
    {
      get { return new FileHierarchyManager("C:\\"); }
    }
  }
}
Hierarchy manager class source code:

using System;
using System.IO;
using VSXtra.Hierarchy;

namespace DeepDiver.BasicHierarchy
{
  public class FileHierarchyManager : HierarchyManager<BasicHierarchyPackage>
  {
    private readonly string _FullPath;
    public FileHierarchyManager(string fullPath)
    {
      _FullPath = fullPath;
    }
    protected override HierarchyNode CreateHierarchyRoot()
    {
      return new RootNode(null, _FullPath, _FullPath);
    }
    protected override void InitializeHierarchyRoot(HierarchyNode root)
    {
      var fileRoot = root as FileHierarchyNode;
      if (fileRoot == null) return;
      ScanContent(fileRoot);
    }
    public void ScanContent(FileHierarchyNode node)
    {
      if (node == null) throw new ArgumentNullException("node");
      try
      {
        var path = node.FullPath;
        foreach (var dir in Directory.GetDirectories(node.FullPath))
        {
          var dirName = dir.Substring(path.Length + (path.EndsWith("\\") ? 0 : 1));
          node.AddChild(new FolderNode(this, dir, dirName));
        }
        foreach (var file in Directory.GetFiles(path))
        {
          var fileNode = new FileNode(this, file, Path.GetFileName(file));
          node.AddChild(fileNode);
        }
      }
      catch (SystemException)
      {
      }
    }
  }
}
Abstract file hierarchy node source code:

using VSXtra.Hierarchy;

namespace DeepDiver.BasicHierarchy
{
  public abstract class FileHierarchyNode : HierarchyNode
  {
    protected FileHierarchyNode(FileHierarchyManager manager, string caption)
      : base(manager, caption)
    {
    }
    public FileHierarchyNode(FileHierarchyManager manager, string fullPath, string caption) : 
      base(manager, caption)
    {
      FullPath = fullPath;
    }
    public string FullPath { get; private set; }
  }
}
Root hierarchy node source code:

using VSXtra.Hierarchy;

namespace DeepDiver.BasicHierarchy
{
  [HierarchyBitmap("HomeImage")]
  public sealed class RootNode : FileHierarchyNode
  {
    public RootNode(FileHierarchyManager manager, string fullPath, string caption) : 
      base(manager, fullPath, caption)
    {
    }
  }
}
Folder node source code:

using VSXtra.Hierarchy;

namespace DeepDiver.BasicHierarchy
{
  [HierarchyBitmap("FolderImage")]
  [SortPriority(20)]
  public class FolderNode : FileHierarchyNode
  {
    public FolderNode(FileHierarchyManager manager, string fullPath, string caption) : 
      base(manager, fullPath, caption)
    {
    }
  }
}
File node source code:

using VSXtra.Hierarchy;

namespace DeepDiver.BasicHierarchy
{
  [HierarchyBitmap("FileImage")]
  [SortPriority(30)]
  public class FileNode : FileHierarchyNode
  {
    public FileNode(FileHierarchyManager manager, string fullPath, string caption) : 
      base(manager, fullPath, caption)
    {
    }
  }
}
Screenshots:

BasicHierarchyToolWindow1.png

Last edited Dec 1, 2008 at 1:00 PM by INovak, version 5

Comments

No comments yet.