//------------------------------------------------------------------------------
//
// Copyright (c) Company. All rights reserved.
//
//------------------------------------------------------------------------------
using System;
using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Collections.Specialized;
namespace SolutionOpen.Command
{
///
/// Command handler
///
internal sealed class HeaderFlip
{
///
/// Command ID.
///
public const int CommandId = 4129;
///
/// Command menu group (command set GUID).
///
public static readonly Guid CommandSet = new Guid("f044c1a5-742b-472d-b45a-fc2b5df582f9");
///
/// VS Package that provides this command, not null.
///
private readonly Package package;
///
/// Initializes a new instance of the class.
/// Adds our command handlers for menu (commands must exist in the command table file)
///
/// Owner package, not null.
private HeaderFlip(Package package)
{
if(package == null)
{
throw new ArgumentNullException("package");
}
this.package = package;
OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if(commandService != null)
{
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
commandService.AddCommand(menuItem);
}
}
///
/// Gets the instance of the command.
///
public static HeaderFlip Instance
{
get;
private set;
}
///
/// Gets the service provider from the owner package.
///
private IServiceProvider ServiceProvider
{
get
{
return this.package;
}
}
///
/// Initializes the singleton instance of the command.
///
/// Owner package, not null.
public static void Initialize(Package package)
{
Instance = new HeaderFlip(package);
}
///
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
///
/// Event sender.
/// Event args.
private void MenuItemCallback(object sender, EventArgs e)
{
// make sure there is an active document
if (SolutionOpenPackage.ApplicationObject.ActiveDocument != null)
{
// Get the current active document and its base name
String activeDocumentFullName = SolutionOpenPackage.ApplicationObject.ActiveDocument.FullName;
String activeDocumentBaseName = SolutionOpenPackage.BaseName(activeDocumentFullName);
// find the files with the same base name
StringCollection matchingFilePaths = new StringCollection();
foreach (String fullName in SolutionOpenPackage.FilePaths.Values)
{
String baseName = SolutionOpenPackage.BaseName(fullName);
if (String.Compare(activeDocumentBaseName, baseName, true) == 0)
matchingFilePaths.Add(fullName);
}
// make sure that we have more than one of them open before we try to switch
if (matchingFilePaths.Count > 1)
{
// figure out which document in the list (if any) is the current document
int activeDocumentIndex = -1;
for (int i = 0; activeDocumentIndex < 0 && i < matchingFilePaths.Count; ++i)
{
if (String.Compare(activeDocumentFullName, matchingFilePaths[i], true) == 0)
activeDocumentIndex = i;
}
// make sure we found this document
if (activeDocumentIndex >= 0)
{
// figure out which document comes next
int nextDocument = activeDocumentIndex + 1;
if (nextDocument >= matchingFilePaths.Count)
nextDocument = 0;
// open and display that document
SolutionOpenPackage.ApplicationObject.ItemOperations.OpenFile(matchingFilePaths[nextDocument], EnvDTE.Constants.vsViewKindAny);
}
}
}
}
}
}