using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
namespace ProjectEvents
{
  /// <summary>The object for implementing an Add-in.</summary>
  /// <seealso class='IDTExtensibility2' />
  public class Connect : IDTExtensibility2
  {
    /// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
    public Connect()
    {
    }

    /// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
    /// <param term='application'>Root object of the host application.</param>
    /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
    /// <param term='addInInst'>Object representing this Add-in.</param>
    /// <seealso class='IDTExtensibility2' />
    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
      _applicationObject = (DTE2)application;
      _addInInstance = (AddIn)addInInst;

      try
      {
        globalProjectItemsEvents = (EnvDTE.ProjectItemsEvents)((Events2)_applicationObject.Events).ProjectItemsEvents;
        globalProjectItemsEvents.ItemAdded += new _dispProjectItemsEvents_ItemAddedEventHandler(GlobalProjectsItemAdded);
        globalProjectItemsEvents.ItemRemoved += new _dispProjectItemsEvents_ItemRemovedEventHandler(GlobalProjectsItemRemoved);
        globalProjectItemsEvents.ItemRenamed += new _dispProjectItemsEvents_ItemRenamedEventHandler(GlobalProjectsItemRenamed);

        globalProjectsEvents = (EnvDTE.ProjectsEvents)((Events2)_applicationObject.Events).ProjectsEvents;
        globalProjectsEvents.ItemAdded += new _dispProjectsEvents_ItemAddedEventHandler(GlobalsProjectAdded);
        globalProjectsEvents.ItemRemoved += new _dispProjectsEvents_ItemRemovedEventHandler(GlobalsProjectRemoved);
        globalProjectsEvents.ItemRenamed += new _dispProjectsEvents_ItemRenamedEventHandler(GlobalsProjectRenamed);
      }
      catch
      {
      }

      try
      {
        csharpProjectsEvents = (EnvDTE.ProjectsEvents)_applicationObject.Events.GetObject("CSharpProjectsEvents");
        csharpProjectsEvents.ItemAdded += new _dispProjectsEvents_ItemAddedEventHandler(CSharpProjectAdded);
        csharpProjectsEvents.ItemRemoved += new _dispProjectsEvents_ItemRemovedEventHandler(CSharpProjectRemoved);
        csharpProjectsEvents.ItemRenamed += new _dispProjectsEvents_ItemRenamedEventHandler(CSharpProjectRenamed);
      }
      catch
      {
      }

      try
      {
        csharpProjectItemsEvents = (EnvDTE.ProjectItemsEvents)_applicationObject.Events.GetObject("CSharpProjectItemsEvents");
        csharpProjectItemsEvents.ItemAdded += new _dispProjectItemsEvents_ItemAddedEventHandler(CSharpProjectItemAdded);
        csharpProjectItemsEvents.ItemRemoved += new _dispProjectItemsEvents_ItemRemovedEventHandler(CSharpProjectItemRemoved);
        csharpProjectItemsEvents.ItemRenamed += new _dispProjectItemsEvents_ItemRenamedEventHandler(CSharpProjectItemRenamed);
      }
      catch
      {
      }

      try
      {
        vbProjectsEvents = (EnvDTE.ProjectsEvents)_applicationObject.Events.GetObject("VBProjectsEvents");
        vbProjectsEvents.ItemAdded += new _dispProjectsEvents_ItemAddedEventHandler(VBProjectAdded);
        vbProjectsEvents.ItemRemoved += new _dispProjectsEvents_ItemRemovedEventHandler(VBProjectRemoved);
        vbProjectsEvents.ItemRenamed += new _dispProjectsEvents_ItemRenamedEventHandler(VBProjectRenamed);
      }
      catch
      {
      }

      try
      {
        vbProjectItemsEvents = (EnvDTE.ProjectItemsEvents)_applicationObject.Events.GetObject("VBProjectItemsEvents");
        vbProjectItemsEvents.ItemAdded += new _dispProjectItemsEvents_ItemAddedEventHandler(VBProjectItemAdded);
        vbProjectItemsEvents.ItemRemoved += new _dispProjectItemsEvents_ItemRemovedEventHandler(VBProjectItemRemoved);
        vbProjectItemsEvents.ItemRenamed += new _dispProjectItemsEvents_ItemRenamedEventHandler(VBProjectItemRenamed);
      }
      catch
      {
      }

