PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/Foundation.Build.VisualBasic6.VisualStudioIntegration/ProjectBase/SelectionListener.cs

https://github.com/DavidMoore/Foundation
C# | 148 lines | 104 code | 19 blank | 25 comment | 12 complexity | 6a2797d82ed9f5a2cb338246e0c7b82d MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. * ***************************************************************************/
  14. using System;
  15. using System.Diagnostics;
  16. using Microsoft.VisualStudio;
  17. using Microsoft.VisualStudio.Shell;
  18. using Microsoft.VisualStudio.Shell.Interop;
  19. using ShellConstants = Microsoft.VisualStudio.Shell.Interop.Constants;
  20. namespace Microsoft.VisualStudio.Project
  21. {
  22. [CLSCompliant(false)]
  23. public abstract class SelectionListener : IVsSelectionEvents, IDisposable
  24. {
  25. #region fields
  26. private uint eventsCookie;
  27. private IVsMonitorSelection monSel;
  28. private ServiceProvider serviceProvider;
  29. private bool isDisposed;
  30. /// <summary>
  31. /// Defines an object that will be a mutex for this object for synchronizing thread calls.
  32. /// </summary>
  33. private static volatile object Mutex = new object();
  34. #endregion
  35. #region ctors
  36. protected SelectionListener(ServiceProvider serviceProviderParameter)
  37. {
  38. if (serviceProviderParameter == null)
  39. {
  40. throw new ArgumentNullException("serviceProviderParameter");
  41. }
  42. this.serviceProvider = serviceProviderParameter;
  43. this.monSel = this.serviceProvider.GetService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection;
  44. if(this.monSel == null)
  45. {
  46. throw new InvalidOperationException();
  47. }
  48. }
  49. #endregion
  50. #region properties
  51. protected uint EventsCookie
  52. {
  53. get
  54. {
  55. return this.eventsCookie;
  56. }
  57. }
  58. protected IVsMonitorSelection SelectionMonitor
  59. {
  60. get
  61. {
  62. return this.monSel;
  63. }
  64. }
  65. protected ServiceProvider ServiceProvider
  66. {
  67. get
  68. {
  69. return this.serviceProvider;
  70. }
  71. }
  72. #endregion
  73. #region IVsSelectionEvents Members
  74. public virtual int OnCmdUIContextChanged(uint dwCmdUICookie, int fActive)
  75. {
  76. return VSConstants.E_NOTIMPL;
  77. }
  78. public virtual int OnElementValueChanged(uint elementid, object varValueOld, object varValueNew)
  79. {
  80. return VSConstants.E_NOTIMPL;
  81. }
  82. public virtual int OnSelectionChanged(IVsHierarchy pHierOld, uint itemidOld, IVsMultiItemSelect pMISOld, ISelectionContainer pSCOld, IVsHierarchy pHierNew, uint itemidNew, IVsMultiItemSelect pMISNew, ISelectionContainer pSCNew)
  83. {
  84. return VSConstants.E_NOTIMPL;
  85. }
  86. #endregion
  87. #region IDisposable Members
  88. /// <summary>
  89. /// The IDispose interface Dispose method for disposing the object determinastically.
  90. /// </summary>
  91. public void Dispose()
  92. {
  93. this.Dispose(true);
  94. GC.SuppressFinalize(this);
  95. }
  96. #endregion
  97. #region methods
  98. public void Init()
  99. {
  100. if(this.SelectionMonitor != null)
  101. {
  102. ErrorHandler.ThrowOnFailure(this.SelectionMonitor.AdviseSelectionEvents(this, out this.eventsCookie));
  103. }
  104. }
  105. /// <summary>
  106. /// The method that does the cleanup.
  107. /// </summary>
  108. /// <param name="disposing"></param>
  109. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "Microsoft.VisualStudio.Shell.Interop.IVsMonitorSelection.UnadviseSelectionEvents(System.UInt32)")]
  110. protected virtual void Dispose(bool disposing)
  111. {
  112. // Everybody can go here.
  113. if(!this.isDisposed)
  114. {
  115. // Synchronize calls to the Dispose simulteniously.
  116. lock(Mutex)
  117. {
  118. if(disposing && this.eventsCookie != (uint)ShellConstants.VSCOOKIE_NIL && this.SelectionMonitor != null)
  119. {
  120. this.SelectionMonitor.UnadviseSelectionEvents((uint)this.eventsCookie);
  121. this.eventsCookie = (uint)ShellConstants.VSCOOKIE_NIL;
  122. }
  123. this.isDisposed = true;
  124. }
  125. }
  126. }
  127. #endregion
  128. }
  129. }