Persisted Tool Window Sample

Package source code:

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

namespace DeepDiver.PersistedToolWindow
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "PersistedToolWindow", "DeepDiver", 1)]
  [ProvideMenuResource(1000, 1)]
  [ProvideToolWindow(typeof(PersistedWindowPane))]
  [Guid(GuidList.guidPersistedToolWindowPkgString)]
  public sealed class PersistedToolWindowPackage : PackageBase
  {
  }
}
Persisted Tool Window pane source code:

using System;
using System.Runtime.InteropServices;
using VSXtra;

namespace DeepDiver.PersistedToolWindow
{
  [Guid("0A6F8EDC-5DDB-4aaa-A6B3-2AC1E319693E")]
  [InitialCaption("Persisted Tool Window")]
  [BitmapResourceId(301)]
  [Toolbar(typeof(DynamicToolWindowCommandGroup.PersistedWindowToolbar))]
  class PersistedWindowPane : ToolWindowPane<PersistedToolWindowPackage, PersistedWindowControl>
  {
    public override void OnToolWindowCreated()
    {
      base.OnToolWindowCreated();
      UIControl.SelectionTracker = SelectionTracker;
      SelectionTracker.SelectionChanged += OnSelectionChanged;
      RefreshList();
    }

    private void OnSelectionChanged(object sender, EventArgs e)
    {
      UIControl.ChangeSelection(SelectionTracker.GetSelectedObject<SelectionProperties>());
    }

    [CommandExecMethod]
    [Promote]
    [CommandId(CmdIDs.cmdidRefreshWindowsList)]
    private void RefreshList()
    {
      UIControl.RefreshData();
    }
  }
}
Command handler source code

using System.Runtime.InteropServices;
using VSXtra;

namespace DeepDiver.PersistedToolWindow
{
  [Guid(GuidList.guidPersistedToolWindowCmdSetString)]
  public sealed class DynamicToolWindowCommandGroup : CommandGroup<PersistedToolWindowPackage>
  {
    [CommandId(CmdIDs.cmdidPersistedWindow)]
    [ShowToolWindowAction(typeof(PersistedWindowPane))]
    public sealed class ShowToolCommand : MenuCommandHandler { }

    [CommandId(CmdIDs.IDM_PersistedWindowToolbar)]
    public sealed class PersistedWindowToolbar : ToolbarDefinition {}
  }
}
Persisted tool window UI code

using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using VSXtra;

namespace DeepDiver.PersistedToolWindow
{
  public partial class PersistedWindowControl : UserControl
  {
    private List<WindowFrame> _ToolWindowList;
    private bool _IgnoreSelectedObjectsChanges;

    public PersistedWindowControl()
    {
      InitializeComponent();
    }

    public SelectionTracker SelectionTracker { get; set; }

    internal void RefreshData()
    {
      _ToolWindowList = new List<WindowFrame>(WindowFrame.ToolWindowFrames);
      PopulateListView();
    }

    private void PopulateListView()
    {
      listView1.Items.Clear();
      foreach (var windowFrame in _ToolWindowList)
      {
        listView1.Items.Add(windowFrame.Caption);
      }
      listView1.SelectedItems.Clear();
      listView1_SelectedIndexChanged(this, null);
      listView1.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
      listView1.Invalidate();
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (_IgnoreSelectedObjectsChanges) return;
      var selectedObjects = new List<SelectionProperties>();
      if (listView1.SelectedItems.Count > 0)
      {
        int index = listView1.SelectedItems[0].Index;
        var frame = _ToolWindowList[index];
        var properties = new SelectionProperties(frame.Caption, frame.Guid) { Index = index };
        selectedObjects.Add(properties);
      }
      SelectionTracker.SelectObjects(selectedObjects, WindowsProperties);
    }

    public void ChangeSelection(SelectionProperties selection)
    {
      _IgnoreSelectedObjectsChanges = true;
      try
      {
        if (selection != null) listView1.Items[selection.Index].Selected = true;
      }
      finally
      {
        _IgnoreSelectedObjectsChanges = false;
      }
    }

    private ArrayList WindowsProperties
    {
      get
      {
        int index = 0;
        var properties = new ArrayList();
        foreach (WindowFrame frame in _ToolWindowList)
        {
          var property = new SelectionProperties(frame.Caption, frame.Guid) { Index = index };
          properties.Add(property);
          ++index;
        }
        return properties;
      }
    }
  }
}
Screenshots:

PersistedToolWindow1.png

Last edited Aug 7, 2008 at 2:18 PM by INovak, version 7

Comments

No comments yet.