      try
      {
        vjProjectsEvents = (EnvDTE.ProjectsEvents)_applicationObject.Events.GetObject("VJSharpProjectsEvents");
        vjProjectsEvents.ItemAdded += new _dispProjectsEvents_ItemAddedEventHandler(JSharpProjectAdded);
        vjProjectsEvents.ItemRemoved += new _dispProjectsEvents_ItemRemovedEventHandler(JSharpProjectRemoved);
        vjProjectsEvents.ItemRenamed += new _dispProjectsEvents_ItemRenamedEventHandler(JSharpProjectRenamed);
      }
      catch
      {
      }

      try
      {
        vjProjectItemsEvents = (EnvDTE.ProjectItemsEvents)_applicationObject.Events.GetObject("VJSharpProjectItemsEvents");
        vjProjectItemsEvents.ItemAdded += new _dispProjectItemsEvents_ItemAddedEventHandler(JSharpProjectItemAdded);
        vjProjectItemsEvents.ItemRemoved += new _dispProjectItemsEvents_ItemRemovedEventHandler(JSharpProjectItemRemoved);
        vjProjectItemsEvents.ItemRenamed += new _dispProjectItemsEvents_ItemRenamedEventHandler(JSharpProjectItemRenamed);
      }
      catch
      {
      }

      try
      {
        miscFilesProjectItemsEvents = _applicationObject.Events.MiscFilesEvents;
        miscFilesProjectItemsEvents.ItemAdded += new _dispProjectItemsEvents_ItemAddedEventHandler(MiscFilesProjectItemAdded);
        miscFilesProjectItemsEvents.ItemRemoved += new _dispProjectItemsEvents_ItemRemovedEventHandler(MiscFilesProjectItemRemoved);
        miscFilesProjectItemsEvents.ItemRenamed += new _dispProjectItemsEvents_ItemRenamedEventHandler(MiscFilesProjectItemRenamed);
      }
      catch
      {
      }

      try
      {
        solutionItemsProjectItemsEvents = _applicationObject.Events.SolutionItemsEvents;
        solutionItemsProjectItemsEvents.ItemAdded += new _dispProjectItemsEvents_ItemAddedEventHandler(SolutionItemsProjectItemAdded);
        solutionItemsProjectItemsEvents.ItemRemoved += new _dispProjectItemsEvents_ItemRemovedEventHandler(SolutionItemsProjectItemRemoved);
        solutionItemsProjectItemsEvents.ItemRenamed += new _dispProjectItemsEvents_ItemRenamedEventHandler(SolutionItemsProjectItemRenamed);
      }
      catch
      {
      }
    }

