using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using EnvDTE;
using AnticipatingMinds.Genesis.KnowledgeManagement;
namespace AnticipatingMinds.DevAdvantageVSAddIn{
/// <summary>
/// Extender Provider object. This is the "class factory" for the Extender and is registered
/// under the CATIDs of the solution objects (vsCATIDSolution and vsCATIDSolutionBrowseObject)
/// it is extending. See SolutionExtender.reg for the registration entries.
/// </summary>
[ComVisibleAttribute(true)]
[GuidAttribute("E480F8BD-8327-41EC-BBD4-637C21E57FE5")]
[ProgId("DevAdvantage.SolutionAndProjectExtenderProvider")]
public class ExtenderProvider : Object, IExtenderProvider
{
/// <summary>
/// Constructor does nothing special.
/// </summary>
public ExtenderProvider(_DTE applicationObject,KnowledgeBase knowledgeBase)
{
this.knowledgeBase = knowledgeBase;
this.applicationObject = applicationObject;
}
//IExtenderProvider implementation
/// <summary>
/// Implementation of IExtenderProvider::CanExtend.
/// </summary>
/// <param name="ExtenderCATID">CATID of the object being extended.</param>
/// <param name="ExtenderName">Name of the Extension.</param>
/// <param name="ExtendeeObject">Object being extended.</param>
/// <returns>true if can provide an extender for Extendee Object, false otherwise.</returns>
public bool CanExtend(string ExtenderCATID, string ExtenderName, object ExtendeeObject)
{
//Extends the solution automation and browseobject unconditionally.
//The extension name is given by SlnAutExtension ("SolutionMisc").
if(ExtenderName.ToUpper() != SolutionExtenderName.ToUpper() && ExtenderName.ToUpper() != ProjectExtenderName.ToUpper())
return false;
if (ExtenderCATID.ToUpper() == EnvDTE.Constants.vsCATIDSolutionBrowseObject.ToUpper())
return true;
if (ExtenderCATID.ToUpper() == EnvDTE.Constants.vsCATIDSolution.ToUpper())
return true;
if (ExtenderCATID.ToUpper() == DevAdvantage.CSharpProjectCATID_GUID.ToUpper())
return true;
if (ExtenderCATID.ToUpper() == VSLangProj.PrjCATID.prjCATIDProject.ToUpper())
return true;
return false;
}
/// <summary>
/// Implementation of IExtenderProvider::GetExtender.
/// </summary>
/// <param name="ExtenderCATID">CATID of the object being extended.</param>
/// <param name="ExtenderName">Name of the Extension.</param>
/// <param name="ExtendeeObject">Object being extended.</param>
/// <param name="ExtenderSite">Site object for the Extender.</param>
/// <param name="cookie">Cookie value that identifies the Extender to its Site.</param>
/// <returns>A newly created Extender object.</returns>
public object GetExtender(string ExtenderCATID, string ExtenderName, object ExtendeeObject, EnvDTE.IExtenderSite ExtenderSite, int Cookie)
{
object extender = null; //In case of failure.
if(servicedExtenders.Contains(ExtendeeObject))
return servicedExtenders[ExtendeeObject];
if (CanExtend(ExtenderCATID, ExtenderName, ExtendeeObject))
{
if (((ExtenderCATID.ToUpper() == EnvDTE.Constants.vsCATIDSolutionBrowseObject.ToUpper()) ||
(ExtenderCATID.ToUpper() == EnvDTE.Constants.vsCATIDSolution.ToUpper())) &&
ExtenderName.ToUpper() == SolutionExtenderName.ToUpper())
{
Solution solution = ExtendeeObject as Solution;
if (solution == null)
{
//Extending the Soltuion browse object (the object that's displayed
//in the property browser when the Solution Node is selected in
//the Solution Explorer). In this case, get the DTE.Solution object
//directly from the object model.
EnvDTE.DTE root = (EnvDTE.DTE) ExtenderSite.GetObject("");
solution = root.Solution;
}
if (solution != null)
{
//Init our extender.
extender = new SolutionExtender(solution,knowledgeBase,Cookie,ExtenderSite);
}
}
else
{
//Extending the project
VSLangProj.ProjectProperties projectProperties = ExtendeeObject as VSLangProj.ProjectProperties;
foreach(Project project in applicationObject.Solution.Projects)
{
Project extendedProject = FindProject(project,projectProperties.__id);
if(extendedProject != null)
{
extender = new ProjectExtender(extendedProject,knowledgeBase,Cookie,ExtenderSite);
break;
}
}
}
}
return extender;
}
private Project FindProject(Project rootProject,string targetedProjectId)
{
string projectId = string.Empty;
//Some etp projects hate Name property and throw exception.
//just ignore it, we are more intrested in their sub projects than in them itself
try
{
projectId = rootProject.Name;
}
catch(Exception)
{
}
//Is it us?
if(projectId == targetedProjectId)
return rootProject;
if(rootProject.ProjectItems != null)
{
foreach(ProjectItem projectItem in rootProject.ProjectItems)
{
Project subProject = projectItem.SubProject;
if(subProject != null)
{
Project foundProject = FindProject(subProject,targetedProjectId);
if(foundProject != null)
return foundProject;
}
}
}
return null;
}
/// <summary>
/// The Extension name for this Extender.
/// </summary>
internal static string SolutionExtenderName = "DevAdvantageSolutionExtenderProvider";
internal static string ProjectExtenderName = "DevAdvantageProjectExtenderProvider";
private KnowledgeBase knowledgeBase;
private Hashtable servicedExtenders = new Hashtable();
private _DTE applicationObject;
}
}
|