    /// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
    /// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />
    public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
    {
      if (globalProjectItemsEvents != null)
      {
        globalProjectItemsEvents.ItemAdded -= new _dispProjectItemsEvents_ItemAddedEventHandler(GlobalProjectsItemAdded);
        globalProjectItemsEvents.ItemRemoved -= new _dispProjectItemsEvents_ItemRemovedEventHandler(GlobalProjectsItemRemoved);
        globalProjectItemsEvents.ItemRenamed -= new _dispProjectItemsEvents_ItemRenamedEventHandler(GlobalProjectsItemRenamed);
      }

      if (globalProjectsEvents != null)
      {
        globalProjectsEvents.ItemAdded -= new _dispProjectsEvents_ItemAddedEventHandler(GlobalsProjectAdded);
        globalProjectsEvents.ItemRemoved -= new _dispProjectsEvents_ItemRemovedEventHandler(GlobalsProjectRemoved);
        globalProjectsEvents.ItemRenamed -= new _dispProjectsEvents_ItemRenamedEventHandler(GlobalsProjectRenamed);
      }

      if (csharpProjectsEvents != null)
      {
        csharpProjectsEvents.ItemAdded -= new _dispProjectsEvents_ItemAddedEventHandler(CSharpProjectAdded);
        csharpProjectsEvents.ItemRemoved -= new _dispProjectsEvents_ItemRemovedEventHandler(CSharpProjectRemoved);
        csharpProjectsEvents.ItemRenamed -= new _dispProjectsEvents_ItemRenamedEventHandler(CSharpProjectRenamed);
      }

      if (csharpProjectItemsEvents != null)
      {
        csharpProjectItemsEvents.ItemAdded -= new _dispProjectItemsEvents_ItemAddedEventHandler(CSharpProjectItemAdded);
        csharpProjectItemsEvents.ItemRemoved -= new _dispProjectItemsEvents_ItemRemovedEventHandler(CSharpProjectItemRemoved);
        csharpProjectItemsEvents.ItemRenamed -= new _dispProjectItemsEvents_ItemRenamedEventHandler(CSharpProjectItemRenamed);
      }

      if (vbProjectsEvents != null)
      {
        vbProjectsEvents.ItemAdded -= new _dispProjectsEvents_ItemAddedEventHandler(VBProjectAdded);
        vbProjectsEvents.ItemRemoved -= new _dispProjectsEvents_ItemRemovedEventHandler(VBProjectRemoved);
        vbProjectsEvents.ItemRenamed -= new _dispProjectsEvents_ItemRenamedEventHandler(VBProjectRenamed);
      }

      if (vbProjectItemsEvents != null)
      {
        vbProjectItemsEvents.ItemAdded -= new _dispProjectItemsEvents_ItemAddedEventHandler(VBProjectItemAdded);
        vbProjectItemsEvents.ItemRemoved -= new _dispProjectItemsEvents_ItemRemovedEventHandler(VBProjectItemRemoved);
        vbProjectItemsEvents.ItemRenamed -= new _dispProjectItemsEvents_ItemRenamedEventHandler(VBProjectItemRenamed);
      }

      if (vjProjectsEvents != null)
      {
        vjProjectsEvents.ItemAdded -= new _dispProjectsEvents_ItemAddedEventHandler(JSharpProjectAdded);
        vjProjectsEvents.ItemRemoved -= new _dispProjectsEvents_ItemRemovedEventHandler(JSharpProjectRemoved);
        vjProjectsEvents.ItemRenamed -= new _dispProjectsEvents_ItemRenamedEventHandler(JSharpProjectRenamed);
      }

      if (vjProjectItemsEvents != null)
      {
        vjProjectItemsEvents.ItemAdded -= new _dispProjectItemsEvents_ItemAddedEventHandler(JSharpProjectItemAdded);
        vjProjectItemsEvents.ItemRemoved -= new _dispProjectItemsEvents_ItemRemovedEventHandler(JSharpProjectItemRemoved);
        vjProjectItemsEvents.ItemRenamed -= new _dispProjectItemsEvents_ItemRenamedEventHandler(JSharpProjectItemRenamed);
      }

      if (miscFilesProjectItemsEvents != null)
      {
        miscFilesProjectItemsEvents.ItemAdded -= new _dispProjectItemsEvents_ItemAddedEventHandler(MiscFilesProjectItemAdded);
        miscFilesProjectItemsEvents.ItemRemoved -= new _dispProjectItemsEvents_ItemRemovedEventHandler(MiscFilesProjectItemRemoved);
        miscFilesProjectItemsEvents.ItemRenamed -= new _dispProjectItemsEvents_ItemRenamedEventHandler(MiscFilesProjectItemRenamed);
      }

      if (solutionItemsProjectItemsEvents != null)
      {
        solutionItemsProjectItemsEvents.ItemAdded -= new _dispProjectItemsEvents_ItemAddedEventHandler(SolutionItemsProjectItemAdded);
        solutionItemsProjectItemsEvents.ItemRemoved -= new _dispProjectItemsEvents_ItemRemovedEventHandler(SolutionItemsProjectItemRemoved);
        solutionItemsProjectItemsEvents.ItemRenamed -= new _dispProjectItemsEvents_ItemRenamedEventHandler(SolutionItemsProjectItemRenamed);
      }
    }

    /// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />		
    public void OnAddInsUpdate(ref Array custom)
    {
    }

    /// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />
    public void OnStartupComplete(ref Array custom)
    {
    }

    /// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />
    public void OnBeginShutdown(ref Array custom)
    {
    }

    void GlobalsProjectAdded(EnvDTE.Project Project)
    {
      System.Windows.Forms.MessageBox.Show("Global project added: " + Project.Name);
    }

    void GlobalsProjectRemoved(EnvDTE.Project Project)
    {
      System.Windows.Forms.MessageBox.Show("Global project removed: " + Project.Name);
    }

    void GlobalsProjectRenamed(EnvDTE.Project Project, string OldName)
    {
      System.Windows.Forms.MessageBox.Show("Global project renamed: " + Project.Name);
    }

    void GlobalProjectsItemAdded(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("Global project item added: " + ProjectItem.Name);
    }

    void GlobalProjectsItemRemoved(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("Global project item removed: " + ProjectItem.Name);
    }

    void GlobalProjectsItemRenamed(EnvDTE.ProjectItem ProjectItem, string OldName)
    {
      System.Windows.Forms.MessageBox.Show("Global project item renamed: " + ProjectItem.Name);
    }

    void CSharpProjectAdded(EnvDTE.Project Project)
    {
      System.Windows.Forms.MessageBox.Show("C# project added: " + Project.Name);
    }

    void CSharpProjectRemoved(EnvDTE.Project Project)
    {
      System.Windows.Forms.MessageBox.Show("C# project removed: " + Project.Name);
    }

    void CSharpProjectRenamed(EnvDTE.Project Project, string OldName)
    {
      System.Windows.Forms.MessageBox.Show("C# project renamed: " + Project.Name);
    }

    void CSharpProjectItemAdded(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("C# project item added: " + ProjectItem.Name);
    }

    void CSharpProjectItemRemoved(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("C# project item removed: " + ProjectItem.Name);
    }

    void CSharpProjectItemRenamed(EnvDTE.ProjectItem ProjectItem, string OldName)
    {
      System.Windows.Forms.MessageBox.Show("C# project item renamed: " + ProjectItem.Name);
    }

    void VBProjectItemAdded(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("VB project item added: " + ProjectItem.Name);
    }

    void VBProjectItemRemoved(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("VB project item removed: " + ProjectItem.Name);
    }

    void VBProjectItemRenamed(EnvDTE.ProjectItem ProjectItem, string OldName)
    {
      System.Windows.Forms.MessageBox.Show("VB project item renamed: " + ProjectItem.Name);
    }

    void VBProjectAdded(EnvDTE.Project Project)
    {
      System.Windows.Forms.MessageBox.Show("VB project added: " + Project.Name);
    }

    void VBProjectRemoved(EnvDTE.Project Project)
    {
      System.Windows.Forms.MessageBox.Show("VB project removed: " + Project.Name);
    }

    void VBProjectRenamed(EnvDTE.Project Project, string OldName)
    {
      System.Windows.Forms.MessageBox.Show("VB project renamed: " + Project.Name);
    }

    void JSharpProjectAdded(EnvDTE.Project Project)
    {
      System.Windows.Forms.MessageBox.Show("J# project added: " + Project.Name);
    }

    void JSharpProjectRemoved(EnvDTE.Project Project)
    {
      System.Windows.Forms.MessageBox.Show("J# project removed: " + Project.Name);
    }

    void JSharpProjectRenamed(EnvDTE.Project Project, string OldName)
    {
      System.Windows.Forms.MessageBox.Show("J# project renamed: " + Project.Name);
    }

    void JSharpProjectItemAdded(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("J# project item added: " + ProjectItem.Name);
    }

    void JSharpProjectItemRemoved(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("J# project item removed: " + ProjectItem.Name);
    }

    void JSharpProjectItemRenamed(EnvDTE.ProjectItem ProjectItem, string OldName)
    {
      System.Windows.Forms.MessageBox.Show("J# project item renamed: " + ProjectItem.Name);
    }

    void MiscFilesProjectItemAdded(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("Misc Files project item added: " + ProjectItem.Name);
    }

    void MiscFilesProjectItemRemoved(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("Misc Files project item removed: " + ProjectItem.Name);
    }

    void MiscFilesProjectItemRenamed(EnvDTE.ProjectItem ProjectItem, string OldName)
    {
      System.Windows.Forms.MessageBox.Show("Misc Files project item renamed: " + ProjectItem.Name);
    }

    void SolutionItemsProjectItemAdded(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("Solution Items project item added: " + ProjectItem.Name);
    }

    void SolutionItemsProjectItemRemoved(EnvDTE.ProjectItem ProjectItem)
    {
      System.Windows.Forms.MessageBox.Show("Solution Items project item removed: " + ProjectItem.Name);
    }

    void SolutionItemsProjectItemRenamed(EnvDTE.ProjectItem ProjectItem, string OldName)
    {
      System.Windows.Forms.MessageBox.Show("Solution Items project item renamed: " + ProjectItem.Name);
    }

    private DTE2 _applicationObject;
    private AddIn _addInInstance;
    private EnvDTE.ProjectsEvents csharpProjectsEvents;
    private EnvDTE.ProjectItemsEvents csharpProjectItemsEvents;
    private EnvDTE.ProjectsEvents vbProjectsEvents;
    private EnvDTE.ProjectItemsEvents vbProjectItemsEvents;
    private EnvDTE.ProjectsEvents vjProjectsEvents;
    private EnvDTE.ProjectItemsEvents vjProjectItemsEvents;
    private EnvDTE.ProjectItemsEvents miscFilesProjectItemsEvents;
    private EnvDTE.ProjectItemsEvents solutionItemsProjectItemsEvents;
    private EnvDTE.ProjectItemsEvents globalProjectItemsEvents;
    private EnvDTE.ProjectsEvents globalProjectsEvents;
  }
}